#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int height = 0;
  int step = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] < height) {
      step += height - A[i];
    } else {
      height = A[i];
    }
  }
  cout << step << endl;
  return 0;
}
