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 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.减去最小的正奇数

2.加上最大的负奇数

最后判断一下两种情况的大小,取较大值

实现代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int m,a[],b[],c[];
int i;
cin>>m;
int k = ,l= ;
for(i=;i<m;i++)
{
cin>>a[i];
if(a[i]>)
b[k++] = a[i];
else
c[l++] = a[i];
}
sort(b,b+k);
int sum = ;
for(i=k-;i>=;i--)
{
sum += b[i];
}
if(sum%==)
{
int sum1=sum,sum2=sum;
sum = -;
for(i=;i<k;i++)
{
if(b[i]%==)
sum1-=b[i];
if(abs(sum1)%==){
sum = sum1;
break;
}
}
//cout<<"sum1:"<<sum1<<endl;
sort(c,c+l);
for(i=l-;i>=;i--){
if(abs(c[i])%==)
sum2+=c[i];
if(abs(sum2)%==){
sum = max(sum,sum2);
}
}
//cout<<"sum2:"<<sum2<<endl;
}
cout<<sum<<endl;
}

codeforces 797B的更多相关文章

  1. Odd sum CodeForces - 797B

    Odd sum CodeForces - 797B 好方法:贪心 贪心2 糟糕(不用动脑)的方法:dp ans[i][0]表示到第i个和为偶数最大,ans[i][1]表示到第i个和为奇数最大. 但是, ...

  2. Codeforces 797B - Odd sum

    B. Odd sum 题目链接:http://codeforces.com/problemset/problem/797/B time limit per test 1 second memory l ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Java:内省(Introspector)

    内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且 ...

  2. Python写代码的时候为什么要注释?Sun因此被Oracle收购

    导读: 此块分为:1.注释的重要性 2.如何正确注释 注释的重要性 在我们看代码的时候,会遇到很多看不懂得代码,特别是在做项目的时候,代码的注释以及命名习惯的重要性就有了为什么这么说呢? 因为在很多情 ...

  3. 转 Velocity中加载vm文件的三种方式

    Velocity中加载vm文件的三种方式   velocitypropertiespath Velocity中加载vm文件的三种方式:    方式一:加载classpath目录下的vm文件 Prope ...

  4. 汇编 STD和CLD指令

    一.用纯汇编封装函数strcmpW 1.用repnz scasw计算字串长度 2.用repz cmpsw比较字串内容 3.把比较的结果存放在EAX里边返回 __declspec(naked) int ...

  5. webpack教程(一)——初体验

    首先全局安装webpack,再npm初始化一个项目,并局部安装webpack开发工具 $ npm install webpack -g npm init (项目名称) $ npm install we ...

  6. Linux umask

    新建一个文件或目录,它的默认权限是什么?如果要修改一个用户创建的文件和目录的默认权限该如何做?本文将介绍相关的内容.说明:本文的演示环境为 ubuntu 16.04. 文件的默认权限 为了查看用户创建 ...

  7. Ubuntu16.04下安装QQ的完整操作记录(经验证可用)

    本机安装了Ubuntu16.04系统,用于日常运维办公.打算在Ubuntu上安装QQ,如下操作记录也使用于Ubuntu18.04: 1)先下载特制的QQ程序包(其实就是基于Wine容器做了一些封装,程 ...

  8. Individual Project-word frequency

    预计时间: 项目要求理解:半小时 c#语言了解:6小时 构思程序框架:2小时 编写调试程序:4小时 项目实际完成时间: 项目要求理解:半小时 c#语言了解:6小时 构思程序框架:2小时 编写调试程序: ...

  9. The Contest CodeForces - 813A (思维)

    Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...

  10. ECharts设置y轴显示

    参考地址:https://www.w3cschool.cn/echarts_tutorial/echarts_tutorial-no3h2cul.html yAxis: { type: 'value' ...