N = int(input())
A = list(map(int,input().split()))
flag = [1 for i in range(N)]
left = 0
right = N - 1
ans = 0
for i in range(N):
    index = 0
    MAX = -1
    for j in range(N):
        if flag[j] and MAX < abs(A[j] * (abs(j - left) - abs(j - right))):
            MAX = abs(A[j] * (abs(j - left) - abs(j - right)))
            index = j
    flag[index] = 0
    ans += A[index] * max(abs(index - left), abs(index - right))
    if abs(index - left) > abs(index - right):
        left += 1
    else:
        right -= 1
print(ans)