zoukankan      html  css  js  c++  java
  • Odd sum

    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
    Copy
    4
    -2 2 -3 1
    output
    Copy
    3
    input
    Copy
    3
    2 -5 -3
    output
    Copy
    -1
    Note

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

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <unordered_set>
    #include <unordered_map>
    //#include <xfunctional>
    #define ll long long
    #define mod 1000000007
    using namespace std;
    int dir[4][2] = { { 0,1 },{ 0,-1 },{ -1,0 },{ 1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    
    int main()
    {
        int n,sum=0;
        cin >> n;
        vector<int> a(n,0);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] > 0)
                sum += a[i];
        }
        sort(a.begin(), a.end());
        if (sum > 0)
        {
            if (sum % 2)
                cout << sum;
            else
            {
                int pos = lower_bound(a.begin(), a.end(), 0)-a.begin();
                int i=pos,j=pos-1;
                for (; i < n && a[i] % 2==0; i++);
                for (; j >=0 && a[j] % 2==0; j--);
                if (j<0)
                {
                    cout << sum - a[i];
                    return 0;
                }
                if (i>=n)
                {
                    cout << sum + a[j];
                    return 0;
                }
                if (a[j] % 2 && a[i] % 2)
                {
                    if (a[i] + a[j] > 0)
                        cout << sum + a[j];
                    else
                        cout << sum - a[i];
                }
            }
        }
        else
        {
            for(int i=a.size()-1;i>=0;i--)
                if (a[i] < 0 && a[i]%2)
                {
                    cout << a[i];
                    break;
                }
        }
        return 0;
    }
  • 相关阅读:
    [Wap] 制作自定义WmlListAdapter来实现Mobile.List控件的各种效果
    [EntLib]UAB(Updater Application Block)下载
    jarhoo是一个很棒的地方
    [GoogleMap]利用GoogleMap地图的这个应用真是太狠了[1]
    [J2ME] VideoCoolala(MobileWebCam)开源说明
    [p2p]手机是否可以通过JXTA网络与PC机/PocketPC/WindowsMobile实现P2P呢?
    Android Layout XML属性
    什么是9.png
    android主流UI布局
    Android开发之旅: Intents和Intent Filters(理论部分)
  • 原文地址:https://www.cnblogs.com/dealer/p/12443721.html
Copyright © 2011-2022 走看看