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

int main(){
    int n;
    cin >> n;
    ll a[n];
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    ll ans = 0;
    ll c = a[(n - 1)] + 1;
    ll d = 0;
    ll 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;
}
