#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    q = []
    for i in range(n):
        l = i
        r = n-i-1
        heappush(q,(-a[i]*max(l,r),l,r,a[i]))
    ans = 0
    while q:
        d,l,r,ai = heappop(q)
        d *= -1
        ans += d
        nq = []
        if l*ai == d:
            while q:
                d,l,r,ai = heappop(q)
                l -= 1
                d = -ai*max(l,r)
                heappush(nq,(d,l,r,ai))
        else:
            while q:
                d,l,r,ai = heappop(q)
                r -= 1
                d = -ai*max(l,r)
                heappush(nq,(d,l,r,ai))
        q = [i for i in nq]
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()
