Description
You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
You should write a program which finds sum of the best subsequence.
The first line contains integer number n (1 ≤ n ≤ 105).
The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.
Print sum of resulting subseqeuence.
4
-2 2 -3 1
3
3
2 -5 -3
-1
In the first example sum of the second and the fourth elements is 3.
题意:求子序列和最大为奇数是多少
解法:奇数和偶数相加减的关系
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <queue> 6 #include <stack> 7 #include <map> 8 #include <cstring> 9 #include <climits> 10 #include <cmath> 11 12 using namespace std; 13 14 const int maxn = 200010; 15 long long a[maxn],b[maxn],c[maxn],dp[maxn]; 16 long long m,n; 17 long long sum; 18 long long Min=(1e6); 19 long long Minn=(1e6); 20 int main() 21 { 22 cin>>n; 23 for(int i=1;i<=n;i++) 24 { 25 cin>>a[i]; 26 if(a[i]>=0) 27 { 28 sum+=a[i]; 29 if(a[i]%2) 30 { 31 Minn=min(Minn,a[i]); 32 } 33 } 34 else 35 { 36 long long ans=abs(a[i]); 37 if(ans%2) 38 { 39 Min=min(ans,Min); 40 // cout<<Min<<endl; 41 } 42 } 43 } 44 if(sum%2) 45 { 46 cout<<sum<<endl; 47 } 48 else 49 { 50 cout<<max(sum-Min,sum-Minn)<<endl; 51 } 52 return 0; 53 }