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

int main(){
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    int ans = 0;
    int c = a[(n - 1)] + 1;
    int d = 0;
    int e = 0;
    for (int i = n - 1; i > -1; i--){
        if (a[i] != c){
            c = a[i];
            if (c <= d){
                break;
            }
            else{
                e = d;
                ans += c;
                ans -= e;
                d++;
            }
        }
        else{
            ans += c;
            ans -= e;
            d++;
        }
    }
    cout << ans << endl;
}
