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