#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back
#define ff first
#define ss second
#define bust ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define nl '\n'

int main()
{
    bust
    ll n, ans = 0;
    cin >> n;
    vl vec(n);
    for(int i = 0; i < n; i++){
        cin >> vec[i];
    }
    ll pr = vec[n-1], cnt = 0, tek = -1;
    for(int i = n - 1; i >= 0; i--){
        if(vec[i] == pr){
            ans += pr - cnt;
            tek++;
        }
        else{
            cnt += tek+1;
            tek = 0;
            if(vec[i] - cnt > 1){
                ans += vec[i] - cnt;
                pr = vec[i];
            }
        }
//        cout << ans  << " " << cnt << '\n';
    }
    cout << ans << nl;
    return 0;
}




