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 Julia (0.5.0) (judge verdict: Time Limit Exceeded)

isnothing(x) = x == nothing

typealias Maybe{T} Union{T, Void}

type Node
  value::Int
  count::Int
  left::Maybe{Node}
  right::Maybe{Node}

  Node(v::Int) = new(v, 1, nothing, nothing)
end

# binary search tree
type Tree
  root::Maybe{Node}
  first::Maybe{Node}
  last::Maybe{Node}

  Tree() = new(nothing, nothing, nothing)
end

Tree(itr) = union!(Tree(), itr)

# implement interfaces
# see https://docs.julialang.org/en/v0.5/manual/interfaces/

# iteration interface
Base.start(::Tree) = 1
Base.next(tree::Tree, state) = (tree[state], state + 1)
Base.done(tree::Tree, state) = state > endof(tree)
Base.iteratorsize(::Type{Tree}) = Base.HasLength()
Base.length(tree::Tree) = !isnothing(tree.root) ? tree.root.count : 0
Base.iteratoreltype(::Type{Tree}) = Base.HasEltype()
Base.eltype(::Type{Tree}) = Int

# indexing interface
# O(log(N))
function Base.getindex(tree::Tree, i::Int)::Int
  (i <= 0 || i > endof(tree)) && throw(BoundsError(tree, i))

  i == 1 && return first(tree)
  i == endof(tree) && return last(tree)

  node = tree.root
  while (l = !isnothing(node.left) ? node.left.count : 0; i != l + 1)
    # l = !isnothing(node.left) ? node.left.count : 0
    if i > l
      i -= l + 1
      node = node.right
    else
      node = node.left
    end
  end

  node.value
end

Base.endof(tree::Tree) = length(tree)

# efficient operations for iterable
Base.in(val::Int, tree::Tree) = find(val, tree) != 0

# stack operations
function Base.push!(tree::Tree, val::Int)::Tree
  isnothing(tree.root) && (tree.root = tree.first = tree.last = Node(val))

  node = tree.root
  nodes = [node]
  while true
    if node.value > val && !isnothing(node.left)
      node = node.left
    elseif node.value < val && !isnothing(node.right)
      node = node.right
    else
      break
    end
    push!(nodes, node)
  end

  newnode = Node(val)
  if node.value > val
    node.left = newnode
    foreach(node -> node.count += 1, nodes)
    val < tree.first.value && (tree.first = newnode)
  elseif node.value < val
    node.right = newnode
    foreach(node -> node.count += 1, nodes)
    val > tree.last.value && (tree.last = newnode)
  end

  tree
end

function Base.push!(tree::Tree, vals...)::Tree
  foreach(val -> push!(tree, val), vals)
  tree
end

function Base.pop!(tree::Tree)::Int
  val = last(tree)
  delete!(tree, val)
  val
end

function Base.shift!(tree::Tree)::Int
  val = first(tree)
  delete!(tree, val)
  val
end

Base.find(val::Int, tree::Tree)::Int = first(_find(val, tree))

Base.isempty(tree::Tree) = length(tree) == 0
Base.empty!(tree::Tree) = (tree.root = tree.first = tree.last = nothing)
Base.minimum(tree::Tree) = first(tree)
Base.maximum(tree::Tree) = last(tree)
Base.extrema(tree::Tree) = (minimum(tree), maximum(tree))
Base.first(t::Tree) = t.first.value
Base.last(t::Tree) = t.last.value

function Base.union!(tree::Tree, itr)::Tree
  foreach(val -> push!(tree, val), itr)
  tree
end

# O(Nlog(N))
function shake!(tree::Tree)::Tree
  sorted = collect(tree)
  empty!(tree)

  f(a::Vector{Int}) = begin
    isempty(a) && return

    i = length(a) ÷ 2 + 1
    push!(tree, a[i])
    f(a[1:i - 1])
    f(a[i + 1:end])
  end
  f(sorted)

  tree
end

function _find(val::Int, tree::Tree)::Tuple{Int, Maybe{Node}}
  isnothing(tree.root) && return (0, nothing)

  node = tree.root
  i = 0
  while true
    if node.value > val && !isnothing(node.left)
      node = node.left
    elseif node.value < val && !isnothing(node.right)
      i += node.count - node.right.count
      node = node.right
    else
      break
    end
  end
  i += node.count - (!isnothing(node.right) ? node.right.count : 0)

  node.value == val ? (i, node) : (0, nothing)
end

# end of bst.jl

parseInt(x) = parse(Int, x)
parseArray(x::Array{SubString{String}, 1}) = map(parseInt, x)

function main()
  n = readline() |> parseInt
  px = readline() |> split |> parseArray

  ix=zeros(Int,n)
  ix[px]=1:n
  tree=Tree()

  ans=0
  for p in n:-1:1
    i=ix[p]
    push!(tree,i)
    log2(length(tree))%1==0 && shake!(tree)
    j = find(i, tree)

    y,z=map(k -> j+k<=endof(tree) ? tree[j+k] : n+1, 1:2)
    x,w=map(k -> j-k>=1 ? tree[j-k] : 0, 1:2)
    ans+=p*((x-w)*(y-i)+(i-x)*(z-y))
  end

  println(ans)
end

main()