N=int(input())
A=[]
for i,a in enumerate(map(int,input().split())):
  A.append([a,i])
A=sorted(A,key=lambda x:x[0], reverse=True)
r,l=0,N-1
ans=0
for a,i in A:
  if (l+r)//2<=i:
    ans+=a*(i-r)
    r+=1
  else:
    ans+=a*(l-i)
    l-=1
print(ans)