#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T = 1;
  while (T--) {
    long long n;
    cin >> n;
    long long a[n], tot = 0;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      if (i) tot += max(0LL, a[i] - a[i - 1]);
    }
    cout << tot << '\n';
  }
  return 0;
}
