CF621A - Wet Shark and Odd and Even

题目大意:给你n个数,每个数选或不选,加起来得到一个偶数,求最大偶数。

排序,偶数在前,奇数在后,奇偶分别递减。偶数直接加,奇数两个两个加,扫一遍即可。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
struct aaa{
	long long x;
	bool friend operator<(aaa a,aaa b){
		if(a.x%2==b.x%2)return a.x>b.x;
		if(a.x%2==0)return 1;
		return 0;
	}
}a[110000];
int n;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		a[i].x=x;
	}
	sort(a+1,a+1+n);
	long long ans=0;
	for(int i=1;i<=n;i++){
		if(a[i].x%2==0){
			ans+=a[i].x;
		}else{
			if(i+1<=n){
				ans+=a[i].x+a[i+1].x;
				a[i].x=a[i+1].x=0;
			}
		}
	}
	cout<<ans<<endl;	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值