PivotOJ

Min Max Subarrays

시간 제한: 3000ms메모리 제한: 2048MB출처: USACO 2025 February PlatinumBOJ 33738

문제

You are given a length-NN integer array a1,a2,,aNa_1,a_2,\dots,a_N (2N106,1aiN2\le N\le 10^6, 1\le a_i\le N). Output the sum of the answers for the subproblem below over all N(N+1)/2N(N+1)/2 contiguous subarrays of aa.

Given a nonempty list of integers, alternate the following operations (starting with the first operation) until the list has size exactly one.

  1. Replace two consecutive integers in the list with their minimum.
  2. Replace two consecutive integers in the list with their maximum.

Determine the maximum possible value of the final remaining integer.

For example,

[4, 10, 3] -> [4, 3] -> [4]
[3, 4, 10] -> [3, 10] -> [10]

In the first array, (10,3)(10, 3) is replaced by min(10,3)=3\min(10, 3)=3 and (4,3)(4, 3) is replaced by max(4,3)=4\max(4, 3)=4.

입력

The first line contains NN.

The second line contains a1,a2,,aNa_1,a_2,\dots,a_N.

출력

The sum of the answer to the subproblem over all subarrays.

예제

예제 1

입력
2
2 1
출력
4

예제 2

입력
3
3 1 3
출력
12

예제 3

입력
4
2 4 1 3
출력
22
코드를 제출하려면 로그인하세요.