import numpy as np

N = int(input())
s = list(map(int, input().split()))
index = list(range(1,N+1))

s = np.array(s)
index = np.array(index)

left = 1
right = N
ans = 0

while left<=right:
    larray = s*abs(index-left)
    rarray = s*abs(index-right)
    lmax = np.argmax(larray)
    rmax = np.argmax(rarray)

    if larray[lmax] > rarray[rmax]:
        ans += larray[lmax]
        s[lmax] = 0
        left += 1
        #print(s, left, right)
    else:
        ans += rarray[rmax]
        s[rmax] = 0
        right -= 1
        #print(s, left, right)

print(ans)
