#include <iostream>

using namespace std;

int a[10008];

int main() {
    int n;
    cin >> n;

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

    long long sum = 0;
    long long offset = 0;
    int prevnum = 0;
    for (int i = n - 1; i >= 0; --i) {
        long long woffset = offset;
        while (prevnum == a[i]){
            int addsum = a[i] - woffset + 1;
            if (addsum > 0) sum += addsum;

            offset++;
            prevnum = a[i];
            i--;
        }
        int addsum = a[i] - offset;
        if (addsum > 0) sum += addsum;
        offset++;
        prevnum = a[i];
    }

    cout << sum << endl;

    return 0;
}