N=int(input())
A=list(map(int,input().split()))
dp=[i for i in range(N)]#移し替えた後の並び
dp2=[i for i in range(N)]#i番めの人の行く先

for i in range(1,N):
  max=0
  maxj=i
  for j in range(i):
    if A[i]*abs(i-dp2[j])+A[j]*abs(j-dp2[i])-A[i]*abs(i-dp2[i])-A[j]*abs(j-dp2[j])>max:
      max=A[i]*abs(i-dp2[j])+A[j]*abs(j-dp2[i])-A[i]*abs(i-dp2[i])-A[j]*abs(j-dp2[j])
      maxj=j
  z=dp[maxj]
  dp[maxj]=dp[i]
  dp[i]=z
  z1=dp2.index(i)
  z2=dp2.index(maxj)
  z=dp2[dp[maxj]]
  dp2[dp[maxj]]=dp2[dp[i]]
  dp2[dp[i]]=dp2[dp[maxj]]

ans=0    
for i in range(N):
  ans+=abs(i-dp2[i])*A[i]
print(ans)