#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n = 0;
  long long sum = 0, cur = 0;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (A[i] + cur > A[i + 1]) {
      sum += A[i] + cur - A[i + 1];
      cur = A[i] + cur - A[i + 1];
    } else {
      cur = 0;
    }
  }
  if (A[n - 1] + cur > A[n]) sum += A[n] + cur - A[n - 1];
  cout << sum << endl;
}
