package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	t, _ := strconv.Atoi(sc.Text())
	return t
}

type ch struct {
	loc  int
	act  int
	maxh int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	N := nextInt(sc)
	child := make([]ch, N)
	for i := 0; i < N; i++ {
		child[i].loc = i
		child[i].act = nextInt(sc)
		child[i].maxh = max(child[i].act*i, child[i].act*(N-1-i))
	}
	childcopy := make([]ch, N)
	childnew := make([]ch, N)
	copy(childcopy, child)
	sort.SliceStable(child, func(i, j int) bool {
		return child[i].maxh > child[j].maxh
	})
	ans := 0
	for i := 0; i < N; i++ {
		// 移動後の嬉しさの最大値の最も高い子から埋めていく
		newloc := N + 1
		maxh := -1
		for j := 0; j < N; j++ {
			if childnew[j].act != 0 {
				continue
			}
			tmph := child[i].act * abs(child[i].loc-j)
			if tmph > maxh {
				maxh = tmph
				newloc = j
			}
		}
		// 先頭からみて最も変更後の嬉しさが高いところにつめる
		childnew[newloc].act = child[i].act
		childnew[newloc].loc = newloc
		ans += maxh
	}
	fmt.Println(ans)
}
func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
