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++14 (GCC 5.4.1) (judge verdict: Accepted)


#include <numeric>
#include <iostream>
#include <sstream>
#include <vector>
#include <cstdio>
#include <map>
#include <set>
#include <functional>
#include <queue>
#include <string>
#include <bitset>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <set>
#include <algorithm>
#include <tuple>
#include <sstream>

using namespace std;

#ifdef LOCAL
#define logv(a) {cerr << #a << " = "; for (auto& e : a) {cerr << e << " ";} cerr << "\n";}
#define logvp(a) {cerr << #a << " = "; for (auto& e : a) {cerr << "(" << e.first << ", " << e.second << ") ";} cerr << "\n";}
#define logvv(a) {cerr << #a << " = \n"; for (auto& r : a) { for (auto& e : r) {cerr << e << " ";} cerr << "\n";} }
#define logvf(a, field) {cerr << #a"."#field << " = \n"; for (auto& e : a) { cerr << e.field << " ";} cerr << "\n"; }
#define logs(a) cerr << #a << " = " << (a) << "\n";
#define logss(a, b) cerr << #a << " = " << (a) << ", " << #b << " = " << (b) << "\n";
#define logp(a) cerr << #a << " = " << "(" << a.first << ", " << a.second << ")" << "\n";
#define cond(pred, stmt) if (pred) { stmt }
#else
#define logv(a)
#define logvp(a)
#define logvv(a)
#define logvf(a, field)
#define logs(a)
#define logss(a, b)
#define logp(a)
#define cond(pred, stmt)
#endif

#define forn(a, e) for (int a = 0; a < (int)e; a++)
#define forr(a, s, e) for (int a = s; a < (int)e; a++)

using i64 = long long;

template<typename T>
using Pair = array<T, 2>;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    i64 n;
    while (cin >> n) {
        vector<int> p(n);
        forn(i, n) {
            cin >> p[i];
        }
        vector<int> np(n, n), pp(n, -1);
        vector<int> nnp(n, n), ppp(n, -1);
        forn(i, n) {
            int cur = i - 1;
            while (cur != -1 && p[cur] <= p[i]) {
                cur = pp[cur];
            }
            pp[i] = cur;
            if (cur == -1) {
                ppp[i] = -1;
                continue;
            }
            cur--;
            while (cur != -1 && p[cur] <= p[i]) {
                cur = pp[cur];
            }
            ppp[i] = cur;
        }
        for (int i = n - 1; i >= 0; i--) {
            int cur = i + 1;
            while (cur != n && p[cur] <= p[i]) {
                cur = np[cur];
            }
            np[i] = cur;
            if (cur == n) {
                nnp[i] = n;
                continue;
            }
            cur++;
            while (cur != n && p[cur] <= p[i]) {
                cur = np[cur];
            }
            nnp[i] = cur;
        }

        logv(np);
        logv(nnp);
        logv(pp);
        logv(ppp);

        i64 ans = 0;
        forn(i, n) {
            i64 cr = (i - pp[i]) * (nnp[i] - np[i]);
            i64 cl = (np[i] - i) * (pp[i] - ppp[i]);
            ans += p[i] * (cr + cl);
        }
        cout << ans << endl;
    }
}