program ab;
var
  m:array[1..2,0..100001] of int64;
  a,b,z,i,j,k,t,n,size,x,prev,s,start:int64;
begin
  readln(n);
  size:=0;
  prev:=0;
  s:=0;
  start:=1;
  for i:=1 to n do begin
    read(x);
    if x = prev then begin
      inc(m[2,size]);
      continue;
    end;
    prev:=x;
    inc(size);
    m[1,size]:=x;
    m[2,size]:=1;
  end;
  //for i:=1 to size do begin
  //  writeln(m[1,i],' | ',m[2,i]);
  //end;
  while true do begin
    s:=s+m[1,size];
    dec(m[2,size]);
    for i:=start to size-1 do dec(m[1,i]);
    if m[1,start] = 0 then inc(start);
    if m[2,size] = 0 then dec(size);
    //writeln('size = ',size);
    //for i:=start to size do begin
    //  writeln(m[1,i],' | ',m[2,i]);
    //end;
    if size = 0 then break;
  end;
  writeln(s);
end.