SDNU_ACM_ICPC_2020_Winter_Practice_2nd F 水

题目

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
Note
In the first example sum of the second and the fourth elements is 3.

大意
输入n个数,从中选取任意个数,不改变原顺序,组成一个子序列,其和为奇数且最大

思路

在数据范围中可以看到有正数,有负数。

那么先把所有正数加起来,如果是奇数,就输出,
如果不是,减去最小的正奇数或者 加上最小的负奇数,输出结果。

AC代码

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
    int n,x,mj=-100000,ans=0,mz=100000;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        if(x>0){ans+=x;if(x%2!=0)mz=min(mz,x);}
        else if(x%2!=0)
        {
            mj=max(mj,x);
        }
    }
    if(ans&1){cout<<ans<<endl;}
    else
    {
        ans=max(ans+mj,ans-mz);
        cout<<ans<<endl;
    }
    return 0;
}

发布了46 篇原创文章 · 获赞 0 · 访问量 1146

猜你喜欢

转载自blog.csdn.net/weixin_45719073/article/details/104268721