#pragma GCC Optimize("O3")
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<ll,ll>pairll;
typedef long double ld;

#define pb push_back
#define fr first
#define sc second
#define INF 1000000000000000007
#define MOD 1000000007
#define endl '\n'

ll n,d[100007],t[100007],res;

int main() {

    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>d[i];
    }
    for(int i=n-1;i>=1;i--){
        if(d[i]!=d[i+1])t[i]=n-i;
        else t[i]=t[i+1];
    }
    for(int i=n;i>=1;i--){
        res+=max(0ll,d[i]-t[i]);
    }
    cout<<res<<endl;
    
    return 0;

}
