program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var i,j,k,l,m,n,r,s : LongInt;
a,kor,kol : array [0..100000] of longint;
begin
Read(n);
j:=1;
FillChar(kol,SizeOf(kol),0);
For i:=1 to n do
Read(a[i]);
For i:=1 to n-1 do
begin
kor[j]:=a[i];
if a[i]<>a[i+1] then
j:=j+1
else
kol[j]:=kol[j]+1;
end;
r:=j;
For i:=1 to r do
kol[i]:=kol[i]+1;
s:=0;
l:=0;
repeat
s:=s+kol[r]*(kor[r]-l);
l:=l+kol[r];
r:=r-1;
until l>=kor[r];
Write(s);
end.
