type Tmas = array[1..200000] of longint;
function noteq(var a,b:Tmas):boolean;
var i:longint; var res:boolean;
begin
  res := false;
  i := 1;
  while not(res) and (i<=200000) do
  begin
    if a[i] <> b[i] then res := true;
    inc(i);
  end;
  noteq := res;
end;
function maxid(var m:Tmas; n:longint):longint;
var i,etalon:longint;
begin
  etalon:=1;
  for i:= 2 to n do
    if m[i] > m[etalon] then etalon := i;
  maxid := etalon;
end;
procedure shleft(var m:Tmas; pos,len:longint; var n:longint);
var i:longint;
begin
  for i := (pos+1) to (pos + len - 1) do m[i] := 0;
  for i := (pos + len) to n do
  begin
    m[i-len+1] := m[i];
    m[i] := 0;
  end;
  n := n + 1 - len;
end;
procedure merge(var m:Tmas; var n:longint);
var etalon,count,pos,i:longint; ok,br:boolean;
begin
  etalon := m[1];
  count:=1;
  pos := 1;
  ok := false;
  i:=2;
  br := true;
  while (i <= n) do
  begin
    if (m[i] = etalon) and (i <> n) then
      inc(count)
    else
    begin
      if (i = n) and (m[i] = etalon) then inc(count);
      ok := true;
      if (i+1) <= n then
      begin
        etalon := m[i+1];
        pos := i+1;
        count := 0;
      end;
    end;
    if (count > 1) and ok then shleft(m,pos,count,n);
    inc(i);
  end;
end;
var n,i:longint; mas,masnull:Tmas; counter:longword;
begin
  read(n);
  counter := 0;
  for i := 1 to n do
    read(mas[i]);
  fillchar(masnull,length(masnull),0);
  while noteq(mas,masnull) do
  begin
    merge(mas,n);
    i := maxid(mas,n);
    dec(mas[i]);
    inc(counter);
  end;
  writeln(counter);
end.
