#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> v;
  for (long long i = (0); i < (n); ++i) {
    long long a;
    cin >> a;
    v.push_back(a);
  }
  long long ans = 0;
  for (long long i = (1); i < (v.size()); ++i) {
    long long a = min(0LL, v[i - 1] - v[i]);
    v[i] -= a;
    ans -= a;
  }
  cout << ans << endl;
  return 0;
}
