Codeforces Round 929 (Div. 3 ABCDEFG题) 视频讲解

03-03 1148阅读

A. Turtle Puzzle: Rearrange and Negate

Problem Statement

You are given an array a a a of n n 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 l, r l,r such that 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1≤l≤r≤n and assign a i = − a i a_i = -a_i ai​=−ai​ for all l ≤ i ≤ r l \le i \le r l≤i≤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)?

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1≤t≤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 n n ( 1 ≤ n ≤ 50 1 \le n \le 50 1≤n≤50) — the number of elements in array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1​,a2​,…,an​ ( − 100 ≤ a i ≤ 100 -100 \le a_i \le 100 −100≤ai​≤100) — elements of the array.

Output

For each test case, output the maximum sum of the array elements after sequentially performing the two given operations.

Example

Example

input
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
output
8
0
1
99
22
15
270
78

Note

In the first test case, you can first rearrange the array to get [ 3 , − 2 , − 3 ] [3,-2,-3] [3,−2,−3] (operation 1), then choose l = 2 , r = 3 l = 2, r = 3 l=2,r=3 and get the sum 3 + − ( ( − 2 ) + ( − 3 ) ) = 8 3 + -((-2) + (-3)) = 8 3+−((−2)+(−3))=8 (operation 2).

In the second test case, you can do nothing in both operations and get the sum 0 0 0.

In the third test case, you can do nothing in both operations and get the sum 0 + 1 = 1 0 + 1 = 1 0+1=1.

In the fourth test case, you can first leave the order unchanged (operation 1), then choose l = 1 , r = 1 l = 1, r = 1 l=1,r=1 and get the sum − ( − 99 ) = 99 -(-99) = 99 −(−99)=99 (operation 2).

In the fifth test case, you can first leave the order unchanged (operation 1), then choose l = 2 , r = 3 l = 2, r = 3 l=2,r=3 and get the sum 10 + − ( ( − 2 ) + ( − 3 ) ) + 7 = 22 10 + -((-2) + (-3)) + 7 = 22 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 l = 1, r = 5 l=1,r=5 and get the sum − ( ( − 1 ) + ( − 2 ) + ( − 3 ) + ( − 4 ) + ( − 5 ) ) = 15 -((-1)+(-2)+(-3)+(-4)+(-5))=15 −((−1)+(−2)+(−3)+(−4)+(−5))=15 (operation 2).

Solution

具体见文后视频。


Code

#include 
#define int long long
using namespace std;
typedef pair PII;
typedef long long LL;
const int MAXN = 55;
int N;
int A[MAXN];
void solve()
{
	cin >> N;
	for (int i = 1; i > A[i];
	sort(A + 1, A + 1 + N);
	for (int i = 1; i 
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);
	int Data;
	cin  Data;
	while (Data --)
		solve();
	return 0;
}

	cin  N;
	int Sum = 0, Vis[3] = {0};
	for (int i = 1; i > A[i], Sum += A[i], Vis[A[i] % 3] = 1;
	if (Sum % 3 == 0) cout 
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);
	int Data;
	cin  Data;
	while (Data --)
		solve();
	return 0;
}

	int A, B, L;
	cin  A > B >> L;
	set S;
	for (int a = 1; a 
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);
	int Data;
	cin  Data;
	while (Data --)
		solve();
	return 0;
}

	unordered_map N;
	for (int i = 1; i > A[i], Cnt[A[i]] ++;
	sort(A + 1, A + 1 + N);
	if (Cnt[A[1]] == 1) cout 
		int mn = 1e18;
		for (int i = 1; i 
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);
	int Data;
	cin  Data;
	while (Data --)
		solve();
	return 0;
}

	int Section = A[r] - A[l - 1];
	return (2 * u + 1 - Section) * Section / 2;
}
void solve()
{
	cin  N;
	for (int i = 1; i  Q;
	while (Q --)
	{
		int L, U;
		cin >> L >> U;
		int l = L, r = N;
		while(l  s;
		int v = s[0] == 'c';
		for (int i = 0; i  

视频讲解

Codeforces Round 929 (Div. 3)(A ~ G 题讲解)


最后祝大家早日Codeforces Round 929 (Div. 3 ABCDEFG题) 视频讲解

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]