#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 10;
int n;
int lines_above_water[N];
int all_lines[N];
long long res;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; cin >> lines_above_water[i++])
    ;
  all_lines[1] = 1;
  for (int i = 2; i <= n; i++)
    all_lines[i] = max(lines_above_water[i] + 1, all_lines[i - 1]);
  for (int i = n; i >= 2; i--)
    all_lines[i - 1] = max(all_lines[i - 1], all_lines[i] - 1);
  for (int i = 1; i <= n; i++)
    res += (long long)all_lines[i] - (long long)lines_above_water[i] - 1ll;
  cout << res << endl;
  return 0;
}
