#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  while (tc--) {
    int n;
    cin >> n;
    vector<int> h(n);
    for (long long i = 0; i < n; i++) {
      cin >> h[i];
    }
    int ans = 0;
    for (long long i = 1; i < n; i++) {
      int tmp = max(h[i] - h[i - 1], 0);
      ans += (tmp);
    }
    cout << ans << "\n";
  }
  return 0;
}
