For her birthday, each of Asuna's nn admirers gifted her a tower. The height of the tower from the ii-th admirer is equal to aiai.
Asuna evaluates the beauty of the received gifts as max(a1,a2,…,an)max(a1,a2,…,an). She can perform the following operation an arbitrary number of times (possibly, zero).
- Take such 1≤i≠j≤n1≤i≠j≤n that ai+ajai+aj is odd and ai>0ai>0, then decrease aiai by 11 and increase ajaj by 11.
It is easy to see that the heights of the towers remain non-negative during the operations.
Help Asuna find the maximum possible beauty of the gifts after any number of operations!
Input
Each test consists of several test cases. The first line of the input data contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of admirers of Asuna.
The second line of each test case contains nn integers a1,a2,…,an (1≤ai≤109)a1,a2,…,an (1≤ai≤109) — the heights of the towers.
It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output a single integer: the maximum value of the beauty of the gifts that Asuna can achieve.
题目分析
操作:因为相加是奇数才能进行操作,所以只有一个奇数和一个偶数之间能够进行一个加1和一个减1操作。
(1)如果全部都是偶数或者都是奇数则不能进行操作,直接输出数组中的最大值。
(2)如果奇数偶数都有:
一个偶数可以把所有奇数都加到自己身上,使得自己变成奇数。
然而,如果要想使得自己保持偶数,就可以使得操作的对面的数减少到1就停止,自己还就是偶数,然后就能去接着操作其他奇数。如果后面还有其他偶数的话可以选择一个1把自己变成奇数去操作其他的偶数。
我们使用这个规律并且通过整理分析数据得到,最后除了最大值有k-1个1,其他都是0,且最后的最大值为总和减去奇数的总数加1。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[200005];
int t, n = 0, i, ji, ou;
long long sum;
cin >> t;
while (t--){
ji=0;
ou=0;
sum = 0;
//操作次数 >= 0 操作(ai+aj)%2 != 0 && ai > 0
//ai--,aj++;
cin >> n;
//只有奇数加偶数是奇数
for(i = 1; i <= n; i++){
cin >> a[i];
if(a[i] % 2 == 0)ou++;
else ji++;
sum += a[i];
}
sort(a+1, a+n+1);
if(ji == n || ji == 0){//全部都是奇数或者都是偶数
//直接输出最大值
cout << a[n] << endl;
}else{
cout << sum - ji + 1 << endl;
}
}
return 0;
}