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 = abs(t - l) * k
  su = abs(N - t - r - 1) * k
  A[t] = 0
  if l > t:
    ans += su
    r += 1
    continue
  if N - r - 1 < t:
    ans += na
    l += 1
    continue

  ln = 0
  rn = 0
  for j in range(N):
    ln += abs(N - r - j - 1) * A[j]
    rn += abs(l - j) * A[j]

  if na + ln > su + rn:
    ans += na
    l += 1
  else:
    ans += su
    r += 1

print(ans)