#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int x;
  cin >> x;
  vector<int> v(x);
  vector<int> vd(x);
  for (int i = 0; i < x; i++) {
    cin >> v[i];
    vd[i] = v[i];
  }
  int mid = max_element(v.begin(), v.end()) - v.begin();
  int maxx = *max_element(v.begin(), v.end());
  if (mid < x - 1) {
    for (int i = mid; i < x; i++) {
      if (v[i] < maxx) {
        vd[i] = maxx;
      }
    }
  }
  if (mid > 0) {
    for (int i = mid - 1; i >= 0; i--) {
      if (vd[i] < vd[i - 1] && i > 0) {
        while (1) {
          if (vd[i] == v[i - 1])
            break;
          else
            vd[i]++;
        }
      }
    }
  }
  for (int i = 1; i < x; i++) {
    if (vd[i - 1] > vd[i]) {
      vd[i] = vd[i - 1];
    }
  }
  long long int sum = 0;
  for (int i = 0; i < x; i++) {
    if (v[i] != vd[i]) {
      int def = vd[i] - v[i];
      sum = sum + def;
    }
  }
  cout << sum << endl;
  return 0;
}
