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
题目大意:求最大奇数和的子序列的和。
方法:先将正数加起来,得sum
    如果sum为奇数,输出sum;
    否则,sum1=sum-最小正奇数,sum2=sum+最大负奇数,输出max(sum1,sum2)。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
const int N=1e5;
int a[N];
int temp1=-1e5,temp2=1e5;
int main()
{
int n;
int sum=;
int index1=-,index2=-;
int sum1=-0x7f7f7f7f,sum2=-0x7f7f7f7f;
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
if(a[i]>)
{
sum+=a[i];
if(a[i]%)
{
temp2=min(temp2,a[i]);
}
}
else if(a[i]<)
{
if(a[i]%)
temp1=max(temp1,a[i]);
}
}
if(sum%)cout<<sum<<endl;
else
{
sum1=sum-temp2;
sum2=sum+temp1;
cout<<max(sum1,sum2)<<endl;
}
return ;
}

有点乱,我是输入的时候就找最大负奇数和最小正奇数的。

Codeforces 797B - Odd sum的更多相关文章

  1. Odd sum CodeForces - 797B

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

  2. Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)

    B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard in ...

  3. Codeforces Round #617 (Div. 3)A. Array with Odd Sum(水题)

    You are given an array aa consisting of nn integers. In one move, you can choose two indices 1≤i,j≤n ...

  4. Odd sum (对本菜鸡来说是个极坑题)

    https://codeforces.com/problemset/problem/797/B time limit per test 1 second memory limit per test 2 ...

  5. B. Odd Sum Segments CF(分割数组)

    题目地址 http://codeforces.com/contest/1196/problem/B B. Odd Sum Segments time limit per test 3 seconds ...

  6. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

  7. codeforces 797B

    B. Odd sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. codeforces 963A Alternating Sum

    codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...

  9. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

随机推荐

  1. django 表单常用field

    BooleanField字段:相当于单选框 CharField:接受字符串 参数:max_length最大长度,min_length最小长度 require字段是否是必须的,默认为required=T ...

  2. 03: pip使用

    1.1 pip常用方法 1.自我升级(升级pip到最新版本) pip install --upgrade pip 2. 安装库 pip install XXX 3. 查看当前环境所有已安装的库 pip ...

  3. linux下部署git服务器

    我这里用的是redhat7.4, 直接开始吧. 环境 服务端: Redhat7.4 + git(version 1.8.3.1) IP:192.168.137.168 客户端: win7 + git ...

  4. python简说(五)操作文件

    f = open('users.txt',encoding='utf-8') #读文件的时候,必须存在在才可以读 文件对象,或者文件句柄res = f.read()print(res)f.close( ...

  5. ORA-16038 ORA-19809 ORA-00312

    问题表现: 连接数据库启动报错,ORA-03113, 查看详细的alert日志发现更多报错,如下 ORA-19809: 超出了恢复文件数的限制ORA-19804: 无法回收 209715200 字节磁 ...

  6. mint-ui之Loadmore使用

    <template> <div class="page-loadmore"> <div class="page-loadmore-wrapp ...

  7. opencv学习之路(1)、示例程序

    一.介绍 工欲善其事必先利其器,首先当然是配置环境安装软件啦.  我安装的vs2012+opencv2.48以及opencv3.0.具体安装步骤按照浅墨大神的博客进行即可:http://blog.cs ...

  8. py4CV例子2.5车牌识别和svm算法重构

    1.什easypr数据集:  ) ) ] all_label_list = temp[:, ] n_sample = , ) matcher = cv2.FlannBasedMatcher(flann ...

  9. Django框架(八) Django之ORM数据库操作

    创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一的关系( ...

  10. 振兴中华|2013年蓝桥杯A组题解析第三题-fishers

    标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格子. 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg) 从我做起振 我做起振兴 做起振兴中 起振兴中华 比赛 ...