import cython
import numpy as np
cimport numpy as cnp

n = int(input())
a = list(map(int, input().split()))

a = np.array(a, dtype=int)


def go(left, right, nm):
    if left > right:
        return 0
    i = np.argmax(nm*a)
    x = a[i]
    new_nm = nm.copy()
    new_nm[i] = False

    to_left = np.abs(i - left)
    to_right = np.abs(right - i)
    rtn_left = x * to_left + go(left+1, right, new_nm) 
    rtn_right = x * to_right + go(left, right-1, new_nm) 
    return max(rtn_left, rtn_right)

not_moved = np.ones_like(a)
rtn = go(0, n-1, not_moved)
print(rtn)
