#include <bits/stdc++.h>
using namespace std;
int main() {
  long N;
  cin >> N;
  vector<long> A(N);
  for (auto& a : A) {
    cin >> a;
  }
  long ans = 0;
  long prevStand = 0;
  for (long i = 1; i <= A.size(); i++) {
    long stand = A[i - 1] - A[i] + prevStand;
    if (stand > 0) {
      ans += stand;
      prevStand = stand;
    } else {
      prevStand = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
