from collections import Counter
N = int(input())
A = list(map(int, input().split()))


def f(v):
    sorted_v = sorted(v)
    c = Counter(v)
    happy = 0
    for key in c.keys():
        v_idx = [n for n, i in enumerate(v) if i == key]
        sorted_v_idx = [n for n, i in enumerate(sorted_v) if i == key]
        move_idx = 0
        for i, j in zip(v_idx, sorted_v_idx):
            move_idx += j - i
        move_idx += (N - len(v)) * len(v_idx)
        happy += move_idx * key
    return happy


ans = 0
for i in range(N // 2 - 1, N // 2 + 1):
    l = A[:i]
    r = A[i:][::-1]
    # print('l, r', l, r, f(l), f(r))
    ans = max(ans, f(l) + f(r))
print(ans)
