#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e15;
const int INF_INT = 1e9;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class TYPE>
struct edge {
  TYPE to, cost;
  edge(TYPE t, TYPE c) {
    to = t;
    cost = c;
  }
};
int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  long long res = 0;
  cin >> a[0];
  for (int i = 1; i < N; i++) {
    cin >> a[i];
    res += max(a[i] - a[i - 1], 0);
    a[i] += res;
  }
  cout << res << endl;
}
