欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1933A.Turtle Puzzle: Rearrange and Negate
Turtle Puzzle: Rearrange and Negate
You are given an array of integers. You must perform the following two operations on the array (the first, then the second):
- Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged.
- Choose at most one contiguous segment of elements and replace the signs of all elements in this segment with their opposites. Formally, you can choose a pair of indices such that and assign for all (negate elements). Note that you may choose not to select a pair of indices and leave all the signs of the elements unchanged.
What is the maximum sum of the array elements after performing these two operations (the first, then the second)?
Input
The first line of the input contains a single integer () — the number of test cases. The descriptions of the test cases follow.
The first line of each test case contains a single integer () — the number of elements in array .
The second line of each test case contains integers () — elements of the array.
Output
For each test case, output the maximum sum of the array elements after sequentially performing the two given operations.
Note
In the first test case, you can first rearrange the array to get (operation 1), then choose and get the sum (operation 2).
In the second test case, you can do nothing in both operations and get the sum .
In the third test case, you can do nothing in both operations and get the sum .
In the fourth test case, you can first leave the order unchanged (operation 1), then choose and get the sum (operation 2).
In the fifth test case, you can first leave the order unchanged (operation 1), then choose and get the sum (operation 2).
In the sixth test case, you can first leave the order unchanged (operation 1), then choose and get the sum (operation 2).
Samples
8
3
-2 3 -3
1
0
2
0 1
1
-99
4
10 -2 -3 7
5
-1 -2 -3 -4 -5
6
-41 22 -69 73 -15 -50
12
1 2 3 4 5 6 7 8 9 10 11 12
8
0
1
99
22
15
270
78
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |