#include <bits/stdc++.h>
using namespace std;
int hikaku(int A0, int A1, int T0, int T1) {
  if (A0 > A1) {
    T1 = (A0 + T0 - A1);
  } else {
    T1 = 0;
  }
  return T1;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> T(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int x = 0;
  for (int i = 0; i < N - 1; i++) {
    T.at(i + 1) = hikaku(A.at(i), A.at(i + 1), T.at(i), T.at(i + 1));
    x += T.at(i + 1);
  }
  cout << x << endl;
  return 0;
}
