Cumulative Array Sum
EasyPractice library · +10 XPprefix sumsrange queries
You are given an array of N integers. You need to compute and print the cumulative sum of the array. The cumulative sum at index i (0-indexed) is the sum of all elements from index 0 up to and including index i.
Input
The first line contains an integer N (1 <= N <= 1000). The second line contains N space-separated integers a_0, a_1, ..., a_{N-1} (-1000 <= a_i <= 1000).
Output
Print N space-separated integers, where the i-th integer is the cumulative sum up to index i.
Constraints
1 <= N <= 1000 -1000 <= a_i <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 1 2 3 4 5
Output
1 3 6 10 15
The cumulative sums are 1, 1+2=3, 3+3=6, 6+4=10, 10+5=15.
Example 2
Input
4 10 -5 8 -3
Output
10 5 13 10
The cumulative sums are 10, 10-5=5, 5+8=13, 13-3=10.
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 10 -5 8 -3