#P9485. 「LAOI-1」积水

「LAOI-1」积水

Problem Description

The ground surface is uneven. Whenever a heavy rain comes, puddles will form on the ground.
Another heavy rain is coming soon, and you decide to modify the ground to reduce water accumulation.

The ground can be modeled as a positive integer sequence aa of length nn, where aia_i represents the altitude at position ii.
When it rains, water gathers in low places and cannot flow away to either side. For example, for the sequence a=[3,1,5,1,2,3]a = [3,1,5,1,2,3], the accumulated water after raining is shown in the figure below (there are 55 cells of water):

Due to a lack of manpower, you only have time to change the altitude of exactly one position to any positive integer.

Ask: after the modification, what is the minimum number of water cells that can remain.

Input Format

The first line contains an integer TT.
Then there are TT test cases, each consisting of two lines.
For each test case, the first line contains an integer nn, the length of the sequence.
The second line contains nn integers, the sequence aa.

Output Format

For each test case, output one integer per line: the minimum number of water cells.

3
6
3 2 1 1 2 3
6
1 1 4 5 1 4
6
1 9 1 9 8 10
2
0
1

Hint

Constraints.

For the first 5%5\% of the testdata, 1n51 \le n \le 5.
For the first 20%20\% of the testdata, 1n5501 \le \sum n \le 550, 1ai5001 \le a_i \le 500.
For the first 35%35\% of the testdata, 1n50501 \le \sum n \le 5050, 1ai50001 \le a_i \le 5000.
For the first 50%50\% of the testdata, 1n50501 \le \sum n \le 5050, 1ai1091 \le a_i \le 10^9.
For 100%100\% of the testdata, 1n,n1061 \le n, \sum n \le 10^6, 1ai1091 \le a_i \le 10^9.

It is guaranteed that 1T1041 \le T \le 10^4.

Translated by ChatGPT 5