CF2178C.First or Second

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn children standing in a line, where the niceness of the ii-th child is aia_i. Santa is deciding which children belong on the nice list and which belong on the naughty list.

An integer XX is initially set to 00. Santa will perform the following operation exactly n−1n-1 times:

  • Choose the first or second child in line and remove them from the line.
  • Let ww be the niceness of the chosen child.
    • If the first child was chosen, add them to the nice list and add ww to XX.
    • If the second child was chosen, add them to the naughty list and subtract ww from XX.

Note that after all operations, exactly one child remains unassigned to a list.

Determine the maximum possible value of XX that Santa can obtain after all n−1n-1 operations.

有 nn 个孩子站成一排,其中第 ii 个孩子的“善良值”为 aia_i。圣诞老人需要决定哪些孩子列入“善良名单”,哪些列入“顽皮名单”。

初始时,整数 XX 设为 00。圣诞老人将恰好执行以下操作 n−1n-1 次:

  • 选择队列中第一个或第二个孩子,并将其从队列中移除;
  • 设被选中孩子的善良值为 ww:
    • 若选择的是第一个孩子,则将其加入善良名单,并将 ww 加到 XX 上;
    • 若选择的是第二个孩子,则将其加入顽皮名单,并将 ww 从 XX 中减去。

注意:经过全部 n−1n-1 次操作后,恰好剩下一个孩子未被分配到任一名单中。

求圣诞老人在完成全部 n−1n-1 次操作后所能得到的 XX 的最大可能值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052\le n\le 2\cdot 10^5) — the number of children.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (−109≤ai≤109-10^9\le a_i\le 10^9) — the niceness of each child.

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(2≤n≤2⋅1052\le n\le 2\cdot 10^5)——儿童的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(−109≤ai≤109-10^9\le a_i\le 10^9)——每个儿童的“善良值”。

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

输出格式

For each test case, print a single integer — the maximum possible value of XX that Santa can obtain after all n−1n-1 operations.

对于每个测试用例,输出一个整数——圣诞老人在执行完全部 n−1n-1 次操作后所能得到的最大可能的 XX 值。

输入输出样例

  • 输入#1

    7
    2
    2 -3
    4
    1 4 3 4
    4
    -4 2 3 -6
    5
    -2 -3 4 10 -9
    5
    -12345678 -1000000000 -999999999 1000000000 -999999999
    2
    -7 1
    5
    7 -6 -1 -8 -8

    输出#1

    3
    8
    4
    15
    2987654321
    -1
    29

说明/提示

In the first test case, Santa will perform exactly one operation. If he chooses the first child, he will add a1=2a_1=2 to XX to get X=2X=2; if he chooses the second child, he will subtract a2=−3a_2=-3 from XX to get X=3X=3. Thus, the answer is 33.

In the second test case, it is optimal to select the first child in all three operations. The value of XX will be 1+4+3=81+4+3=8.

In the third test case, below is an optimal sequence of operations:

Type

Niceness of remaining children

XX after operation

0

—

[−4,2,3,−6][-4, 2, 3, -6]

00

1

First

[2,3,−6][2, 3, -6]

−4-4

2

First

[3,−6][3, -6]

−2-2

3

Second

[3][3]

44

In the fourth test case, below is an optimal sequence of operations:

Type

Niceness of remaining children

XX after operation

0

—

[−2,−3,4,10,−9][-2, -3, 4, 10, -9]

00

1

Second

[−2,4,10,−9][-2, 4, 10, -9]

33

2

First

[4,10,−9][4, 10, -9]

11

3

First

[10,−9][10, -9]

55

4

First

[−9][-9]

1515

在第一个测试用例中,圣诞老人将恰好执行一次操作。如果他选择第一个孩子,则将 a1=2a_1=2 加到 XX 上,得到 X=2X=2;如果他选择第二个孩子,则从 XX 中减去 a2=−3a_2=-3,得到 X=3X=3。因此,答案为 33。

在第二个测试用例中,最优策略是在全部三次操作中均选择第一个孩子。此时 XX 的值为 1+4+3=81+4+3=8。

在第三个测试用例中,以下是一个最优的操作序列:

操作类型

剩余孩子的“善良值”

操作后的 XX

0

—

[−4,2,3,−6][-4, 2, 3, -6]

00

1

第一种

[2,3,−6][2, 3, -6]

−4-4

2

第一种

[3,−6][3, -6]

−2-2

3

第二种

[3][3]

44

在第四个测试用例中,以下是一个最优的操作序列:

操作类型

剩余孩子的“善良值”

操作后的 XX

0

—

[−2,−3,4,10,−9][-2, -3, 4, 10, -9]

00

1

第二种

[−2,4,10,−9][-2, 4, 10, -9]

33

2

第一种

[4,10,−9][4, 10, -9]

11

3

第一种

[10,−9][10, -9]

55

4

第一种

[−9][-9]

1515

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

首页