

N = int(eval(input()))



A = list(map(int, input().split()))

AI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True)



def solve(a, i, prev):

  pl, pr, ps = i, 0, 0

  for l, r, s in prev:

    yield l, r-1, max(s+abs(r-i)*a, ps+abs(i-pl)*a)

    pl, pr, ps = l, r, s

  yield pl+1, pr, ps+abs(i-pl)*a



prev = [(1, N, 0)]

for a,i in AI:

  prev = [*solve(a,i, prev)]



print((max(s for l, r, s in prev)))

N = int(eval(input()))



def solve(a, i, prev):

  r = N - len(prev) - i + 1

  p = -i*a

  for j, s in enumerate(prev):

    yield max(p+abs(j-i)*a, s+abs(j+r)*a)

    p = s

  yield s+abs(len(prev)-i)*a



pd = [0]

A = list(map(int, input().split()))

for a,i in sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True):

  pd = [*solve(a,i, pd)]



print((max(pd)))

N = int(eval(input()))
A = list(map(int, input().split()))
AI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True)


def solve(a, i, prev):
    pl, pr, ps = i, 0, 0
    for l, r, s in prev:
        yield l, r - 1, max(s + abs(r - i) * a, ps + abs(i - pl) * a)
        pl, pr, ps = l, r, s
    yield pl + 1, pr, ps + abs(i - pl) * a


prev = [(1, N, 0)]
for a, i in AI:
    prev = [*solve(a, i, prev)]
print((max(s for l, r, s in prev)))

N = int(eval(input()))


def solve(a, i, prev):
    r = N - len(prev) - i + 1
    p = -i * a
    for j, s in enumerate(prev):
        yield max(p + abs(j - i) * a, s + abs(j + r) * a)
        p = s
    yield s + abs(len(prev) - i) * a


pd = [0]
A = list(map(int, input().split()))
for a, i in sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True):
    pd = [*solve(a, i, pd)]
print((max(pd)))

-A = list(map(int, input().split())) -AI = sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True) -    pl, pr, ps = i, 0, 0 -    for l, r, s in prev: -        yield l, r - 1, max(s + abs(r - i) * a, ps + abs(i - pl) * a) -        pl, pr, ps = l, r, s -    yield pl + 1, pr, ps + abs(i - pl) * a +    r = N - len(prev) - i + 1 +    p = -i * a +    for j, s in enumerate(prev): +        yield max(p + abs(j - i) * a, s + abs(j + r) * a) +        p = s +    yield s + abs(len(prev) - i) * a -prev = [(1, N, 0)] -for a, i in AI: -    prev = [*solve(a, i, prev)] -print((max(s for l, r, s in prev))) +pd = [0] +A = list(map(int, input().split())) +for a, i in sorted(((a, i) for i, a in enumerate(A, 1)), reverse=True): +    pd = [*solve(a, i, pd)] +print((max(pd)))