#include <bits/stdc++.h>
using namespace std;
int nmax = 201000;
int main() {
  int n, a[nmax];
  int answer = 0;
  int step = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    step = max(0, (a[i - 1] + step) - a[i]);
    answer += step;
  }
  cout << answer;
}
