CF2185B.Prefix Max
入门
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array of n integers a1,a2,…,an.
The value of an array is the sum of the maximums of each prefix of the array. More formally, the value of an array a is ∑i=1nmax(a1,…,ai). For example, the value of the array [1,2,1] is max(1)+max(1,2)+max(1,2,1)=1+2+2=5.
You can choose two indices i and j and swap elements ai and aj; this operation can be applied at most one time.
Find the maximum possible value of the array a after at most one operation.
给你一个包含 n 个整数的数组 a1,a2,…,an。
数组的值定义为该数组每个前缀的最大值之和。更准确地说,数组 a 的值为 ∑i=1nmax(a1,…,ai)。例如,数组 [1,2,1] 的值为 max(1)+max(1,2)+max(1,2,1)=1+2+2=5。
你可以选择两个下标 i 和 j,并交换元素 ai 与 aj;该操作最多可执行一次。
求在至多执行一次该操作后,数组 a 可能达到的最大值。
输入格式
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n (2≤n≤50) — the length of the array a.
The second line contains n integers a1,a2,…,an (1≤ai≤104) — the array a.
输入的第一行包含一个整数 t(1≤t≤100)—— 表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤50)—— 表示数组 a 的长度。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤104)—— 表示数组 a。
输出格式
For each test case, output the maximum possible value of the array a after the swap has been performed.
对于每个测试用例,输出执行交换操作后数组 a 的最大可能值。
输入输出样例
输入#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 a1 with a4 to get the array [5,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.
For the second test case, the current value of the array is max(5)+max(5,1)=10. If we were to swap a1 and a2, a would be equal to [1,5], which has a value of max(1)+max(1,5)=6, meaning the best option is to not perform a swap.
对于第一个测试用例,我们可以交换 a1 与 a4,得到数组 [5,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。
对于第二个测试用例,当前数组的值为 max(5)+max(5,1)=10。若我们交换 a1 与 a2,则 a 将变为 [1,5],其值为 max(1)+max(1,5)=6,因此最优选择是不执行任何交换。
输入解题思路,AI测评打分。不知道怎么写?