N = int(input())
As = [int(s) for s in input().split()]

l = 0
r = N - 1

S = 0
for k in range(N):
    m, mp, il = 0, 0, False
    for p in range(N):
        v = As[p]

        tm = v * abs(l - p)
        if m < tm:
            m = tm
            mp = p
            il = True

        tm = v * abs(r - p)
        if m < tm:
            m = tm
            mp = p
            il = False

    S += m
    As[mp] = 0
    if il:
        l += 1
    else:
        r -= 1

print(S)
