program Z5;

{$APPTYPE CONSOLE}

{%File 'luke.out'}
{%File 'luke.in'}

uses
  SysUtils,Math;

var
  r:Extended;
  t,h,i,j,n,c,kr:LongInt;
  next,k,value:array[0..100000] of LongInt;
procedure add(a:LongInt);
begin
  t:=t+1;
  next[t]:=h;
  h:=t;
  k[t]:=1;
  value[t]:=c;
end;


begin
  //Assign (Input,'luke.in');
  //Assign (Output,'luke.out');
  //Reset (Input);
  //Rewrite (Output);
  Read (n);
  t:=0;
  for i:=1 to n do
    begin
      read (c);
      j:=h;
      if (j = 0) or (value[h]<c) then
        add (c)
      else
        k[h]:=k[h]+1;
    end;
  j:=h;
  r:=0;
  kr:=0;
  value[0]:=0;
  while kr<value[j] do
    begin
      r:=r+(value[j]-kr)*k[j];//min(k[j],(value[j]-kr));
      kr:=kr+k[j];//min(k[j],(value[j]-kr));
      j:=next[j];
      //if value[j] < kr then
        //Break;
    end;
  write (r:0:0);
  //Close (Input);
  //Close (Output);
  { TODO -oUser -cConsole Main : Insert code here }
end.