#P11096. [ROI 2022] 体育课 (Day 1)

    ID: 10708 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>贪心2022Special JudgeROI(俄罗斯)

[ROI 2022] 体育课 (Day 1)

Problem Description

Translated from ROI 2022 D1T1

Before the PE class, a class of nn students stands in a line. All students in the class have different heights. At position i(1in)i(1\le i\le n) in the line, there is a student of height pi(1pin)p_i(1\le p_i\le n).

At the beginning of the class, the PE teacher may want to change the order of the students in the line. To do this, he can perform the following operation once: choose a segment of the line from position ll to position rr (1lrn1 \le l \le r \le n), and sort the students in this segment in increasing order from left to right. For example, if n=5n = 5 and the initial order is [5,2,4,1,3][5, 2, 4, 1, 3], and the teacher chooses l=1,r=4l = 1,r = 4, then after sorting the order becomes [1,2,4,5,3][1, 2, 4, 5, 3].

The teacher wants to make two specific students as far apart from each other as possible. The distance between two students is the difference between the indices of the positions where they stand. For every pair of students, the teacher wants to know the maximum distance they can achieve after performing the sorting operation once. Please help the teacher compute the sum of these values.

If d(i,j)d(i, j) denotes the maximum distance that the teacher can achieve between the students who initially stand at positions ii and jj by choosing a segment and sorting it, you need to compute $\sum\limits_{i=1}^{n-1}\sum\limits_{j=i+1}^{n}d(i,j)$。

Input Format

The first line contains an integer nn, the number of students in the class (2n3,0002 \le n \le 3,000).

The second line contains nn integers p1,,pnp_1,\dots , p_n, representing the heights of the students in the line (1pin1 \le p_i \le n).

It is guaranteed that all pip_i are distinct.

Output Format

Output one integer, the answer to the problem, i.e., $\sum\limits_{i=1}^{n-1}\sum\limits_{j=i+1}^{n}d(i,j)$。

5
5 2 4 1 3
35
10
2 1 6 8 3 5 9 10 7 4
256
2
2 1
1

Hint

In Sample 1, the answer is the sum of the following numbers:

For example, to make the distance between the students who were initially at positions 44 and 55 with heights 11 and 33 equal to 44, the teacher can choose l=1,r=4l = 1,r = 4. Then the sequence changes from [5,2,4,1,3][\underline{5, 2, 4, 1}, 3] to [1,2,4,5,3][\underline{1, 2, 4, 5}, 3]. The chosen segment is underlined.

Subtask Score nn\le
11 1616 1010
22 2828 5050
33 1515 100100
44 2323 600600
55 1818 30003000

Translated by ChatGPT 5