CF BUDDY
← Problems·

1426D · Non-zero Segments

1500 · constructive algorithms, data structures, greedy

Problem: Kolya got an integer array a1,a2,,ana_1, a_2, \dots, a_n. The array can contain both positive and negative integers, but Kolya doesn't like 00, so the array doesn't contain any zeros.

Kolya doesn't like that the sum of some subsegments of his array can be 00. The subsegment is some consecutive segment of elements of the array.

You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum 00. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, 00, any by absolute value, even such a huge that they can't be represented in most standard programming languages).

Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum 00.

Input Format: The first line of the input contains one integer nn (2n2000002 \le n \le 200\,000) — the number of elements in Kolya's array.

The second line of the input contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (109ai109,ai0-10^{9} \le a_i \le 10^{9}, a_i \neq 0) — the description of Kolya's array.

Output Format: Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum 00.

Note: Consider the first example. There is only one subsegment with the sum 00. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer 11 between second and third elements of the array.

There are no subsegments having sum 00 in the second example so you don't need to do anything.

Sample Cases

Case 1

Input

4
1 -5 3 2

Output

1

Case 2

Input

5
4 -2 3 -9 2

Output

0

Case 3

Input

9
-1 1 -1 1 -1 1 1 -1 -1

Output

6

Case 4

Input

8
16 -5 -11 -15 10 5 4 -4

Output

3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback