#158Medium
Number of ways
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
In how many ways can an array of integers be cut into three non-empty blocks of consecutive elements with equal sums? In other words, count the pairs , , such that .
Input format
The first line contains (). The second line contains integers ().
Output format
A single number — the number of ways.
Examples
Input
5 1 2 3 0 3
Output
2
Input
4 0 1 -1 0
Output
1
Note
If the total is not divisible by , the answer is . Otherwise the first cut is where and the second where . Move from left to right and add the number of earlier prefixes with — .
For both conditions coincide — the cuts must not fall at the same place. If all numbers are zero, the answer is , up to — a 64-bit type is needed.
Source: Codeforces, problem 466C.
