Maximize Non-Negative Sum
EasyPractice library · +10 XPgreedyarrays
Given a list of N integers, you need to select some numbers such that their sum is maximized. You can only select non-negative numbers. If there are no non-negative numbers, select none and the sum is 0. Print the maximum possible sum.
Input
The first line contains an integer N. The second line contains N integers, separated by spaces.
Output
Print a single integer representing the maximum possible sum.
Constraints
1 <= N <= 1000 -1000 <= A_i <= 1000, where A_i is the i-th integer.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 1 2 -3 4 -5
Output
7
Sum of positive numbers: 1 + 2 + 4 = 7.
Example 2
Input
3 -1 -2 -3
Output
0
No positive numbers, so the maximum sum is 0.
Hints
Similar problems
- Smallest ElementEasy
- Count Elements Greater Than PreviousEasy
- Minimum Absolute DifferenceEasy
- Maximum Sum of Non-Adjacent Elements in a Circular ArrayMedium
- Minimum Operations to Make Array SortedMedium
- Longest Subarray with Constant DifferenceMedium
More topics: Arrays problems, Greedy problems
# 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
3 -1 -2 -3