package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scan(&n)
	var a int
	as := make([]Item, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		as[i] = Item{i + 1, a}
	}

	sort.Slice(as, func(i, j int) bool {
		ai := as[i]
		aj := as[j]
		iv := max(ai.value*(ai.index-1), ai.value*(n-ai.index))
		jv := max(aj.value*(aj.index-1), aj.value*(n-aj.index))
		if iv == jv {
			return ai.value < aj.value
		}
		return iv < jv
	})

	ans := 0
	l, r := 1, n
	for i := n - 1; i >= 0; i-- {
		a := as[i]
		lv := a.value * (a.index - l)
		rv := a.value * (r - a.index)
		if lv == rv && i > 0 {
			b := as[i-1]
			lb := b.value * (b.index - l)
			rb := b.value * (r - b.index)
			if lb < rb {
				rv = -1
			} else {
				lv = -1
			}
		}

		if lv < rv {
			r--
			ans += rv
		} else {
			l++
			ans += lv
		}
	}

	fmt.Println(ans)
}

// Item .
type Item struct {
	index, value int
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
