// @problemURL url.com
package main

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

func nt(s *bufio.Scanner) string {
	s.Scan()
	return s.Text()
}

func ni(s *bufio.Scanner) int {
	return atoi(nt(s))
}

func atoi(s string) int {
	i, err := strconv.Atoi(s)
	if err != nil {
		panic(err)
	}
	return i
}

type pair struct {
	x, y, z int
}

func main() {
	var in = bufio.NewReader(os.Stdin)
	s := bufio.NewScanner(in)
	s.Split(bufio.ScanWords)

	// Code
	n := ni(s)
	ar := make([]pair, n)
	for i := 0; i < n; i++ {
		ar[i] = pair{i + 1, ni(s), 0}
	}
	sort.Slice(ar, func(i, j int) bool {
		return ar[i].y < ar[j].y
	})

	i1, i2 := 0, n-1
	var sum int
	for i := n - 1; i >= 0; i-- {
		if abs(ar[i].x-i1) > abs(ar[i].x-i2) {
			ar[i].z = i1 + 1
			i1++
		} else {
			ar[i].z = i2 + 1
			i2--
		}

		sum += ar[i].y * abs(ar[i].x-ar[i].z)
	}

	fmt.Println(sum)
	// fmt.Println(ar)
}

func abs(i int) int {
	if i >= 0 {
		return i
	}
	return -i
}
