K. Windblume Festival
Problem
The Windblume Festival in Mondstadt is coming! People are preparing windblumes for Barbatos and for those they love and adore. The Windblume Festival is also an opportunity to improve the relationships people have.
Source: Genshin Impact Official
During the festival, a famous game will be played every year, invented by Jean, the Acting Grand Master of the Knights of Favonius. In the game, n n n players numbered from 1 1 1 to n n n stand in a circle, each holding an integer with them. Each turn, one player will be removed. The game will end when there is only one player left.
For each turn, let k k k be the number of players remaining and a i a_i ai be the integer player i i i holds. Two adjacent players, x x x and ( x m o d k + 1 ) (x \bmod k + 1) (xmodk+1) are selected and player ( x m o d k + 1 ) (x \bmod k + 1) (xmodk+1) is removed from the game. Player x x x’s integer will then change from a x a_x ax to ( a x − a x m o d k + 1 ) (a_x - a_{x \bmod k + 1}) (ax−axmodk+1). Player y y y in this turn will become player ( y − 1 ) (y - 1) (y−1) in the next turn for all KaTeX parse error: Expected 'EOF', got '&' at position 3: x &̲lt; y \le k, though the integer they hold will not change.
Jean wants to know the maximum possible integer held by the last remaining player in the game by selecting the players in each round optimally.
Input
There are multiple test cases. The first line of the input contains one integer T T T indicating the number of test cases. For each test case:
The first line contains one integer n n n ( 1 ≤ n ≤ 1 0 6 1 \le n \le 10^6 1≤n≤106) indicating the initial number of players.
The next line contains n n n integers a i a_i ai ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 −109≤ai≤109) where a i a_i ai is the integer held by player i i i at the beginning.
It is guaranteed that the sum of n n n of all test cases will not exceed 1 0 6 10^6 106.
Output
For each test case output one line containing one integer indicating the maximum possible integer.
Example
Input
5
4
1 -3 2 -4
11
91 66 73 71 32 83 72 79 84 33 93
12
91 66 73 71 32 83 72 79 84 33 33 93
13
91 66 73 71 32 83 72 79 84 33 33 33 93
1
0
Output
10
713
746
779
0
Note
For the first sample test case follow the strategy shown below, where the underlined integers are the integers held by the players selected in each turn.
{ 1 ‾ , − 3 , 2 , − 4 ‾ } \{\underline{1}, -3, 2, \underline{-4}\} {1,−3,2,−4} (select x = 4 x = 4 x=4) → \to → { − 3 , 2 , − 5 ‾ } \{-3, \underline{2, -5}\} {−3,2,−5} (select x = 2 x = 2 x=2) → \to → { − 3 , 7 ‾ } \{\underline{-3, 7}\} {−3,7} (select x = 2 x = 2 x=2) → \to → { 10 } \{10\} {10}.
Code
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
ll n;
cin>>n;
ll x;
if(n==1)
{
cin>>x;
cout<<x<<endl;
return;
}
ll sum=0,f1=0,f2=0,mn=1e18;
for(ll i=0;i<n;i++)
{
cin>>x;
sum+=abs(x);
mn=min(mn,abs(x));
if(x<=0) f1=1;
if(x>=0) f2=1;
}
if(f1&&f2) cout<<sum<<endl;
else cout<<sum-2*mn<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
ll t;
cin>>t;
while(t--) solve();
return 0;
}