from copy import deepcopy

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

index = [i for i in range(N)]
index = sorted(index, key=lambda i: A[i], reverse=True)

print(A)
print(index)


def LorR(index, res):
    i = index[0]
    dist = max(i, N - 1 - i)
    end = False
    cand1 = cand2 = 0
    while not end:
        j = i + dist
        if 0 <= j and j < N and not res[j]:
            r = deepcopy(res)
            r[j] = True
            cand1 = A[i] * dist + loop(index[1:], r)
            end = True

        j = i - dist
        if 0 <= j and j < N and not res[j]:
            r = deepcopy(res)
            r[j] = True
            cand2 = A[i] * dist + loop(index[1:], r)
            end = True

        dist -= 1
    return max(cand1, cand2)


def loop(index, res):
    if not index:
        return 0

    num = 0
    a = A[index[0]]
    while num < len(index) and A[index[num]] == a:
        num += 1

    cand = [0] * num
    for i in range(num):
        ind = deepcopy(index)
        head = ind.pop(i)
        ind.insert(0, head)
        cand[i] = LorR(ind, res)

    return max(cand)


print(loop(index, [False] * N))
