#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  vector<int> A(N), height(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  height[0] = 0;
  for (int i = 1; i < N; i++) {
    int prev = A[i - 1] + height[i - 1];
    height[i] = max(0, A[i] - prev);
  }
  long long sum = 0;
  for (auto h : height) {
    sum += h;
  }
  cout << sum << endl;
  return 0;
}
