#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const* argv[]) {
  int n = 0;
  vector<int> ai;
  cin >> n;
  int i;
  while (cin >> i) {
    ai.push_back(i);
  }
  int total = 0;
  int before_box = 0;
  for (auto it = ai.begin(); it + 1 < ai.end();) {
    int before_length = *it++ + before_box;
    int next_length = *it;
    if (next_length - before_length >= 0) {
      before_box = 0;
    } else {
      before_box = before_length - next_length;
    }
    total += before_box;
  }
  cout << total << endl;
  return 0;
}
