摧毁高塔AI (CI翻译)
Quack the Duck has returned to his homeland and found $n$ towers standing in a line. The height of the $i$-th tower is $a_i$. He wants vengeance for the destruction of his ecosystem, and has vowed to wreak as much havoc as possible with his laser gun.
鸭子 Quack 回到了自己的家乡,发现有 $n$ 座塔排成一列。第 $i$ 座塔的高度为 $a_i$。为了报复生态系统被摧毁的仇恨,他发誓要用自己的激光枪制造尽可能大的破坏。AI (CI翻译)
Quack will operate on each tower exactly once, in any order he chooses. The operation on tower $i$ is as follows:
Quack 将按照自己选择的任意顺序,对每座塔恰好操作一次。对第 $i$ 座塔的操作如下:AI (CI翻译)
- Quack climbs to the top of tower
$i$and shoots a laser to the right, cutting the first taller tower it hits down to the same height as tower$i$.Formally, let$j$be the smallest index such that$j \gt i$and$a_j \gt a_i$, where$a_i$and$a_j$are the current heights of the towers. If such$j$exists, then$a_j$is replaced with$a_i$. Otherwise, nothing happens.
- Quack 爬到第
$i$座塔的顶部,向右发射激光,将它击中的第一座更高的塔削到与第$i$座塔相同的高度。形式化地,设$j$是满足$j \gt i$且$a_j \gt a_i$的最小下标,其中$a_i$和$a_j$表示当前塔的高度。如果这样的$j$存在,则将$a_j$替换为$a_i$。否则不进行任何操作。
Find the minimum possible final sum of tower heights over all possible orders of operations.
求在所有可能的操作顺序中,最终塔高总和的最小值。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 500$)。接下来给出各个测试用例的描述。AI (CI翻译)
The first line of each test case contains an integer $n$ ($1\le n\le 100$) — the number of towers.
每个测试用例的第一行包含一个整数 $n$($1\le n\le 100$)——塔的数量。AI (CI翻译)
The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1\le a_i\le 1000$) — the heights of the towers.
下一行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($1\le a_i\le 1000$)——各座塔的高度。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output a single integer — the minimum possible final sum of tower heights over all possible orders of operations.
对于每个测试用例,输出一个整数——在所有可能的操作顺序中,最终塔高总和的最小值。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, one optimal order is $3,1,2$. The heights change as follows:
在第一个测试用例中,一种最优操作顺序是 $3,1,2$。塔高变化如下:AI (CI翻译)
$$ [1,3,5]\to [1,3,5]\to [1,1,5]\to [1,1,1]. $$
$$ [1,3,5]\to [1,3,5]\to [1,1,5]\to [1,1,1]. $$AI (CI翻译)
Thus the final sum is $1+1+1=3$.
因此,最终总和为 $1+1+1=3$。AI (CI翻译)
In the second test case, no operation can change any tower. For every tower, there is no higher tower to its right. Therefore the final heights remain $[5,4,3]$, and the answer is $5+4+3=12$.
在第二个测试用例中,没有任何操作可以改变塔高。对于每座塔,其右侧都不存在更高的塔。因此,最终塔高仍为 $[5,4,3]$,答案为 $5+4+3=12$。AI (CI翻译)
In the third test case, one optimal order is $4,1,3,2$. The heights change as follows:
在第三个测试用例中,一种最优操作顺序是 $4,1,3,2$。塔高变化如下:AI (CI翻译)
$$ [3,2,5,1]\to [3,2,5,1]\to [3,2,3,1]\to [3,2,3,1]\to [3,2,2,1]. $$
$$ [3,2,5,1]\to [3,2,5,1]\to [3,2,3,1]\to [3,2,3,1]\to [3,2,2,1]. $$AI (CI翻译)
Therefore the final sum is $3+2+2+1=8$.
因此,最终总和为 $3+2+2+1=8$。AI (CI翻译)
样例
10
3
1 3 5
3
5 4 3
4
3 2 5 1
4
2 1 4 3
5
4 1 3 5 2
5
2 2 3 1 4
1
7
6
6 1 5 2 4 3
4
1 1 1 1
5
10 3 8 6 9
3
12
8
5
8
8
7
11
4
22
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。