def f(a,n,r):
    if n==0:
        return 0
    if a[n-1]-r<=0:
        return 0
    else:
        o=0
        for i in range(n-1,-1,-1):
            if (a[i]==a[n-1]) and (o==0):
                m=i
            else: o=1
        u=n-m
       
        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))
