• 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 }
  • 相关阅读:
    把你的名字刻到IE上
    C#格式化字符串
    CEO、COO、CFO、CTO、CIO
    Net通用分页(可以选择页码的显示,且有中英选择)
    laravelmiddleware中间件常用使用方法
    laravel路由组中namespace的的用法详解
    qq自己设定动态图像视屏
    laravel实现excel表的导入导出功能
    git创建远程分支并推送
    jquery如何用Ajax将信息遍历到界面上
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6721467.html
Copyright © 2020-2023  润新知