codeforces——797B——Odd sum

来源:互联网 发布:djvu mac 打开 编辑:程序博客网 时间:2024/06/06 17:01
B. Odd sum
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 lengthn. 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 numbersa1, 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
32 -5 -3
Output
-1
Note

In the first example sum of the second and the fourth elements is 3.


求所给所给值的所有子集中 和最大的奇数 输出那个数

要注意算法。。

#include<cstdio>#include<iostream>#include<algorithm>#define MIN -1e9using namespace std;bool compare(int a,int b){    return a>b;}int main(){    int n;    while(~scanf("%d",&n))    {        int t2,ans=0,com[3]= {MIN,MIN,MIN},t3=MIN;        while(n--)        {            scanf("%d",&t2);            if(t2%2==0)            {                if(t2>0)                    ans+=t2;            }            else if(t2>0)            {                com[2]=t2;                sort(com,com+3,compare);                if(com[0]+com[1]+com[2]>0)                {                    ans+=com[0]+com[1];                    com[0]=com[2];                    com[1]=com[2]=MIN;                }            }            else if(t2>t3)                t3=t2;        }        sort(com,com+3,compare);        if(com[0]!=MIN)            ans+=com[0];        else            ans+=t3;        if(com[1]!=MIN)            if(t3+com[1]>0)                ans+=t3+com[1];        cout<<ans<<endl;    }    return 0;}

原创粉丝点击