#include <iostream>

const int N = 2e5;

using namespace std;
typedef long long ll;

int main(){
    int n, a[N];
    cin >> n;
    for(int i = 0; i < n; ++i)
        cin >> a[i];
    ll rs = 0, cnt = 0;
    pair<int, int> pr = {n - 1, -1};
    for(int i = n - 1; i >= 0; --i){
        if(a[i] == pr.second)
            rs += max(0ll, a[i] - cnt);
        else{
            cnt += pr.first - i;
            rs += max(0ll, a[i] - cnt);
            pr = {i, a[i]};
        }
    }
    cout << rs << '\n';
}
