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