#P12675. 「LAOI-8」Boundary
「LAOI-8」Boundary
Description
You are given a permutation of length , meaning that contains all positive integers from to exactly once. You can perform the following two operations:
- Increase by , at a cost of .
- Select an interval such that and , and set all elements in that interval to , at a cost equal to the length of the interval.

Note: Operation two can still be applied even if .
Your task is to turn all elements in the array into with the minimum total cost.
Input Format
This problem contains multiple test cases.
The first line contains an integer , the number of test cases.
For each test case:
- The first line contains an integer , representing the length of the permutation .
- The second line contains integers, representing the permutation .
Output Format
Output lines in total.
Each line contains a single integer — the minimum cost for that test case.
2
3
1 3 2
9
1 2 3 4 5 6 7 8 9
4
13
Hint
Explanation
For the first test case:
- Increase by : cost .
- Apply operation 2 on interval : cost .
Total cost: (this is optimal).
For the second test case:
- Increase and by : cost .
- Apply operation 2 on intervals and : cost .
- Apply operation 2 on interval : cost .
Total cost: (this is optimal).
Constraints
Subtasks are used in this problem.
| Subtask | Special Properties | Score | |
|---|---|---|---|
| None | |||
| is strictly increasing | |||
| None | |||
For all test cases, it is guaranteed that , , .
京公网安备 11011102002149号