n = int(input())
a = list(map(int,input().split()))
raw = [i for i in range(n)]
for i in range(n):
  tmp = []
  for j in range(n):
    tmp.append(abs(raw[i]-j)*a[i] + abs(raw[j]-i)*a[j]
              -abs(raw[i]-i)*a[i] - abs(raw[j]-j)*a[j])
  maxi = max(tmp)
  #print(tmp)
  for j in range(n):
    if tmp[j] == maxi:
      raw[i],raw[j] = raw[j],raw[i]
      a[i],a[j] = a[j],a[i]
#print(raw)
#print(a)
ans = 0
for i in range(n):
  ans += a[i] * abs(i-raw[i])
print(ans)