Circular Highway
Time limit1sMemory limit512 MB
Count starting stations on a circular road where buying all fuel and driving forward never empties the tank before returning.
- Level
Medium5 of 10
- Topics
- Prefix sum, Greedy, Array
- Solved
- No attempts yet
Problem
Taeyoung's home city has one circular highway. The highway holds gas stations numbered 1 through . Driving forward from station brings you to station , and driving forward from station brings you back to station 1.
Station sells a fixed amount of fuel , and the road from station to the next station burns units of fuel. Buying the fuel of every station gives exactly enough to go around the highway once, so the sum of the equals the sum of the .
Taeyoung picks one station and starts there with an empty tank. At every station he reaches, he buys all the fuel that station sells, then drives on to the next station. If the fuel runs out before he reaches the next station, the car stops where it is.

In the picture each of the three stations sells 2 units of fuel. The road from station 1 to station 2 and the road from station 2 to station 3 cost 1 unit each, and the road from station 3 to station 1 costs 4 units. Starting at station 1 completes the loop.
Count the starting stations that let the car go all the way around without stopping. If there is no such station, the count is 0.
Input
The first line holds the number of gas stations ().
The second line holds integers. The th integer is the amount of fuel () that station sells.
The third line holds integers. The th integer is the amount of fuel () burned on the road from station to station . Station is station 1.
The sum of the always equals the sum of the .
Output
Print on one line the number of starting stations from which the car goes around the highway without stopping.