const processData = (lines) => {
  const nums = lines[1].split(' ').map(x => +x)
  const sortedClusters = nums.map((value, index) => ({value, index})).sort(
    (a, b) => b.value-a.value
  )
  let start = 0
  let end = nums.length - 1
  let i = 0
  let sum = 0
  while(start <= end) {
    const {value, index} = sortedClusters[i]
    i++
    const startDiff = Math.abs(start - index)
    const endDiff = Math.abs(end - index)
    if (startDiff > endDiff) {
      sum += value * startDiff
      start++
    } else {
      sum += value * endDiff
      end--
    }
  }
  console.log(sum)
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})
