package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func Scanner() string {
	sc.Scan()
	return sc.Text()
}

func intScanner() int {
	n, _ := strconv.Atoi(Scanner())
	return n
}

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, 1000000007)
	sc.Split(bufio.ScanWords)
	n := intScanner()
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, 2)
		a[i][1] = intScanner()
		a[i][0] = i
	}
	sort.Slice(a, func(i, j int) bool { return a[i][1] > a[j][1] })
	r := n - 1
	l := 0
	ans := 0
	for len(a) != 0 {
		mx := -1
		f := false
		midx := -1
		for idx, vs := range a {
			i, v := vs[0], vs[1]
			p := v * abs(r-i)
			q := v * abs(i-l)
			if p > q {
				if p > mx {
					f = true
					mx = p
					midx = idx
				}
			} else {
				if q > mx {
					f = false
					mx = q
					midx = idx
				}
			}
		}
		ans += mx
		if f {
			r--
		} else {
			l++
		}
		a = append(a[:midx], a[midx+1:]...)
	}
	fmt.Println(ans)
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}
