from heapq import heappop, heappush


def solve(n, a_list):
    # from left to right
    h_left = []
    res_left = [0] * n
    for i in range(n):
        r = 0
        for j in range(i):
            heappush(h_left, (-a_list[j], j))
        for k in range(i):
            a, j = heappop(h_left)
            r += (-a) * abs(n - 1 - j - k)
        assert len(h_left) == 0
        res_left[i] = r
    # print(res_left)

    # from right to left
    h_right = []
    res_right = [0] * n
    for i in range(n):
        r = 0
        for j in range(i, n):
            heappush(h_right, (-a_list[j], j))
        for k in range(n - i):
            a, j = heappop(h_right)
            r += (-a) * abs(j - k)
        res_right[i] = r
    # print(res_right)

    res = max([res_left[i] + res_right[i] for i in range(n)])
    return res


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    res = solve(n, a_list)
    print(res)


def test():
    assert solve(4, [1, 3, 4, 2]) == 20
    assert solve(6, [5, 5, 6, 1, 1, 1]) == 58
    assert solve(6, [8, 6, 9, 1, 2, 1]) == 85
    # print(solve(2000, [0] * 2000))


if __name__ == "__main__":
    test()
    main()
