#include <iostream>
#include <stdio.h>
#include <set>
#include <vector>
#include <cmath>

using namespace std;

#define MAXN 100100

int n, k, res;
int N[MAXN];

int main(void)
{
    cin >> n;
    for (int i=0; i<n; i++) cin >> N[i];

    int maxi = n, MaxI; N[maxi] = -1;
    int f=0;
    while (N[maxi] != 0){
//        maxi = n;
        f = 0;
        for (int i=0; i<n; i++) // find maxi
            if (N[maxi] < N[i]) maxi = i;
        MaxI = N[maxi];

        for (int i=0; i<n; i++){
            if ((f == 1) && (N[i] != MaxI)) break;
            if (N[i] == MaxI){
                N[i]--;
                f = 1;
            }
        }
        res++;
    }

    cout << res << endl;

    return 0;
}
/*
#include <iostream>
#include <stdio.h>
#include <set>
#include <vector>
#include <cmath>

using namespace std;

#define MAXN 100100

struct segment
{
    int num, k, pr, nx;
};

int n, k, res, x;
segment N[MAXN];

int main(void)
{
    cin >> n;
    for (int i=0; i<n; i++){
        cin >> x; N[i].num = x; N[i].k = 1; N[i].nx = i+1;
        if (N[i-1].num == x){
            N[i-1].k++;
            N[i-1-N[i-1].k].nx++;
        }
    }

    int maxi = 0;
    while ((N[0].num != 0) && (N[0].k != n)){
        // find maxi
        for (int i=0; i<n; i+=N[i].k)
            if (N[maxi].num > N[i].num) maxi = i;

        N[maxi].num--; res++;
//        if ((0 < maxi) && ( N[maxi].nx  < (n-1))){
        if (N[maxi].num == N[ N[maxi].pr ].num)
            N[maxi].k += N[ N[maxi].pr ].k;
        if (N[maxi].num == N[ N[maxi].nx ].num)
            N[maxi].k += N[ N[maxi].nx ].k;
//        }
/*
        if ((maxi == (n-1)) && (N[maxi].num == N[ N[maxi].pr ].num))
            N[maxi].k += N[ N[maxi].pr ].k;
        if ((maxi == 0) && (N[maxi].num == N[ N[maxi].nx ].num))
            N[maxi].k += N[ N[maxi].nx ].k;*//*
    }

    cout << res << endl;

    return 0;
}
*/
