#!python3
import sys
sys.setrecursionlimit(1000000000)
from functools import lru_cache

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    A = list(enumerate(iim()))

    A.sort(key=lambda x: x[1], reverse=True)

    @lru_cache
    def rec(i, j1, j2):
        if i == N:
            return 0
        pos, ai = A[i]

        ax = abs(pos - j1) * ai + rec(i+1, j1+1, j2)
        bx = abs(pos - j2) * ai + rec(i+1, j1, j2-1)

        return max(ax, bx)

    print(rec(0, 0, N-1))

if __name__ == "__main__":
    resolve()
