Inchworm Cut
InterviewTime limit1sMemory limit1024 MB
Count the ways to split an array into head, chest, and abdomen segments whose sums satisfy head < abdomen < chest.
- Level
Medium5 of 10
- Topics
- Prefix sum, Two pointers, Array
- Solved
- No attempts yet
Problem
An inchworm is made of a one-dimensional array of positive integers. Because it is an insect, its body can be divided into a head, a chest, and an abdomen.
Each part is a contiguous segment of the array. The head is on the left, the chest is in the middle, and the abdomen is on the right. The size of each part is the sum of the values in its segment.
Muji knows that the chest is larger than the abdomen and the abdomen is larger than the head. However, he does not know where the head and chest are separated, or where the chest and abdomen are separated. Help Muji by counting the number of ways the inchworm can be divided. Specifically, count the pairs that satisfy
with .

Input
The first line contains the integer . ()
The second line contains the values separated by spaces. ()
Output
Print the number of ways the inchworm's head, chest, and abdomen can be separated.