#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int ans = 0;
  long long int step = 0;
  for (int i = 0; i < n - 1; i++) {
    if (step + a[i] < a[i + 1]) {
      step = a[i + 1] + step - a[i];
      ans += step;
    } else {
      step = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
