from sys import stdin

N = int(stdin.readline().rstrip())
As = [int(x) for x in stdin.readline().rstrip().split()]

selected = []
selectedA = []


left_pos = 0
right_pos = N-1

happy_sum = 0
for i in range(N):

    max_val = 0 
    max_pos = 0
    left_sum = 0
    right_sum = 0
    move_val = 0

    for j in range(N):
        if j in selected:
            continue
        if max_val < As[j]:
            max_val = As[j]
            max_pos = j
            left_sum = sum(As[:j])
            right_sum = sum(As[j+1:])
#            move_val = max(left_sum, right_sum)
#        elif max_val == As[j]:
#            if max(move_val, )
    selected.append(max_pos)
    if left_sum >= right_sum:
        happy_sum += abs(left_pos - max_pos)*max_val
        left_pos+=1
    else:
        happy_sum += abs(right_pos - max_pos)*max_val
        right_pos-=1



       
#    selected.append(max_pos)
#    selectedA.append(max_val)
#    if abs(left_pos-max_pos) >= abs(right_pos -max_pos):
#        happy_sum += abs(left_pos-max_pos)*max_val
#        left_pos += 1
#    else:
#        happy_sum += abs(right_pos-max_pos)*max_val
#        right_pos -= 1
#    
#    if len(selected) == N:
#        break

print(happy_sum)


    

