#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 10e5;
int n, a[MAXN], ans = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int maxn = a[1];
  for (int i = 2; i <= n; i++) {
    if (a[i] > maxn) maxn = a[i];
    while (a[i] < maxn) {
      ans++;
      a[i]++;
    }
  }
  cout << ans;
  return 0;
}
