Sum of Positive Numbers
EasyPractice library · +10 XPgreedyloops
Given N integers, compute the sum of only the positive integers among them. If there are no positive integers, the sum is 0.
Input
The first line contains an integer N (1 <= N <= 1000). The second line contains N space-separated integers.
Output
Print a single integer: the sum of the positive integers.
Constraints
1 <= N <= 1000. Each integer is between -1000 and 1000.
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
8
The positive numbers are 1, 3, and 4. Their sum is 1 + 3 + 4 = 8.
Example 2
Input
3 -1 -5 -2
Output
0
There are no positive numbers, so the sum is 0.
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
3 -1 -5 -2