import numpy as np
N = int(input())
activeness = np.array(input().split(), dtype=np.int64)

left = 0
right = N - 1
total = 0

while activeness.any():
    happiness_left = activeness * np.abs(np.arange(N) - left)
    left_argmax = happiness_left.argmax()
    left_max = happiness_left[left_argmax]

    happiness_right = activeness * np.abs(np.arange(N) - right)
    right_argmax = happiness_right.argmax()
    right_max = happiness_right[right_argmax]

    if left == right and left_max == right_max:
        total += left_max
        break

    if left_max > right_max:
        total += left_max
        left += 1
        activeness[left_argmax] = 0
    else:
        total += right_max
        right -= 1
        activeness[right_argmax] = 0

print(total)