#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto& i : a) cin >> i;
    long long res = 0;
    int delta = 0, lastDelta = 0, prev = -1;
    for (int i = a.size() - 1; i >= 0; --i) {
        if (prev != a[i]) {
            delta += lastDelta;
            lastDelta = 0;
        }
        res += max(0, a[i] - delta);
        ++lastDelta;
        prev = a[i];
    }
    cout << res << '\n';
    return 0;
}
