Educational Codeforces Round 19 B

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.

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.

题意:求子序列和最大为奇数是多少

解法:奇数和偶数相加减的关系

 

复制代码
 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 }
复制代码

 

 

posted @   樱花落舞  阅读(282)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2016-04-17 Codeforces Round #347 (Div. 2) A
2016-04-17 大数模版
点击右上角即可分享
微信分享提示