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

B = A
ans = 0
a = 0

for i in range(N-1):
      c = max(B)
      R = list(reversed(B))
      if (B.index(c)-(N-i)/2)*c > (R.index(c)-(N-i)/2)*c:
            if (N-i-B.index(c))>B.index(c):
                  ans+=(a+N-i-B.index(c))*c
            else:
                  ans+=B.index(c)*c
                  a+=1
            B.pop(B.index(c))
      else:
            if (N-i-R.index(c))>R.index(c):
                  ans+=(a+N-i-R.index(c))*c
            else:
                  ans+=R.index(c)*c
                  a+=1
            B.pop(N-i-R.index(c))

print(ans)