type tmas = array [1..200000] of longword;
procedure read_mas({var fin: text; }var m: tmas; var N: longword);
var i: longword;
begin
        read({fin,} N);
        for i:= 1 to N do
                read({fin,} m[i]);
end;

function zero_mas(m: tmas; N: longword): boolean;
var flag: boolean; i: longword;
begin
        flag:= true;
        for i:= 1 to N do
                if m[i] <> 0 then flag:= false;
        zero_mas:= flag;
end;

function count_steps(var m : tmas; N: longword): int64;
var max, i, left, right, res: longword;
begin
        res:= 0;
        while not(zero_mas(m, N)) do
        begin
                max:= 0;
                left:=  0;
                right:= 0;
                for i:= 1 to N do
                        if m[i] > max then max:= i;
                while (m[max - left - 1] = m[max]) and ((max - left - 1) >= 0) do
                        inc(left);
                while (m[max + right + 1] = m[max]) and ((max + right + 1) <= N) do
                        inc(right);
                for i:= max - left - 1 to max + right + 1 do
                        dec(m[i]);
                inc(res);
        end;
        count_steps:= res;
end;

var fin, fout: text; N: longword; m: tmas;
begin
       // assign(fin, 'input.txt'); reset(fin);
       // assign(fout, 'output.txt'); rewrite(fout);
        read_mas({fin,} m, N);
        writeln({fout,} count_steps(m, N));
       // close(fin); close(fout);
end.
