#include <iostream>
#include <math.h>
#include "stdfix.h"
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int movenum = 0;
    vector<int> largestnums;
    int largest = 1;
    int maxrow = 0;
    int row = 0;
    int maxrowstart;
    int rowstart = 0;
    int inputnum;
    int N;
    vector<int> numlist;

    cin >> N;
    if(N <= 100000 && N >= 1) {
        for(int i = 0; i < N; i++) {
            cin >> inputnum;
            numlist.push_back(inputnum);
        }
        while(largest > 0) {
            largest = 0;
            maxrow = 0;
            row = 0;
            maxrowstart = 0;
            rowstart = 0;
            for(int i = 0; i < numlist.size(); i++) {
                if(numlist[i] > largest) {
                    largest = numlist[i];
                }
            }
            for(int i = 0; i < numlist.size(); i++){
                if(numlist[i] == largest){
                    row += 1;
                    if(row > maxrow) {
                        maxrowstart = rowstart;
                        maxrow = row;
                    }
                }
                if(numlist[i] != largest){
                    row = 0;
                    if(i < numlist.size() - 1){
                        rowstart = i+1;
                    }

                }

            }
            for(int i = maxrowstart; i < maxrowstart + maxrow; i++) {
                numlist[i]--;
            }
            movenum++;
        }
        cout << movenum - 1;
    }
    return 0;
}
