797B - Odd sum - CodeForces Solution


dp greedy implementation *1400

Please click on ads to support us..

Python Code:

n = int(input())
arr = list(map(int, input().split()))

totalSum = 0
for i in arr:
    if (i > 0):
        totalSum += i

if (totalSum % 2 == 0):
    minOdd = 10**9
    for i in arr:
        if (abs(i) % 2 == 1 and abs(i) < minOdd):
            minOdd = abs(i)

    totalSum -= minOdd
print(totalSum)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

void solution() {
    int n;
    cin >> n;
    long long int arr[n];
    for (int i=0; i<n; i++)
        cin >> arr[i];
    sort(arr, arr+n);
    long long int sum = 0;
    long long int lastOdd = INT_MAX;
    for (int i=n-1; i>=0; i--) {
        if (arr[i]%2) lastOdd = min(lastOdd, abs(arr[i]));
        if (arr[i] > 0)
            sum += arr[i];
    }
    if (sum%2 == 0)
        sum -= lastOdd;
    cout << sum << endl;
}


int main() {
    solution();
}

	  		  	 	  			  	   			  		 		


Comments

Submit
0 Comments
More Questions

1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals