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