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. Introduction To Machine Learning Self-Evaluation Test

    Preface Section 1 - Mathematical background Multivariate calculus take derivatives and integrals; de ...

  2. 单片机中printf函数的重映射

    单片机中printf函数的重映射 一.源自于:大侠有话说 1.如果你在学习单片机之前学过C语言,那么一定知道printf这个函数.它最最好用的功能 除了打印你想要的字符到屏幕上外,还能把数字进行格式化 ...

  3. Codeforces 15E Triangles - 组合数学

    Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby fores ...

  4. nginx: [emerg] BIO_new_file("/etc/nginx/ssl_key/server.crt") failed (SSL: error:02001002:syste

    Centos 7.5 nginx+web集群配置https报错 报错信息: [root@lb01 conf.d]# nginx -tnginx: [emerg] BIO_new_file(" ...

  5. C/C++宏定义交换两个值

    #define SWAP(t,a,b) \ do{ \ t c = a; \ a = b; \ b = c; \ }); void main() { ,b=; SWAP(int,a,b); }

  6. Flask学习【第1篇】:Flask介绍

    Flask介绍(轻量级的框架,非常快速的就能把程序搭建起来) Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是So ...

  7. android设备如何进入深度休眠还能继续使用定时器【求解】

    经过试验,andriod设备进入深度休眠的时候,定时器是不能使用.但是阻止设备进入深度休眠,可以获取一把锁,但是拿了锁之后,设备不能进入休眠,系统的功耗会增加.怎么能够在系统进入休眠,定时器还能正常工 ...

  8. HDU 5929 Basic Data Structure(模拟 + 乱搞)题解

    题意:给定一种二进制操作nand,为 0 nand 0 = 10 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP ...

  9. --HTML标签2

    表单元素: <input>标签 搜集用户信息 属性:type=" " text 默认值 size 长度 value 规定值 readonly 规定值 placehold ...

  10. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...