#C14261. Count Zero Sum Sublists
Count Zero Sum Sublists
Count Zero Sum Sublists
Given a list of integers, your task is to count the number of contiguous sublists (subarrays) whose elements sum to zero.
Formally, let \( A = [a_1, a_2, \dots, a_n] \) be the given list. Define the prefix sum \( p_i = \sum_{j=1}^{i} a_j \) with \( p_0 = 0 \). A sublist \( A[i+1 \dots j] \) (with \( 0 \le i < j \le n \)) has a zero sum if and only if \( p_i = p_j \). The answer is the sum of \( \binom{k}{2} = \frac{k(k-1)}{2} \) for each distinct prefix sum value which appears \( k \) times.
Your program should read input from stdin
and print the result to stdout
.
inputFormat
The first line contains an integer n
which represents the number of elements in the list.
The second line contains n
space-separated integers representing the elements of the list.
outputFormat
Output a single integer, denoting the number of contiguous sublists that sum to zero.
## sample6
1 -1 2 -2 3 0
4
</p>