n=int(input())
a=list(map(int,input().split()))
a=[[a[i],i]for i in range(n)]
c=0
for i in range(n):
    for j in range(n-1,i,-1):
        if abs(a[i][1]-i)*a[i][0]+abs(a[j][1]-j)*a[j][0]<abs(a[i][1]-j)*a[i][0]+abs(a[j][1]-i)*a[j][0]:
            c+=abs(a[i][1]-j)*a[i][0]+abs(a[j][1]-i)*a[j][0]-abs(a[i][1]-i)*a[i][0]-abs(a[j][1]-j)*a[j][0]
            a[i],a[j]=a[j],a[i]
print(c)