Description
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.
Input
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.
Output
Print the maximum possible even sum that can be obtained if we use some of the given integers.
Sample Input
3 1 2 3
6
5 999999999 999999999 999999999 999999999 999999999
3999999996
Hint
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<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
int a[100001],b[100001];
for(int i=0;i<t;i++)
scanf("%d",&a[i]);
long long sum=0,max,k=0;
for(int i=0;i<t;i++)
{
sum+=a[i];
if(a[i]%2!=0)
{
b[k]=a[i];
k++;
}
}
if(k%2==0)
printf("%lld\n",sum);
else
{
sort(b,b+k);
printf("%lld\n",sum-b[0]);
}
}
return 0;
}
思路:判断有多少个奇数,偶数个直接输出,奇数个减去最小的奇数就是最大值了。注意取的值都是正值,没好好审题导致开始还在那想小于0的情况晕
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
int a[100001];
for(int i=0;i<t;i++)
scanf("%d",&a[i]);
sort(a,a+t);
long long sum=0,max=0;
for(int i=t-1;i>=0;i--)
{
sum+=a[i];
if(sum%2==0)
{
if(sum>max)
{
max=sum;
}
}
}
printf("%lld\n",max);
}
return 0;
}
这完全是暴力解没有考虑奇数大小!