import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))

C = np.zeros([N, N], dtype=np.int32)

C[0] = np.arange(N)

for i in range(1, N):
    C[i] = C[i - 1] + 1
    C[i] -= 2

for i in range(N):
    C[i] *= A[i]
C = np.abs(C)

left = 0
right = N - 1

cnt = 0
while left < right:
    a = np.argmax(C[:, left])
    b = np.argmax(C[:, right])

    if C[a, left] > C[b, right]:
        cnt += C[a, left]
        C[a] = 0
        C[:, left] = 0
        left += 1
    elif C[a, left] < C[b, right]:
        cnt += C[b, right]
        C[b] = 0
        C[:, right] = 0
        right -= 1
    elif C[a, left] == C[b, right]:
        if A[a] > A[b]:
            cnt += C[b, right]
            C[b] = 0
            C[:, right] = 0
            right -= 1
        else:
            cnt += C[a, left]
            C[a] = 0
            C[:, left] = 0
            left += 1
            

cnt += np.max(C)

print(cnt)
