#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio();cin.tie();cout.tie();
#define ops cout<<"ops"<<endl;
#define en cout<<endl;
#define fi first
#define se second

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pllll;
typedef string str;
typedef char cr;

const ll DIM=1e5+7;
const ll DIMM=1e3+7;
const ll DDIM=1e7+7;
const ll INF=1e16+7;
const ll X=1e9;
const ll BS=37;
const ll MODULO=1e9+7;

ll n,m,k;

ll a[DIM];

void solve(){

    cin>>n;

    for(int i=1;i<=n;i++){
     cin>>a[i];
    }

    ll res=0;
    ll dif=0;
    ll c=0;

    for(int i=n;i>=1;i--){

      if(a[i]==a[i-1]){
       c++;
       continue;
      }

      if((a[i]-dif)<1)break;

      c++;

      res+=c*(a[i]-dif);
      dif+=c;

      c=0;
    }

    cout<<res<<endl;

    return;
}

int main()
{

    fast;

    int nt;

    nt=1;

    //cin>>nt;

    while(nt--)
        solve();

    return 0;
}
