N = int(input())
A = list(map(int, input().split(" ")))
A_before_old = []
for i in range(N):
    A_before_old.append((A[i], i))
A_before = sorted(A_before_old, reverse=True)
A_after = [-1] * N
s = 0
deg = [(0, 0)] * N
l = 0
while l < N:
    calc_list = []
    while True:
        calc_list.append(A_before[l])
        if l + 1 >= N: break
        if A_before[l][0] != A_before[l + 1][0]: break
        l += 1
    if calc_list[0][1] < N // 2:
        calc_list = sorted(calc_list)
    for i in range(len(calc_list)):
        diff = max(calc_list[i][1], N - calc_list[i][1] + 1)
        ok = 0
        for j in range(N):
            for k in [1, -1]:
                offset = (diff - j) * k
                idx = calc_list[i][1] + offset
                if idx < 0 : continue
                if idx >= N : continue
                if A_after[idx] == -1:
                    deg[idx] = calc_list[i]
                    A_after[idx] = calc_list[i][0]
                    s += calc_list[i][0] * abs(offset)
                    ok = 1
                if ok == 1: break
            if ok == 1: break
    l += 1
print(s)