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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace ABC140E
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());

            List<int> P = new List<int>();
            P.AddRange(ReadInt());

            int[] numToIndex = new int[P.Count + 1];
            for (int i = 0; i < P.Count; i++)
            {
                numToIndex[P[i]] = i + 1;
            }

            // 見終わったものの座標のリスト　昇順 <index, value>
            SortedList<int, int> finished = new SortedList<int, int>();
            finished.Add(-1, -1);
            finished.Add(0, 0);
            finished.Add(N + 1, N + 1);
            finished.Add(N + 2, N + 2);

            //大きいものから入れていく 今探しているナンバーはi
            long ans = 0;
            for (int i = N; i > 0; i--)
            {
                int ind = numToIndex[i];

                int R1ind = LowerBound(finished.Keys, ind);
                int R1 = finished.Keys[R1ind];
                int R2 = finished.Keys[R1ind + 1]; if (R2 == N + 2) R2 = N + 1;
                int L1 = finished.Keys[R1ind - 1];
                int L2 = finished.Keys[R1ind - 2]; if (L2 == -1) L2 = 0;

                long c1 = (long)i * (R1 - ind) * (L1 - L2);
                long c2 = (long)i * (ind - L1) * (R2 - R1);
                ans += c1 + c2;

                finished.Add(ind, i);
            }

            Console.WriteLine(ans);
            Console.ReadKey();
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }

        /// <summary>
        /// 二分探索 key以上の最低
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="a"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static int LowerBound<T>(IList<T> a, T v)
        {
            return LowerBound(a, v, Comparer<T>.Default);
        }

        public static int LowerBound<T>(IList<T> a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Count - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res == -1) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }
    }

}