N = int(input())
A = list(map(int, input().split()))

l = 0
r = N-1
res = 0
babies = {}
for i in range(N):
  babies[i] = {'incant': A[i], 'born': i}

while 0 < r-l:
  baby_info = max(babies.items(), key=lambda x:x[1]['incant'])
  index = baby_info[0]
  baby = baby_info[1]
  incant = baby['incant']
  if baby['born'] - l > r - baby['born']:
    next_pos = l
    l+=1
  else:
    next_pos = r
    r-=1

  res += incant * abs(baby['born']-next_pos)
  babies[index] = babies[next_pos]
  del babies[next_pos]

for i in babies:
  baby = babies[i]
  if 0 < baby['incant']:
    res += abs(i-baby['born']) * baby['incant']

print(res)