

# coding: utf-8
DATA = """
5
1 2 9 3 4
""".split("\n")

##
# 

def get_debug_line():
	for i in DATA:
		yield i

if 1:
	get_line = input
else:
	get_line = get_debug_line().__next__
	get_line()

N = int(get_line().strip())

Memo = [0] * (N)
A = [int(i) for i in get_line().strip().split(" ")]

for i in range(len(A)):
    Memo[i] = (A[i], i)

def VV (s, e):
    def V(l):
        v, index = l
        if index - s < e - index:
            return v * (e - index)
        else:
            return v * (index - s)
    return V

s = 0
e = N-1

ret = 0
while(len(Memo) > 0):
    Memo.sort(key = VV(s, e))
    pop = Memo.pop()
    #print (pop, s, e)
    if pop[1] - s == e - pop[1]:
        if len(Memo) > 0:
            pop2 = Memo[-1]
            if pop2[1] - s > e - pop2[1]:
                ret += pop[0] * (e - pop[1])
                e -= 1
            else:
                ret += pop[0] * (pop[1] - s)
                s += 1
    elif pop[1] - s < e - pop[1]:
        ret += pop[0] * (e - pop[1])
        e -= 1
    else:
        ret += pop[0] * (pop[1] - s)
        s += 1
print (ret)
