CF2237A.Destroying Towers

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Quack the Duck has returned to his homeland and found nn towers standing in a line. The height of the ii-th tower is aia_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 will operate on each tower exactly once, in any order he chooses. The operation on tower ii is as follows:

  • Quack climbs to the top of tower ii and shoots a laser to the right, cutting the first taller tower it hits down to the same height as tower ii.

    Formally, let jj be the smallest index such that j>ij \gt i and aj>aia_j \gt a_i, where aia_i and aja_j are the current heights of the towers. If such jj exists, then aja_j is replaced with aia_i. Otherwise, nothing happens.

Find the minimum possible final sum of tower heights over all possible orders of operations.

鸭子奎克回到了他的故乡,发现有 nn 座塔排成一列。第 ii 座塔的高度为 aia_i。他决心为自身生态环境所遭受的破坏复仇,并发誓要用他的激光枪制造尽可能大的混乱。

奎克将恰好对每座塔执行一次操作,且操作顺序可由他任意选择。对第 ii 座塔的操作如下:

  • 奎克爬上第 ii 座塔的顶端,向右发射一道激光,将第一个被击中的、比当前塔更高的塔削至与第 ii 座塔等高。

    形式化地,令 jj 为满足 j>ij > i 且 aj>aia_j > a_i 的最小下标(其中 aia_i 和 aja_j 表示各塔当前的高度)。若这样的 jj 存在,则将 aja_j 替换为 aia_i;否则,不进行任何操作。

求在所有可能的操作顺序下,塔的最终高度之和的最小可能值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1001\le n\le 100) — the number of towers.

The following line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤10001\le a_i\le 1000) — the heights of the towers.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001\le n\le 100)—— 塔的数量。

接下来的一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤10001\le a_i\le 1000)—— 各塔的高度。

输出格式

For each test case, output a single integer — the minimum possible final sum of tower heights over all possible orders of operations.

对于每个测试用例,输出一个整数——在所有可能的操作顺序下,塔高度最终总和的最小可能值。

输入输出样例

  • 输入#1

    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

    输出#1

    3
    12
    8
    5
    8
    8
    7
    11
    4
    22

说明/提示

In the first test case, one optimal order is 3,1,23,1,2. The heights change as follows:

\[1,3,5\]\\to \[1,3,5\]\\to \[1,1,5\]\\to \[1,1,1\].

Thus the final sum is 1+1+1=31+1+1=3.

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][5,4,3], and the answer is 5+4+3=125+4+3=12.

In the third test case, one optimal order is 4,1,3,24,1,3,2. The heights change as follows:

\[3,2,5,1\]\\to \[3,2,5,1\]\\to \[3,2,3,1\]\\to \[3,2,3,1\]\\to \[3,2,2,1\].

Therefore the final sum is 3+2+2+1=83+2+2+1=8.

在第一个测试用例中,一种最优的操作顺序是 3,1,23,1,2。塔的高度变化如下:

\[1,3,5\]\\to \[1,3,5\]\\to \[1,1,5\]\\to \[1,1,1\].

因此最终的和为 1+1+1=31+1+1=3。

在第二个测试用例中,没有任何操作能改变任意一座塔。对于每座塔,其右侧均不存在更高的塔。因此最终高度仍为 [5,4,3][5,4,3],答案为 5+4+3=125+4+3=12。

在第三个测试用例中,一种最优的操作顺序是 4,1,3,24,1,3,2。塔的高度变化如下:

\[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+2+1=83+2+2+1=8。

输入解题思路,AI测评打分。不知道怎么写?

首页