n = int(input())
a = list(map(int,input().split()))
d = []
for i in range(n):
  d.append([a[i],i+1])
d.sort(reverse = True)
count = 0
left = 1
right = n
m = 0
h = 0
for i in range(n):
  k = d[i][0]
  v = d[i][1]  
  if (v-left) < (right-v):
    count += k*(right-v)
    right -= 1
  elif (v-left) == (right-v):
      if m>=h:
          count += k*(v-left)
          left += 1
      else:
          count += k*(right-v)
          right -= 1
  else:
    count += k*(v-left)
    left += 1
  if v-1 > n//2:
      m += 1
  else:
      h += 1
print(count)