#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, h[10 * 20], all = 0, pre;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (long long i = 0; i < n; i++) {
    if (h[i] - h[i + 1] > 0) {
      pre = h[i] - h[i + 1];
      all = all + pre;
      h[i + 1] += pre;
    }
  }
  cout << all << endl;
}
