e=enumerate

n,a=open(0)

n=int(n)

d=[0]+[1e18]*n

for j,(a,i)in e(sorted((-int(a),i)for i,a in e(a.split()))):d=[min(t+a*abs(~i-j+k+n),d[k-1]+a*abs(~i+k))for k,t in e(d)]

print((-min(d)))
n=int(eval(input()))

d=[0]+[-10**18]*n

for j,(a,i)in enumerate(sorted((-a,i)for i,a in enumerate(map(int,input().split())))):

    d=[max(t-a*abs(~i-j+k+n),d[k-1]-a*abs(~i+k))for k,t in enumerate(d)]

print((max(d)))
e = enumerate
n, a = open(0)
n = int(n)
d = [0] + [1e18] * n
for j, (a, i) in e(sorted((-int(a), i) for i, a in e(a.split()))):
    d = [min(t + a * abs(~i - j + k + n), d[k - 1] + a * abs(~i + k)) for k, t in e(d)]
print((-min(d)))

n = int(eval(input()))
d = [0] + [-(10**18)] * n
for j, (a, i) in enumerate(
    sorted((-a, i) for i, a in enumerate(map(int, input().split())))
):
    d = [
        max(t - a * abs(~i - j + k + n), d[k - 1] - a * abs(~i + k))
        for k, t in enumerate(d)
    ]
print((max(d)))

-e = enumerate -n, a = open(0) -n = int(n) -d = [0] + [1e18] * n -for j, (a, i) in e(sorted((-int(a), i) for i, a in e(a.split()))): -    d = [min(t + a * abs(~i - j + k + n), d[k - 1] + a * abs(~i + k)) for k, t in e(d)] -print((-min(d))) +n = int(eval(input())) +d = [0] + [-(10**18)] * n +for j, (a, i) in enumerate( +    sorted((-a, i) for i, a in enumerate(map(int, input().split()))) +): +    d = [ +        max(t - a * abs(~i - j + k + n), d[k - 1] - a * abs(~i + k)) +        for k, t in enumerate(d) +    ] +print((max(d)))