from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

ans = 0
def main():
    global ans
    n = int(input())
    a = list(map(int, input().split()))
    csum = [0] * (n + 1)
    for i in range(n):
        csum[i+1] = csum[i] + a[i]
    d = 0
    for i in range(1, n):
        if ans < csum[i] * (n - i) + (csum[-1] - csum[i]) * i:
            d = i
            ans = csum[i] * (n - i) + (csum[-1] - csum[i]) * i
    def bsort(A):
        global ans
        l = len(A)
        for i in range(l - 1):
            for j in range(l - 1 - i):
                if A[j] > A[j+1]:
                    ans += A[j] - A[j-1]
                    A[j], A[j-1] = A[j-1], A[j]
    bsort(a[:d])
    bsort(list(reversed(a[d:])))
    print(ans)
main()