Problem:
Score : 400 points

Problem Statement

One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

Constraints

1 ≦ N ≦ 200,000

(a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Sample Input 1

3
2 1 3

Sample Output 1

9

Sample Input 2

4
1 3 2 4

Sample Output 2

19

Sample Input 3

8
5 4 8 1 2 6 7 3

Sample Output 3

85

Solution in Go (1.6) (judge verdict: Wrong Answer)

package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int64 {
	var i int64
	var e error
	for {
		sc.Scan()
		k := sc.Text()
		//re, _ := regexp.Compile(`\d{1,100}`)
		//t := re.FindAllStringSubmatch(k, -1)
		//k = t[0][0]
		//k = strings.TrimSpace(k)
		i, e = strconv.ParseInt(k, 10, 64)
		if e == nil {
			break
		}
	}
	return i
}
func bool2int(b bool) int {
	if b {
		return 1
	}
	return 0
}

func min(a int64, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func sum(a []int64) (ans int64) {
	n := int64(len(a))
	dpary := make([][]int64, 2)
	for i := range dpary {
		dpary[i] = make([]int64, n)
	}

	for i := int64(0); i < n; i++ {
		dpary[0][i] = a[i]
		//dpary[]ans = append(ans, b)
	}

	for i := int64(1); i < n; i++ {
		for j := int64(0); j < n-i; j++ {
			dpary[i%2][j] = min(dpary[(i-1)%2][j], dpary[(i-1)%2][j+1])
			ans += dpary[i%2][j]
		}
	}
	return
}

func late(dpary []int64, n int64) {
	ans := int64(0)
	sep := int64(0)
	for i := int64(0); i < n; i++ {
		b := nextInt()
		dpary[i] = b
		ans += b
		if b == 1 {
			sep = i
		}
		//dpary[]ans = append(ans, b)
	}
	ans += (n-sep)*sep*1 + sum(dpary[:sep]) + sum(dpary[sep:])
	/*
		for i := int64(1); i < n; i++ {
			for j := int64(0); j < n-i; j++ {
				dpary[i%2][j] = min(dpary[(i-1)%2][j], dpary[(i-1)%2][j+1])
				ans += dpary[i%2][j]
			}
		}
	*/
	fmt.Println(ans)
}

func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	//ans := make([]int64, n)
	ans := int64(0)
	//dary2 := make([]bool, h)
	//fmt.Println(dary2[0])
	//dpary := make([][]int64, 2)
	//for i := range dpary {
	dpary := make([]int64, n)
	//}

	//late(dpary, n)
	for i := int64(0); i < n; i++ {
		b := nextInt()
		dpary[i] = b
		//invary[b-1] = i
		ans += (i + 1) * (n - i)
		//dpary[]ans = append(ans, b)
	}
	tmp := ans
	head := int64(0)
	tail := n - 1
	invary := make([]int64, n)
	for i := int64(0); i < (n+1)/2; i++ {
		if dpary[head] < dpary[tail] {
			invary[i] = dpary[head]
			head += 1
		} else {
			invary[i] = dpary[tail]
			tail -= 1
		}
		//fmt.Println(invary[i], i+1, (n+1)/2*(invary[i]-i-1)+(invary[i]-i-1)-i-1)
		if invary[i] > i+1 {
			ans -= (n+1)/2*(invary[i]-i-1) + (invary[i] - i - 1) - i - 1
			//break
		}
	}
	//fmt.Println(invary)
	f := math.Pow(2, float64(n-2))
	e := int64(f) - 1
	if ans < tmp-e {
		ans = tmp - e
	}
	fmt.Println(ans)
}