import sys

def calc(A, j, L, R):
    a, i = A[j]
    J = j + 1
    if L == R:
        s = a * abs(L - i)
    else:
        s = a * abs(L - i) + calc(A, J, L + 1, R)
        t = a * abs(R - i) + calc(A, J, L, R - 1)
        if s < t:
            s = t
    return s


def main():
    N = int(input())

    sys.setrecursionlimit(N + 10)

    A = []
    for i, a in enumerate(map(int, input().split(' '))):
        A.append((a, i))
    A.sort(reverse=True)

    print(calc(A, 0, 0, N - 1))


if __name__ == '__main__':
    main()
