package main

import (
	"fmt"
	"sort"
)

var N int
var A []int

func main() {
	fmt.Scan(&N)
	A = make([]int, N)
	for i := range A {
		fmt.Scan(&A[i])
	}
	xs := make([]int, N)
	for i := range xs {
		xs[i] = i
	}
	ans := -1
	for NextPermutation(sort.IntSlice(xs)) {
		val := 0
		for i := range xs {
			val += abs(xs[i]-i) * A[xs[i]]
		}
		ans = max(ans, val)
	}
	fmt.Println(ans)
}

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

func NextPermutation(x sort.Interface) bool {
	n := x.Len() - 1
	if n < 1 {
		return false
	}
	j := n - 1
	for ; !x.Less(j, j+1); j-- {
		if j == 0 {
			return false
		}
	}
	l := n
	for !x.Less(j, l) {
		l--
	}
	x.Swap(j, l)
	for k, l := j+1, n; k < l; {
		x.Swap(k, l)
		k++
		l--
	}
	return true
}
