CF1903C.Theofanis' Nightmare

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Theofanis easily gets obsessed with problems before going to sleep and often has nightmares about them. To deal with his obsession he visited his doctor, Dr. Emix.

In his latest nightmare, he has an array aa of size nn and wants to divide it into non-empty subarrays†^{\dagger} such that every element is in exactly one of the subarrays.

For example, the array [1,−3,7,−6,2,5][1,-3,7,-6,2,5] can be divided to [1][−3,7][−6,2][5][1] [-3,7] [-6,2] [5].

The Cypriot value of such division is equal to Σi=1ki⋅sumi\Sigma_{i=1}^{k} i \cdot \mathrm{sum}_i where kk is the number of subarrays that we divided the array into and sumi\mathrm{sum}_i is the sum of the ii-th subarray.

The Cypriot value of this division of the array [1][−3,7][−6,2][5]=1⋅1+2⋅(−3+7)+3⋅(−6+2)+4⋅5=17[1] [-3,7] [-6,2] [5] = 1 \cdot 1 + 2 \cdot (-3 + 7) + 3 \cdot (-6 + 2) + 4 \cdot 5 = 17.

Theofanis is wondering what is the maximum Cypriot value of any division of the array.

†^{\dagger} An array bb is a subarray of an array aa if bb can be obtained from aa by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

西奥法尼斯在睡觉前很容易对问题着迷,常常因此做噩梦。为了应对这种痴迷,他去看了他的医生——埃米克斯博士。

在他最近的一场噩梦中,他拥有一个长度为 nn 的数组 aa,并希望将其划分为若干非空子数组†^{\dagger},使得每个元素恰好属于其中一个子数组。

例如,数组 [1,−3,7,−6,2,5][1,-3,7,-6,2,5] 可被划分为 [1] [−3,7] [−6,2] [5][1]\ [-3,7]\ [-6,2]\ [5]。

该划分的**塞浦路斯值(Cypriot value)**定义为 Σi=1ki⋅sumi\Sigma_{i=1}^{k} i \cdot \mathrm{sum}_i,其中 kk 是划分所得子数组的个数,sumi\mathrm{sum}_i 是第 ii 个子数组的元素和。

上述划分 [1] [−3,7] [−6,2] [5][1]\ [-3,7]\ [-6,2]\ [5] 的塞浦路斯值为
1⋅1+2⋅(−3+7)+3⋅(−6+2)+4⋅5=171 \cdot 1 + 2 \cdot (-3 + 7) + 3 \cdot (-6 + 2) + 4 \cdot 5 = 17。

西奥法尼斯想知道:该数组所有可能划分方式中,最大的塞浦路斯值是多少?

†^{\dagger} 若数组 bb 可通过对数组 aa 删除开头若干(可能为零或全部)元素及结尾若干(可能为零或全部)元素而得到,则称 bb 是 aa 的一个子数组。特别地,任意数组是其自身的子数组。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Each test case consists of two lines.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^{5}) — the size of the array.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−108≤ai≤108-10^8 \le a_i \le 10^{8}) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^{5}.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例由两行组成。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^{5})—— 数组的大小。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−108≤ai≤108-10^8 \le a_i \le 10^{8})—— 数组的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^{5}。

输出格式

For each test case, print one integer — the maximum Cypriot value of the array aa.

对于每个测试用例,输出一个整数——数组 aa 的最大塞浦路斯值。

输入输出样例

  • 输入#1

    4
    6
    1 -3 7 -6 2 5
    4
    2 9 -5 -3
    8
    -3 -4 2 -5 1 10 17 23
    1
    830

    输出#1

    32
    4
    343
    830

说明/提示

In the first test case, to get the maximum Cypriot value we divide the array into [1][−3][7][−6][2][5][1][-3][7][-6][2][5] which gives us: Σi=1ki⋅sumi=1⋅1+2⋅(−3)+3⋅7+4⋅(−6)+5⋅2+6⋅5=32\Sigma_{i=1}^{k} i \cdot \mathrm{sum}_i = 1 \cdot 1 + 2 \cdot (-3) + 3 \cdot 7 + 4 \cdot (-6) + 5 \cdot 2 + 6 \cdot 5 = 32

Similarly, in the second test case we divide the array into [2][9,−5,−3][2][9,-5,-3] which gives us Σi=1ki⋅sumi=1⋅2+2⋅(9+(−5)+(−3))=4\Sigma_{i=1}^{k} i \cdot \mathrm{sum}_i = 1 \cdot 2 + 2 \cdot (9 + (-5) + (-3)) = 4.

在第一个测试用例中,为获得最大的塞浦路斯值,我们将数组划分为 [1][−3][7][−6][2][5][1][-3][7][-6][2][5],从而得到:Σi=1ki⋅sumi=1⋅1+2⋅(−3)+3⋅7+4⋅(−6)+5⋅2+6⋅5=32\Sigma_{i=1}^{k} i \cdot \mathrm{sum}_i = 1 \cdot 1 + 2 \cdot (-3) + 3 \cdot 7 + 4 \cdot (-6) + 5 \cdot 2 + 6 \cdot 5 = 32。

类似地,在第二个测试用例中,我们将数组划分为 [2][9,−5,−3][2][9,-5,-3],从而得到 Σi=1ki⋅sumi=1⋅2+2⋅(9+(−5)+(−3))=4\Sigma_{i=1}^{k} i \cdot \mathrm{sum}_i = 1 \cdot 2 + 2 \cdot (9 + (-5) + (-3)) = 4。

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

首页