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

D = [[0] * 2 for _ in range(N)]

for i in range(N):
  D[i] = [i, A[i]]

D.sort(key = lambda x: x[1], reverse = True)

ans = 0
l = 0
r = 0
for i in D:
  t, k = i
  na = (t - l) * k
  su = (N - t - r - 1) * k
  A[t] = 0
  ln = 0
  rn = 0
  for j in range(t):
    ln += A[j]
  for j in range(t, N):
    rn += A[t]
  if na + ln > su + rn:
    ans += na
    l += 1
  else:
    ans += su
    r += 1

print(ans)