#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#define ll       long long
#define endl     "\n"
using namespace std;
ll z[100003], l[100003], t[100003];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll i, n, s = 0;
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        cin >> z[i];
    }
    for (i = n; i >= 0; i--)
    {
        if (z[i] - t[i + 1] <= 0)
        {
            cout << s;
            return 0;
        }
        else
        {
            s += z[i] - t[i + 1];
        }
        if (z[i] == z[i - 1])
        {
            t[i] = t[i + 1];
            l[i] = l[i + 1] + 1;
        }
        else
        {
            t[i] = t[i + 1] + l[i + 1] + 1;
        }
    }
    return 0;
}

