#include <bits/stdc++.h>
using namespace std;
using vi = vector<long long int>;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
signed main() {
  long long int n;
  cin >> n;
  long long int ans = 0;
  long long int prev = 0;
  cin >> prev;
  for (long long int i = 0; i < n - 1; ++i) {
    long long int a;
    cin >> a;
    ans += max(0ll, a - prev);
    prev = a;
  }
  cout << ans;
  return 0;
}
