def maximumIndices(liste):
  maxi = liste[0]
  indice = 0
  longueur=len(liste)
  for i in range(longueur):
    if liste[i] >= maxi:
      maxi = liste[i]
      indice = i
  return indice

N = int(input())
activity = input().split(" ")
for i in range(N):
  activity[i]=int(activity[i])
bornesup = N-1
borneinf = 0
total = 0
while borneinf<bornesup:
  indice = maximumIndices(activity)
  distsup = bornesup-indice
  if indice<distsup:
    total+=activity[indice]*distsup
    bornesup-=1
  else:
    total+=activity[indice]*indice
    borneinf+=1
  activity[indice]=0
print(total)
