#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];
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      while (a[j] < a[i]) {
        ans++;
        a[j]++;
      }
      break;
    }
  }
  cout << ans;
  return 0;
}
