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.
The 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.
其实我也只在比赛的时候写了个A题而已,还WA了一次,
题意:求和,要求出最大的偶数和,
可以这样理解,如果加完是奇数,则要减去最小的奇数;如果加完是偶数,则就是最大值
AC代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std ;
long long a[100000];
int main()
{
int n ;
while(cin>>n)
{
long long sum = 0 ;
for(int i = 0 ; i < n ;i++)
{
cin>>a[i];
}
long long sum2=0;
for(int i = 0 ; i < n ; i++)
{
sum+=a[i];
}
sort(a,a+n);
for(int i = 0 ; i < n ; i++)
{
int j ;
if(sum%2!=0)
{
for(j=0;j<n;j++)
{
if(a[j]%2!=0)
{
sum-=a[j];
break;
}
}
}
}
cout<<sum<<endl;
}
return 0 ;
}