Odd sum CF

来源:互联网 发布:淘宝助手创建宝贝模板 编辑:程序博客网 时间:2024/06/04 19:26
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 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 numbern (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
32 -5 -3
Output
-1
Note

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


题目的意思就是说从一个序列中挑选子序列,使得这个子序列的和是最大的奇数

先考虑极端情况

1.所有的元素都是正的,容易想到是对这n个元素求和,判断是不是奇数,如果是 直接输出,如果不是,就减去n个元素中最小的偶数。

2.所有的元素都是负的,那么直接找到最大的奇元素就好了;

进行推广

如果又有正的,又有负的,那么就对整数元素进行求和,盘算是不是奇数,如果是直接输出,如果不是,求max(最大整数和-最小正偶数,最大整数和+最大负偶数)

下面附上代码

#include<bits/stdc++.h>using namespace std;int main(){    int n;    scanf("%d",&n);    int sum=0;    int minn=0x3f3f3f3f;    int maxx=-0x3f3f3f3f;    for(int i=0;i<n;i++)    {        int t;        scanf("%d",&t);        if(t>0)            sum+=t;            if(t<0&&t%2&&t>maxx)                maxx=t;            if(t>0&&t%2&&t<minn)                minn=t;    }    if(sum%2==0)    {        cout<<max(sum-minn,sum+maxx)<<endl;    }    else        cout<<sum<<endl;}

0 0
原创粉丝点击