Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark.
Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.
InputThe first line of the input contains one integer, n (1 ≤ n ≤ 100 000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 109, inclusive.
Print the maximum possible even sum that can be obtained if we use some of the given integers.
3
1 2 3
6
5
999999999 999999999 999999999 999999999 999999999
3999999996
In the first sample, we can simply take all three integers for a total sum of 6.
In the second sample Wet Shark should take any four out of five integers 999 999 999.
思路:如果说有数中奇数数目为偶数,则全部相加的和即为最优解,否则,如果奇数个数为奇数,
则需要在总合中找到最小的奇数减掉,即为最优解。
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
long long a[n],Max=0,cnt,num;
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
Max+=a[i];
if(a[i]%2!=0)
cnt++;
}
sort(a,a+n);
num=0;
if(cnt%2!=0)
{
for(int i=0;i<n;i++)
{
if(a[i]%2!=0)
{
num=a[i];
break;
}
}
Max-=num;
}
printf("%lld\n",Max);
return 0;
}