n = int(input())
A = list(map(int, input().split()))
B = sorted(A, reverse=True)
x = 0
S = []
for i in range(n):
  S.append(i+1)

  T = []
for i in range(n):
  for j in range(n):
    if B[i]==A[j]:
      T.append(j+1)
      A[j] = 0

for i in range(n-1):
  a = S[0]
  b = S[-1]
  if T[i] > (a+b)/2:
    x += B[i]*(T[i]-a)
    S.remove(a)
  elif T[i] < (a+b)/2:
    x += B[i]*(b-T[i])
    S.remove(b)
  else:
    if T[i+1] > T[i]:
      x += B[i]*(b-T[i])
      S.remove(b)
    else:
      x += B[i]*(T[i]-a)
      S.remove(a)
y = x + B[-1]*abs(S[0]-T[-1])
print(y)