#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int amount = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i != 0) {
      int diff = A[i] - A[i - 1];
      if (diff > 0) {
        amount += diff;
      }
    }
  }
  cout << amount << endl;
}
