A maintenance pipeline has n joints in a row, numbered 0 to n-1, and joint i requires exactly seal[i] units of sealant to be fully sealed. Two robots start sealing the pipeline at the same time from opposite ends: Robot A starts at joint 0 and works rightward (increasing index), while Robot B starts at joint n-1 and works leftward (decreasing index). Robot A carries a canister with capacity capA units of sealant, and Robot B carries one with capacity capB units; both canisters start completely full.
Each robot advances one joint at a time, in lockstep with the other:
If the two robots would seal the very same joint at the same moment (this happens exactly when n is odd and both reach the middle joint together), no refill occurs at that joint even if one or both canisters are running low: whichever robot currently holds strictly more sealant seals that joint alone (ties are broken in favor of Robot A), and that robot's canister decreases by the joint's requirement -- the other robot's canister is left untouched. If n is even, the two robots simply meet in the middle and each finishes their own half without ever sharing a joint.
Determine the total number of refills performed by both robots combined.
Line 1 contains a single integer n.
Line 2 contains n space-separated integers, seal[0] ... seal[n-1].
Line 3 contains two space-separated integers capA and capB.
Print a single integer: the total number of refills performed by both robots combined.
Example 1
Input
4 2 2 3 3 5 5
Expected
1
Explanation
Robot A seals joint 0 (needs 2, canister 5->3, no refill) then joint 1 (needs 2, canister 3->1, no refill). Robot B seals joint 3 (needs 3, canister 5->2, no refill) then joint 2 (needs 3, but only 2 remains, so it refills to 5, then drops to 2). n=4 is even, so there is no shared middle joint. Total refills: 1.
Example 2
Input
3 4 4 3 5 6
Expected
1
Explanation
Robot A seals joint 0 (needs 4, canister 5->1, no refill). Robot B seals joint 2 (needs 3, canister 6->3, no refill). n=3 is odd, so they meet at joint 1: Robot A has 1 left and Robot B has 3 left, so Robot B (strictly more) seals it; it needs 4 but only 3 remain, so Robot B refills to 6 and then drops to 2. Total refills: 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →