A11301 | Turtle Puzzle: Rearrange and Negate
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an array $a$ of $n$ integers. You must perform the following two operations on the array (the first, then the second):
1. Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged.
2. 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 $l, r$ such that $1 \le l \le r \le n$ and assign $a_i = -a_i$ for all $l \le i \le r$ (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)?
1. Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged.
2. 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 $l, r$ such that $1 \le l \le r \le n$ and assign $a_i = -a_i$ for all $l \le i \le r$ (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)?
输入格式
The first line of the input contains a single integer $t$ ( $1 \le t \le 1000$ ) — the number of test cases. The descriptions of the test cases follow.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 50$ ) — the number of elements in array $a$ .
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-100 \le a_i \le 100$ ) — elements of the array.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 50$ ) — the number of elements in array $a$ .
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-100 \le a_i \le 100$ ) — elements of the array.
输出格式
For each test case, output the maximum sum of the array elements after sequentially performing the two given operations.
输入输出样例
输入 #1
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
输出 #1
8 0 1 99 22 15 270 78
In the first test case, you can first rearrange the array to get $[3,-2,-3]$ (operation 1), then choose $l = 2, r = 3$ and get the sum $3 + -((-2) + (-3)) = 8$ (operation 2).
In the second test case, you can do nothing in both operations and get the sum $0$ .
In the third test case, you can do nothing in both operations and get the sum $0 + 1 = 1$ .
In the fourth test case, you can first leave the order unchanged (operation 1), then choose $l = 1, r = 1$ and get the sum $-(-99) = 99$ (operation 2).
In the fifth test case, you can first leave the order unchanged (operation 1), then choose $l = 2, r = 3$ and get the sum $10 + -((-2) + (-3)) + 7 = 22$ (operation 2).
In the sixth test case, you can first leave the order unchanged (operation 1), then choose $l = 1, r = 5$ and get the sum $-((-1)+(-2)+(-3)+(-4)+(-5))=15$ (operation 2).
In the second test case, you can do nothing in both operations and get the sum $0$ .
In the third test case, you can do nothing in both operations and get the sum $0 + 1 = 1$ .
In the fourth test case, you can first leave the order unchanged (operation 1), then choose $l = 1, r = 1$ and get the sum $-(-99) = 99$ (operation 2).
In the fifth test case, you can first leave the order unchanged (operation 1), then choose $l = 2, r = 3$ and get the sum $10 + -((-2) + (-3)) + 7 = 22$ (operation 2).
In the sixth test case, you can first leave the order unchanged (operation 1), then choose $l = 1, r = 5$ and get the sum $-((-1)+(-2)+(-3)+(-4)+(-5))=15$ (operation 2).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted