from collections import deque

n=int(input())
a=list(map(int,input().split()))
gg=a.copy()
tmp=a.copy()
tmp.sort()

ans=deque([tmp[0]])

for i in range(1,n):
    if a.index(tmp[i])<=n//2-1:
        ans.append(tmp[i])
        a[a.index(tmp[i])]+=float('inf')
    else:
        ans.appendleft(tmp[i])
        a[a.index(tmp[i])] += float('inf')

ans=list(ans)

cnt=0
for i in range(n):
    y=gg.index(ans[i])
    cnt+=abs(y-i)*ans[i]
    gg[y]+=float('inf')
if n%4!=0:
    cnt+=1
print(cnt)