#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("avx2")

using namespace std;

#define x first
#define y second
#define pb push_back
#define all(a) a.begin(),a.end()
#define int long long

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
typedef vector<ll>vll;
typedef vector<int>vi;
typedef vector<bool>vb;
typedef vector<vi>vvi;
typedef vector<vll>vvll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;

const int N=1e5+55,mod=1e9+7;
const char nl='\n';

int a[N];

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    a[n+1]=-1;
    vpii v;
    int last=1;
    for(int i=2;i<=n+1;++i){
        if(a[i]!=a[i-1]){
            v.pb({a[i-1],last});
            last=1;
        }
        else{
            last++;
        }
    }
    int left=0,ans=0;
    for(int i=v.size()-1;i>=0;--i){
        if(v[i].x-left<=0){
            break;
        }
        ans+=(v[i].x-left)*v[i].y;
        left+=v[i].y;
    }
    cout<<ans;
}
