N = int(input())
*A, = map(int, input().split())

AL = [0]*N
used = [False]*N

l = 0
r = N-1
ans = 0
for _ in range(N):
    maxp = 0
    for i, a in enumerate(A):
        if used[i]:
            continue
        p = max(i-l, r-i)*a
        if p >= maxp:
            if p > maxp:
                maxp = p
                ml = []
                mr = []

            if i-l >= r-i:
                ml.append((i-l, i, l))
            elif i-l <= r-i:
                mr.append((r-i, i, r))

    if len(ml) == 1:
        d, i, to = ml[0]
    elif len(mr) == 1:
        d, i, to = mr[0]
    else:
        d, i, to = sorted(ml+mr)[0]

    ans += d*A[i]
    AL[to] = A[i]
    used[i] = True
    if to == l:
        l += 1
    else:
        r -= 1

print(ans)

