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


def f(v):
    sorted_v = sorted(v)
    c = Counter(v)
    happy = 0
    for key in c.keys():
        v_idx = np.where(v == key)[0]
        sorted_v_idx = np.where(sorted_v == key)[0]
        move_idx = sorted_v_idx - v_idx + (N - len(v))
        happy += move_idx.sum() * key
    return happy


ans = 0
for i in range(1, N):
    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)
