import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    used = [False] * N

    first = 0
    back = N - 1
    ans = 0
    for _ in range(N):
        maxValue = -1
        toMove = -1
        for i in range(N):
            if not used[i]:
                if maxValue < A[i] * max(abs(i - first), abs(i - back)):
                    maxValue = A[i] * max(abs(i - first), abs(i - back))
                    toMove = i
        ans += maxValue
        used[toMove] = True            
        if abs(toMove - first) >= abs(toMove - back): first += 1
        else: back -= 1
        
        #print(toMove, ans)
    print(ans)

    return 0

if __name__ == "__main__":
    solve()