#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int stoles = 0;
  int previous = 0;
  int current;
  for (int i = 0; i < n; i++) {
    cin >> current;
    int diff = current - previous;
    if (diff < 0) {
      stoles -= diff;
      previous = current - diff;
    } else {
      previous = current;
    }
  }
  cout << stoles;
  return 0;
}
