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
  1. 4
    -2 2 -3 1
output
  1. 3
input
  1. 3
    2 -5 -3
output
  1. -1
    题目大意:求最大奇数和的子序列的和。
    方法:先将正数加起来,得sum
        如果sum为奇数,输出sum;
        否则,sum1=sum-最小正奇数,sum2=sum+最大负奇数,输出max(sum1,sum2)。
    代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<iostream>
  5. #define ll long long
  6. using namespace std;
  7. const int N=1e5;
  8. int a[N];
  9. int temp1=-1e5,temp2=1e5;
  10. int main()
  11. {
  12. int n;
  13. int sum=;
  14. int index1=-,index2=-;
  15. int sum1=-0x7f7f7f7f,sum2=-0x7f7f7f7f;
  16. cin>>n;
  17. for(int i=;i<n;i++)
  18. {
  19. cin>>a[i];
  20. if(a[i]>)
  21. {
  22. sum+=a[i];
  23. if(a[i]%)
  24. {
  25. temp2=min(temp2,a[i]);
  26. }
  27. }
  28. else if(a[i]<)
  29. {
  30. if(a[i]%)
  31. temp1=max(temp1,a[i]);
  32. }
  33. }
  34. if(sum%)cout<<sum<<endl;
  35. else
  36. {
  37. sum1=sum-temp2;
  38. sum2=sum+temp1;
  39. cout<<max(sum1,sum2)<<endl;
  40. }
  41. return ;
  42. }

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

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. Windbg解决系统蓝屏

    win10企业版连续两天遭遇系统蓝屏, 今天就各种检查,准备好好地研究一下这个问题,以下是整个过程: 首先,找到系统蓝屏时的错误日志: [计算机] --> [管理] --> [系统工具] ...

  2. Spring Boot(六):如何使用mybatis

    Spring Boot(六):如何使用mybatis orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动 ...

  3. windows下常用linux对应工具

    tail 可从http://files.cnblogs.com/hantianwei/tail.zip下载,解压后exe,如下: e:\>tail -fn 300 tool-slow.logus ...

  4. python简说(三)字典

    一.字典 stu_info = {"name": "王志华", "age": 18, "addr": "北京& ...

  5. Selenium 15: How to Run Parallel Tests Using Selenium Grid and JUnit

    In this post, I will give two techniques and describe how to run your selenium tests in parallel by ...

  6. 关于scrapy 使用代理相关问题

    在scrapy中使用代理时,我们不能保证每个代理都可用,难免出现代理ip错误的情况,如果代理ip出现错误设置一个请求超时和重新发送这个链接 在yield scrapy.Request时候加上一个参数: ...

  7. eMMC之分区管理、总线协议和工作模式【转】

    本文转载自:https://blog.csdn.net/u013686019/article/details/66472291 一.eMMC 简介 eMMC 是 embedded MultiMedia ...

  8. 编译openwrt时总是报错“staging_dir/target-aarch64_generic_glibc/stam$/.tools_compile_yynyyyyynyyyyynyynnyyyynyyyyyyyyyyyyyyynyynynnyyynny' failed”

    1. 详细错误如下: tools/Makefile:146: recipe for target '/home/jello/openwrt/staging_dir/target-aarch64_gen ...

  9. 数据库04_SQL简单实践

    笔试实例 以MySQL为例,制作一个简单的表profit如下: T1 分别求1991年以及1992年第一季度的金额总和,效果如下: sql语句实现: SELECT year, SUM(amount) ...

  10. Unity3D学习笔记(三十六):Shader着色器(3)- 光照

    光照模型:用数学的方法模拟现实世界中的光照效果.   场景中模型身上的光反射到相机中的光线: 1.漫反射:产生明暗效果 2.高光反射:产生镜面反射,物体中有最亮且比较耀眼的一部分 3.自发光: 4.环 ...