Problem:
Score: 500 points

Problem Statement

Given is a permutation P of \{1, 2, \ldots, N\}.

For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.

Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Constraints

2 \le N \le 10^5

1 \le P_i \le N

P_i \neq P_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Sample Input 1

3
2 3 1

Sample Output 1

5

X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

30

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

136

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
        Scanner std = new Scanner(System.in);
        int count = std.nextInt();
        List<Integer> values = IntStream.range(0, count).map(it -> std.nextInt()).boxed().collect(Collectors.toList());
//        Set<Integer> set = new TreeSet<>();
        Deque<Integer> deque = new ArrayDeque<>();
        List<Integer> indexs = IntStream.range(0, count).boxed().collect(Collectors.toList());
        for (int i = 0; i < count; i++) {
            int value = values.get(i);
            indexs.set(value - 1, i);
        }
        long ans = 0;
        for (int x = count - 1; x >= 0; x--) {
            // x = value
            Integer index = indexs.get(x);


            int appearCount = 0;
//            Deque<Integer> lDeque = new ArrayDeque<>();
            int[] l = new int[2];
            Arrays.fill(l, -1);
            int[] r = new int[2];
            Arrays.fill(r, count);
//            Deque<Integer> rDeque = new ArrayDeque<>();
            {
                deque.add(index);
                deque = deque.stream()
                        .sorted()
                        .collect(Collectors.toCollection(ArrayDeque::new));
                {
                    Iterator it = deque.descendingIterator();
                    while (true) {
                        if (!it.hasNext() || it.next() == index) break;
                    }
                    for (int j = 0; j < 2; j++) {
                        if (!it.hasNext()) {
                            break;
                        }
                        Integer newIndex = (Integer) it.next();
                        l[j] = newIndex;
                    }
                }

                {
                    Iterator it = deque.iterator();
                    while (true) {
                        if (!it.hasNext() || it.next() == index) break;
                    }
                    for (int j = 0; j < 2; j++) {
                        if (!it.hasNext()) {
                            break;
                        }
                        r[j] = (int) it.next();
                    }
                }

                int[] ls = new int[2];
                int[] rs = new int[2];

                ls[0] = index - l[0];
                ls[1] = l[0] - l[1];

                rs[0] = r[0] - index;
                rs[1] = r[1] - r[0];
                appearCount = ls[0] * rs[1] + rs[0] * ls[1];
            }

            ans += appearCount * (x + 1);

        }

        System.out.println(ans);
    }
}