#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
signed main(void) {
  int n, cnt;
  cin >> n;
  int a[n];
  for (int i = 0; i < (n); i++) cin >> a[i];
  cnt = 0;
  for (int i = 1; i < n; i++) {
    cnt += max(a[i - 1] - a[i], 0);
    a[i] += max(a[i - 1] - a[i], 0);
  }
  cout << cnt << endl;
  return 0;
}
