var n, i, k, k1:longint;
    a:array[1..100000]of longint;
    s:int64;

begin
  
  read(n);
  for i:=1 to n do read(a[i]);

  i:=0; s:=0; k:=0; k1:=1;
  while a[n-i]-k>0 do 
    begin
      s:=s+a[n-i]-k; //writeln(s,' ',k);
      if a[n-i]=a[n-i-1] then inc(k1)
        else begin k:=k+k1; k1:=0;  inc(k1); end;
      inc(i); 
    end;
  

  write(s);
  
end.
