#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	int n;
	cin>>n;
    int arr[n];
    
    for(int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int a = 0, b = 0;
    long long res = 0;
    for(int i = n-1; i >= 0; i--)
    {
        b = 0;
        if(a >= arr[i]) break;
        for(;i > 0, arr[i] == arr[i-1]; i--)
        {
            res += arr[i] - a;
            b++;
        }
        res += arr[i] - a;
        b++;
        a += b;
    }
    // if(arr[0] > a)  res += arr[0] - a;
    cout << res;
}
