Codeforces 797B - Odd sum

时间:2022-03-29 00:20:58
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 &num;575 &lpar;Div&period; 3&rpar; B&period; Odd Sum Segments &lpar;构造,数学&rpar;

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

  3. Codeforces Round &num;617 &lpar;Div&period; 3&rpar;A&period; 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 &lpar;对本菜鸡来说是个极坑题&rpar;

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

  5. B&period; 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&period; Sum Queries&quest; (线段树

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

随机推荐

  1. 【实用技巧】取消Win7开机账户的手动选择

    因为前面碰到的一些事情,稍有感慨. 关于win7的一些小技巧都不是什么很有技术含量东西,或者说很浅显.我说一个技巧,也许很多人都知道,也许也早有人说过.但我想说的是我不是在炫耀什么,我只是想分享一些我 ...

  2. iOS 拉伸图片

    UIEdgeInsets:四个参数,会填入上左下右几个值,这几个值,代表着距离边界的这几个点描绘的区域,是不会拉伸的.所以,到时候,计算清楚这几个值就可以了.

  3. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

    一.简介 1.You now have three methods for distinguishing references:  Objects are identical if they occ ...

  4. SQL-Server索引漫谈

    http://www.cnblogs.com/teroy/archive/2013/05/23/3070547.html

  5. Redis的key过期处理策略

    Redis中有三种处理策略:定时删除.惰性删除和定期删除. 定时删除:在设置键的过期时间的时候创建一个定时器,当过期时间到的时候立马执行删除操作.不过这种处理方式是即时的,不管这个时间内有多少过期键, ...

  6. Linux官方源、镜像源汇总

    本文收录在日常运维杂烩系列 一.站点版 1.企业站 搜狐:http://mirrors.sohu.com/ 网易:http://mirrors.163.com/ 阿里云:http://mirrors. ...

  7. 附002&period;Docker常见命令

    # docker --help Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ -h ...

  8. dp练习(0)——数字三角形

    3298: 数字三角形 时间限制: 1 Sec  内存限制: 128 MB提交: 114  解决: 29[提交][状态][讨论版] 题目描述 如图示出了一个数字三角形. 请编一个程序计算从顶至底的某处 ...

  9. 如何合理命名CSS文件——摘自网友

    有经验的网页制作者都明白,对于有多个栏目的大型网站而言,使用单一的CSS文件是不可能的.但CSS文件名如何命名对于新手来说是件容易出乱子的事.如何才能将CSS的命名做得井井有条? 坚持使用统一的CSS ...

  10. 切割数组 - 将一个数组拆分成多个长度为n的数组

    有时候接口返回的数据很长,而前端显示需要分组显示这些数据,这个时候就需要将数组拆分: datas = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; var arrLen ...