package main

import (
	"fmt"
	"sort"
)

type As [][]int

func (a As) Len() int {
	return len(a)
}

func (a As) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func (a As) Less(i, j int) bool {
	return a[i][0] > a[j][0]
}

func main() {
	var n int
	fmt.Scan(&n)
	as := make(As, n)
	for i := range as {
		var a int
		fmt.Scan(&a)
		as[i] = []int{a, i}
	}
	sort.Sort(as)
	var sum int
	l := 0
	r := n - 1
	for i := range as {
		a, p := as[i][0], as[i][1]
		if abs(p-l) > abs(r-p) {
			sum += a * abs(p-l)
			//fmt.Println("[l] a,p,sum,p-l,r-p: ", a, p, sum, abs(p-l), abs(r-p))
			l += 1
		} else {
			sum += a * abs(r-p)
			//fmt.Println("[r] a,p,sum,p-l,r-p: ", a, p, sum, abs(p-l), abs(r-p))
			r -= 1
		}
	}
	fmt.Println(sum)
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
