Odd sum (of the chicken dish is a very pit title)

https://codeforces.com/problemset/problem/797/B

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

Print sum of resulting subseqeuence.

Examples
input
4
-2 2 -3 1
output
3
input
3
2 -5 -3
output
-1 

  Qiuzi sequence, the sum of the maximum odd. The meaning of the questions, the sequence elements can be freely combined.
  Solution to a problem :: To most, certainly have to put all the positive numbers add up for the sum, to see if he is not odd, and if so, direct output. Otherwise,
      make sum2 = sum, sum1 = sum;
      array row a sequence;
      positive numbers do over, it is not odd, so for a negative start. But on the negative is not easily engage in, you know, sum2 each plus a negative number, will be smaller. Our goal is to become the odd sum2, according to the even - odd = odd and greedy thoughts,
we need the greatest negative odd, after finding, sum2 + = it, you can.
      But just performed is a subtraction operation, it does not guarantee that it is the largest, because the sum added to the process, there may be an odd number, then the sum of the state at this time is not necessarily smaller than sum2. So:
       SUM1 = SUM, according to the greedy idea, and even - odd = odd, find the smallest positive odd, sum1- = it.
      Again sum1, sum2 a comparison can be performed, however, according to the sample 2, sum1 is even possible, since the sum does not appear odd, is determined in addition to the pertinent process. Similarly sum2 too.
    Hang death of me ....
    on the code
  
#include<iostream>
#include<cstring>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#include<map>
const int maxn=1e5+10;
int maxx=-2e4+10;
int a[maxn];
int main()    //qq as a
{
    int n;
    cin>>n;
    int sum=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>0)
            sum+=a[i];
    //    cout<<"sum: "<<sum<<"  a:"<<a[i]<<endl;
    }
    if(sum%2!=0)
        cout<<sum<<endl;
    else
    {
        sort(a,a+n);
        int sum1=sum;
        for(int i=0;i<n;i++)
        {
            if(a[i]>0&&a[i]%2!=0)
            {
                sum1-=a[i];break;
            }
        }
        int sum2=sum;
        for(int i=n-1;i>=0;i--)
        {
            if(a[i]<0&&(-a[i])%2!=0)
            {
                sum2+=a[i];break;
            }
        }
    //    cout<<sum1<<" "<<sum2<<endl;
        if(sum1%2==0)
            cout<<sum2<<endl;
        else if(sum2%2==0)
            cout<<sum1<<endl;
        else if(sum1<sum2)
            cout<<sum2<<endl;
        else
            cout<<sum1<<endl;
    }
    return 0;
}

 


      

Guess you like

Origin www.cnblogs.com/liyexin/p/11600335.html