Kolya got an integer array a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,…,an. The array can contain both positive and negative integers, but Kolya doesn’t like 0 0 0, so the array doesn’t contain any zeros.
Kolya doesn’t like that the sum of some subsegments of his array can be 0 0 0. 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 0 0 0. 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, 0 0 0, 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 0 0 0.
Input
The first line of the input contains one integer n ( 2 ≤ n ≤ 200000 ) n (2≤n≤200000) n(2≤n≤200000) — the number of elements in Kolya’s array.
The second line of the input contains n integers a 1 , a 2 , … , a n ( − 1 0 9 ≤ a i ≤ 1 0 9 , a i ≠ 0 ) a_1,a_2,…,a_n (−10^9≤a_i≤10^9,a_i≠0) a1,a2,…,an(−109≤ai≤109,ai=0) — the description of Kolya’s array.
Output
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 0 0 0.
Examples
input
4 1 -5 3 2output
1input
5 4 -2 3 -9 2output
0input
9 -1 1 -1 1 -1 1 1 -1 -1output
6input
8 16 -5 -11 -15 10 5 4 -4output
3Note
Consider the first example. There is only one subsegment with the sum 0 0 0. 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 1 1 1 between second and third elements of the array.
There are no subsegments having sum 0 0 0 in the second example so you don’t need to do anything.
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; typedef long long ll; map<ll, int> ma; ll sum[maxn]; inline void solve() { int n; scanf("%d", &n); for (int i = 1, x; i <= n; i++) { scanf("%d", &x); sum[i] = sum[i - 1] + x; } int ans = 0; for (int i = n; i >= 0; i--) { if (!ma.count(sum[i])) ma[sum[i]] = 1; else { ma.clear(); ans++; i += 2; } } printf("%d\n", ans); } int main() { int T = 1; //scanf("%d", &T); while (T--) { solve(); } }