program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var i,j,k,l,m,n,r,s : uInt64;
a : array [0..100000] of longint;
begin
Read(n);
For i:=1 to n do
Read(a[i]);
s:=0;
For i:=n downto 1 do
begin
If a[i]>0 then
begin
r:=a[i];
s:=s+a[i];
a[i]:=0;
end;
For j:=i-1 downto 1 do
If a[j]<>r then
a[j]:=a[j]-1;
end;
Write(s);
end.
