#161Medium
Good subarrays
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
You are given an array of digits from to . A subarray is good if the sum of its elements equals its length: . Count the good subarrays.
Input format
The first line contains (). The second line contains a string of digits (no spaces).
Output format
A single number — the number of good subarrays.
Examples
Input
3 120
Output
3
Input
5 11011
Output
6
Note
Subtract from every element: . Now the condition is , that is, two prefixes are equal: . Count how many times every prefix value occurs — a value seen times gives pairs. The prefixes lie in , so a shifted array works instead of a dictionary.
In the first example 1, 20 and 120 are good. If all digits are 1, the answer is — a 64-bit type is needed.
Source: Codeforces, problem 1398C.
