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