#include <bits/stdc++.h>
using namespace std;
long long a[200005];
int main() {
  long long n, tong = 0;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) {
    if (a[i] < a[i + 1]) tong += a[i + 1] - a[i];
  }
  cout << tong;
}
