题目链接:http://codeforces.com/contest/797/problem/B
题意:n个数,求选出一些数,使这些数的和为奇数且最大。
把所有偶数>0的加起来,再把奇数统计出来。这样暂时的和为偶数且最大。
加上一个最大的奇数则一定是奇数和。接下来在奇数里扫2个相邻的数,假如和>=0则可以算上。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int maxn = 100100; 6 int n; 7 vector<int> odd; 8 9 int main() { 10 // freopen("in", "r", stdin); 11 while(~scanf("%d", &n)) { 12 int ret = 0, a; 13 odd.clear(); 14 for(int i = 1; i <= n; i++) { 15 scanf("%d", &a); 16 if(a % 2 == 0) { 17 if(a > 0) ret += a; 18 } 19 else odd.push_back(a); 20 } 21 sort(odd.begin(), odd.end()); 22 ret += odd[odd.size()-1]; 23 for(int i = odd.size()-2; i >= 1; i--) { 24 if(odd[i]+odd[i-1]>=0) { 25 ret += odd[i]+odd[i-1]; 26 i--; 27 } 28 } 29 printf("%d ", ret); 30 } 31 return 0; 32 }