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