#! /usr/bin/env python3

import copy

N = int(input())
A = [int(_) for _ in input().split()]

ret = 0
r = N
l = 1

B = sorted(A)

for i in range(N):
    if B == []:
        break
    _ = B[-1]
    idxs = [i+1 for i, value in enumerate(A) if value == _]

    for idx in idxs[::-1]:
        x = idx
        if abs(x - r)  > abs(x - l):
            ret += _ * abs(x-r)
            print(_, _ * abs(x-r), ret)
            r -= 1
        else:
            ret += _ * abs(x-l)
            print(_, _ * abs(x-l), ret)
            l += 1
        B.pop(-1)

print(ret)