n = int(input())
a = list(map(int,input().split()))
ans = 0
p = []
for i in range(n):
    p.append([a[i],i])
lef = 0
rig = n-1
p.sort(reverse=True)

v = 0
for i in range(n//2):
    ans += max(p[2*i][0]*abs(p[2*i][1]-(lef+i)) + p[2*i+1][0]*abs(p[2*i+1][1]-(rig-i)),
    p[2*i][0]*abs(p[2*i][1]-(rig-i)) + p[2*i+1][0]*abs(p[2*i+1][1]-(lef+i)))
if n % 2 ==1:
    ans += p[n-1][0]*abs(p[n-1][1]-(-(-n//2)))

print(ans)