def f(a,n,r):
    if n==0:
        return 0
    if a[n-1]-r<=0:
        return 0
    else:
        i=n-1
        while a[i]==a[n-1]:
            i-=1
        i+=1
        u=n-i


       
        return f(a,n-u,r+u)+ (a[n-1]-r)*u
    
        
        
    


n=int(input())
a=list(map(int,input().split()))

print(f(a,n,0))
