CF2185B.Prefix Max

入门

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array of nn integers a1,a2,…,ana_1, a_2, \ldots, a_n.

The value of an array is the sum of the maximums of each prefix of the array. More formally, the value of an array aa is ∑i=1nmax⁡(a1,…,ai)\sum_{i=1} ^{n} \operatorname{max}(a_1, \ldots, a_i). For example, the value of the array [1,2,11, 2, 1] is max⁡(1)+max⁡(1,2)+max⁡(1,2,1)=1+2+2=5\operatorname{max}(1) + \operatorname{max}(1, 2) + \operatorname{max}(1, 2, 1) = 1 + 2 + 2 = 5.

You can choose two indices ii and jj and swap elements aia_i and aja_j; this operation can be applied at most one time.

Find the maximum possible value of the array aa after at most one operation.

给你一个包含 nn 个整数的数组 a1,a2,…,ana_1, a_2, \ldots, a_n。

数组的值定义为该数组每个前缀的最大值之和。更准确地说,数组 aa 的值为 ∑i=1nmax⁡(a1,…,ai)\sum_{i=1} ^{n} \operatorname{max}(a_1, \ldots, a_i)。例如,数组 [1,2,1][1, 2, 1] 的值为 max⁡(1)+max⁡(1,2)+max⁡(1,2,1)=1+2+2=5\operatorname{max}(1) + \operatorname{max}(1, 2) + \operatorname{max}(1, 2, 1) = 1 + 2 + 2 = 5。

你可以选择两个下标 ii 和 jj,并交换元素 aia_i 与 aja_j;该操作最多可执行一次。

求在至多执行一次该操作后,数组 aa 可能达到的最大值。

输入格式

The first line of the input contains a single integer tt (1≤t≤1001 \leq t \leq 100) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤502 \le n \le 50) — the length of the array aa.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1041 \le a_i \le 10^4) — the array aa.

输入的第一行包含一个整数 tt(1≤t≤1001 \leq t \leq 100)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤502 \le n \le 50)—— 表示数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1041 \le a_i \le 10^4)—— 表示数组 aa。

输出格式

For each test case, output the maximum possible value of the array aa after the swap has been performed.

对于每个测试用例,输出执行交换操作后数组 aa 的最大可能值。

输入输出样例

  • 输入#1

    4
    5
    2 1 4 5 3
    2
    5 1
    3
    3 2 1
    2
    6 7

    输出#1

    25
    10
    9
    14

说明/提示

For the first test case, we can swap a1a_1 with a4a_4 to get the array [5,1,4,2,35, 1, 4, 2, 3], which has a value of max⁡(5)+max⁡(5,1)+max⁡(5,1,4)+max⁡(5,1,4,2)+max⁡(5,1,4,2,3)=25\operatorname{max}(5) + \operatorname{max}(5, 1) + \operatorname{max}(5, 1, 4) + \operatorname{max}(5, 1, 4, 2) + \operatorname{max}(5, 1, 4, 2, 3) = 25.

For the second test case, the current value of the array is max⁡(5)+max⁡(5,1)=10\operatorname{max}(5) + \operatorname{max}(5, 1) = 10. If we were to swap a1a_1 and a2a_2, aa would be equal to [1,51, 5], which has a value of max⁡(1)+max⁡(1,5)=6\operatorname{max}(1) + \operatorname{max}(1, 5) = 6, meaning the best option is to not perform a swap.

对于第一个测试用例,我们可以交换 a1a_1 与 a4a_4,得到数组 [5,1,4,2,35, 1, 4, 2, 3],其值为 max⁡(5)+max⁡(5,1)+max⁡(5,1,4)+max⁡(5,1,4,2)+max⁡(5,1,4,2,3)=25\operatorname{max}(5) + \operatorname{max}(5, 1) + \operatorname{max}(5, 1, 4) + \operatorname{max}(5, 1, 4, 2) + \operatorname{max}(5, 1, 4, 2, 3) = 25。

对于第二个测试用例,当前数组的值为 max⁡(5)+max⁡(5,1)=10\operatorname{max}(5) + \operatorname{max}(5, 1) = 10。若我们交换 a1a_1 与 a2a_2,则 aa 将变为 [1,51, 5],其值为 max⁡(1)+max⁡(1,5)=6\operatorname{max}(1) + \operatorname{max}(1, 5) = 6,因此最优选择是不执行任何交换。

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

首页