CF1405B.Array Cancellation

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You're given an array aa of nn integers, such that a1+a2+⋯+an=0a_1 + a_2 + \cdots + a_n = 0.

In one operation, you can choose two different indices ii and jj (1≤i,j≤n1 \le i, j \le n), decrement aia_i by one and increment aja_j by one. If i<ji \lt j this operation is free, otherwise it costs one coin.

How many coins do you have to spend in order to make all elements equal to 00?

给你一个包含 nn 个整数的数组 aa,满足 a1+a2+⋯+an=0a_1 + a_2 + \cdots + a_n = 0。

一次操作中,你可以选择两个不同的下标 ii 和 jj(1≤i,j≤n1 \le i, j \le n),将 aia_i 减 11,同时将 aja_j 加 11。若 i<ji \lt j,该操作免费;否则(即 i>ji > j)需花费一枚硬币。

为使数组所有元素均变为 00,你至少需要花费多少枚硬币?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001 \le t \le 5000). Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1051 \le n \le 10^5) — the number of elements.

The next line contains nn integers a1,…,ana_1, \ldots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9). It is given that ∑i=1nai=0\sum_{i=1}^n a_i = 0.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

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

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

下一行包含 nn 个整数 a1,…,ana_1, \ldots, a_n(−109≤ai≤109-10^9 \le a_i \le 10^9)。已知 ∑i=1nai=0\sum_{i=1}^n a_i = 0。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, print the minimum number of coins we have to spend in order to make all elements equal to 00.

对于每个测试用例,输出使所有元素变为 00 所需花费的最少硬币数量。

输入输出样例

  • 输入#1

    7
    4
    -3 5 -3 1
    2
    1 -1
    4
    -3 2 -3 4
    4
    -1 1 1 -1
    7
    -5 7 -6 -4 17 -13 4
    6
    -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000
    1
    0

    输出#1

    3
    0
    4
    1
    8
    3000000000
    0

说明/提示

Possible strategy for the first test case:

  • Do (i=2,j=3)(i=2, j=3) three times (free), a=[−3,2,0,1]a = [-3, 2, 0, 1].
  • Do (i=2,j=1)(i=2, j=1) two times (pay two coins), a=[−1,0,0,1]a = [-1, 0, 0, 1].
  • Do (i=4,j=1)(i=4, j=1) one time (pay one coin), a=[0,0,0,0]a = [0, 0, 0, 0].

第一个测试用例的可能策略:

  • 执行 (i=2,j=3)(i=2, j=3) 三次(免费),a=[−3,2,0,1]a = [-3, 2, 0, 1]。
  • 执行 (i=2,j=1)(i=2, j=1) 两次(支付两枚硬币),a=[−1,0,0,1]a = [-1, 0, 0, 1]。
  • 执行 (i=4,j=1)(i=4, j=1) 一次(支付一枚硬币),a=[0,0,0,0]a = [0, 0, 0, 0]。

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

首页