Maximum Odd Sum
EasyPractice library · +10 XPgreedyiterationbasic
Given a list of N integers, find the maximum possible sum you can obtain by selecting any subset of these numbers, with the condition that the sum must be odd. If no subset can produce an odd sum, output 0.
Input
The first line contains an integer N. The second line contains N space-separated integers.
Output
Print a single integer, the maximum odd sum achievable. If no odd sum is possible, print 0.
Constraints
1 <= N <= 1000 -1000 <= array element <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests
Example 1
Input
5 1 2 3 4 5
Output
15
The sum of all positive numbers (1+2+3+4+5=15) is odd. So, the maximum odd sum is 15.
Example 2
Input
4 2 4 6 8
Output
0
All numbers are even. No subset can produce an odd sum. Output is 0.
Example 3
Input
3 -1 -3 -5
Output
0
The sum of positive numbers is 0. The largest negative odd is -1. The maximum odd sum is -1.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 3 4 5
Example 2 input
4 2 4 6 8
Example 3 input
3 -1 -3 -5