#include <bits/stdc++.h>
using namespace std;
char e = '\n';
long long MOD = 1e9 + 7;
long long i, j, le;
int sv(int x) {
  int n = 200000;
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  if (prime[x])
    return 1;
  else
    return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v(n, 0);
    for (long long i = 0; i < n; i++) cin >> v[i];
    long long ans = 0;
    for (long long i = 1; i < n; i++) {
      if (v[i] > v[i - 1]) ans += v[i] - v[i - 1];
    }
    cout << ans << e;
  }
  return 0;
}
