
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <stdio.h>
#include <limits>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n;
    cin >> n;

    vector<vector<long long>> partitions = {};


    long long f;
    cin >> f;
    vector<long long> fV = { f };
    partitions.push_back(fV);

    int m = 1;

    for (int i = 1; i < n; i++) {
        long long e;
        cin >> e;
        if (partitions[m - 1][0] == e) {
            partitions[m - 1].push_back(e);
        }
        else {
            vector<long long> eV = { e };
            partitions.push_back(eV);
            m++;
        }
    }

    int stepsDone = 0;
    long long result = 0;

    for (int i = m - 1; i >= 0; i--) {
        int thisLen = 0;
        for (int j : partitions[i]) {
            if (j - stepsDone < 0) {
                continue;
            }
            result += j - stepsDone;
            thisLen++;
        }
        stepsDone += thisLen;
    }

    printf("%lld", result);
    
}
