zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    记Git报错-Everything up-to-date
    开发相关的绝赞网站和框架
    记Git报错-refusing to merge unrelated histories
    Nodejs和npm入门使用
    【转】什么是JavaScript
    CSS学习摘要-定位实例
    golang 文件服务器
    golang postgresql CRUD
    golang sqlite3 CRUD
    psql: 致命错误: 对用户"user1"的对等认证失败
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6721467.html
Copyright © 2011-2022 走看看