#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
int main() {
  int n;
  cin >> n;
  long long a[n];
  long long ans = 0;
  for (int i = 0; i < (int)(n); i++) {
    cin >> a[i];
    if (i > 0) {
      ans += min(-(a[i] - a[i - 1]), long long(0));
    }
  }
  cout << abs(ans) << endl;
}
