#include <iostream>
#include <algorithm>
using namespace std;

int arr[200000];
int sorted[200000];
int decrease;
int arrSize;
int offset;

int removeNum(int n) {
    int res = 0;

    int j = 0;
    for (int i = 0; i < arrSize; i++) {
        if (arr[i] == 0) {
                if (j > 0)
                    res +=  n;

                j = 0;
                continue;
        }

        arr[i] -= n;
        j++;

        if (arr[i] == 0) offset++;

        if (i == arrSize - 1) {
            if (j > 0)
                res +=  n;
            j = 0;
        }
    }

    decrease += n;
    return res;
}

int getFirstNotNullInSorted() {
   /* for (int i = 0; i < arrSize; i++) {
        if (sorted[i] > decrease) return sorted[i] - decrease;
    }*/

    if (offset < arrSize) return sorted[offset] - decrease;

    return -1;
}

int main() {
    cin >> arrSize;

    for (int i = 0; i < arrSize; i++) {
            cin >> arr[i];
            sorted[i] = arr[i];
    }

    offset = 0;
    decrease = 0;
    sort(sorted, sorted+arrSize);

    int res = 0;
    int n;
    while (true) {
        n = getFirstNotNullInSorted();
        if (n < 0) break;

        //cout << "got first not null: " << n << endl;

        int a = removeNum(n);
       // cout << "got res: " << a << endl;
        res += a;

        //cout << "==========" << endl;
    }

    cout << res;
}
