import numpy as np
n=int(input())
A=list(map(int,input().split()))
index=np.argsort(A)[::-1]
under=0
top=n-1
point=[0]*n
for i in range(n-1):
	j=index[i]
	if top-j>j-under:
		point[j]=top
		top-=1
	elif top-j<j-under:
		point[j]=under
		under+=1
	else:
		if j>index[i+1]:
			point[j]=under
			under+=1
		else:
			point[j]=top
			top-=1
point[index[-1]]=top
ans=0
for i in range(n):
	ans+=A[i]*abs(point[i]-i)
print(ans)
