#P11096. [ROI 2022] 体育课 (Day 1)
[ROI 2022] 体育课 (Day 1)
Problem Description
Translated from ROI 2022 D1T1。
Before the PE class, a class of students stands in a line. All students in the class have different heights. At position in the line, there is a student of height .
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 to position (), and sort the students in this segment in increasing order from left to right. For example, if and the initial order is , and the teacher chooses , then after sorting the order becomes .
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 denotes the maximum distance that the teacher can achieve between the students who initially stand at positions and 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 , the number of students in the class ().
The second line contains integers , representing the heights of the students in the line ().
It is guaranteed that all 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 and with heights and equal to , the teacher can choose . Then the sequence changes from to . The chosen segment is underlined.
| Subtask | Score | |
|---|---|---|
Translated by ChatGPT 5