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 ;
}

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

最新文章

  1. 【Django】--Models 和ORM以及admin配置
  2. 解决 Gnome3 窗口背景是黑色的问题
  3. Laravel学习笔记(六)数据库 数据库填充
  4. Corporative Network_并查集
  5. DoTween使用
  6. springmvc+hibernate入门-揭开神秘的面纱
  7. HW6.13
  8. java1.8的几大新特性(二)
  9. PostMessage 向Windows窗口发送Alt组合键
  10. 《JavaScript 高级程序设计》读书笔记五 引用类型
  11. scrapy 基础
  12. Spring Boot 启动:No active profile set, falling back to default profiles: default
  13. Caffe和py-faster-rcnn日常使用备忘录
  14. [UE4]Size Box
  15. 与元素类型 &quot;item&quot; 相关联的 &quot;name&quot; 属性值不能包含 &#39;&lt;&#39; 字符。
  16. JAVA框架 Spring junit整合单元测试
  17. LA 3644 易爆物
  18. Docker实战(八)之Web服务与应用
  19. 【转】嵌入式Linux驱动面试题三道
  20. sum函数

热门文章

  1. AnswerOpenCV(1001-1007)一周佳作欣赏
  2. JS事件覆盖问题和触发问题
  3. bzoj 1095 Hide 捉迷藏 - 动态点分治 -堆
  4. Manjaro kde 18.0安装与基本配置
  5. Django 创建项目笔记
  6. bsgs整理
  7. python 之 知识点(1)
  8. 题解——洛谷P3128 [USACO15DEC]最大流Max Flow
  9. Unity3D学习笔记(二十六):MVC框架下的背包系统(1)
  10. windows service in vs