• Educational Codeforces Round 19 B


    Description

    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
    Note

    In the first example sum of the second and the fourth elements is 3.

    题意:求子序列和最大为奇数是多少

    解法:奇数和偶数相加减的关系

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <queue>
     6 #include <stack>
     7 #include <map>
     8 #include <cstring>
     9 #include <climits>
    10 #include <cmath>
    11 
    12 using namespace std;
    13 
    14 const int maxn = 200010;
    15 long long a[maxn],b[maxn],c[maxn],dp[maxn];
    16 long long m,n;
    17 long long sum;
    18 long long Min=(1e6);
    19 long long Minn=(1e6);
    20 int main()
    21 {
    22     cin>>n;
    23     for(int i=1;i<=n;i++)
    24     {
    25         cin>>a[i];
    26         if(a[i]>=0)
    27         {
    28             sum+=a[i];
    29             if(a[i]%2)
    30             {
    31                 Minn=min(Minn,a[i]);
    32             }
    33         }
    34         else
    35         {
    36             long long ans=abs(a[i]);
    37             if(ans%2)
    38             {
    39                 Min=min(ans,Min);
    40              //   cout<<Min<<endl;
    41             }
    42         }
    43     }
    44     if(sum%2)
    45     {
    46         cout<<sum<<endl;
    47     }
    48     else
    49     {
    50         cout<<max(sum-Min,sum-Minn)<<endl;
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    8.图形软件开发
    7.GDI绘图技术
    15.MFC网络通信
    JavaWeb:基于MVC设计模式的一个小案例(一)
    在虚拟机里连接PLC S7-200
    mark-又重新回到博客园
    早起的奇迹
    STM32-cJSON库的打包和解析
    Copley-STM32串口+CANopen实现双电机力矩同步
    DataStructure-链表实现指数非递减一元多项式的求和
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6721467.html
Copyright © 2020-2023  润新知