N = int(input())
l = 0
r = N-1
A = list(map(int, input().split()))
for i in range(N):
  A[i] = (A[i]*max(abs(i-r),abs(i-l)), i)
A.sort(key=lambda x:x[0], reverse=True)
ans = 0
for i in range(N):
  a, x = A[i]
  if abs(x-r)>abs(x-l):
    ans += abs(x-r)*a
    r -= 1
  else:
    ans += abs(x-l)*a
    l += 1
print(ans)