CodeForces - 621A Wet Shark and Odd and Even

题目: 传送门
思路:
保证 sum 结果为 偶数,则 a[i] 为偶数 ,一定可以加上 .
如果a[I]为奇数,我们先把其放进奇数之和里,如果最后奇数之和为偶数,直接加上,反正,减去最小的奇数即可。

#include <iostream>
#include <algorithm>
using namespace std;

long long ans = 0;
long long mins_odd=1e15,sum_odd=0;

int main() {
    int n;
    cin>>n;
    while(n--) {
        long long a;
        cin>>a;
        if(a%2==0) {
            ans+=a;
        }
        else {
            sum_odd+=a;
            mins_odd=min(mins_odd,a);
        }
    }
    cout<<ans + (sum_odd%2==0? sum_odd:sum_odd-mins_odd)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43305984/article/details/89184472