#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long res = 0;
  long long prev;
  long long curr;
  cin >> prev;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i < n; ++i) {
    cin >> curr;
    if (curr > prev) {
      res += curr - prev;
    }
    prev = curr;
  }
  cout << res << endl;
}
