Putnik
Time limit2sMemory limit1024 MB
Choose the stations where the express Putnik stops so that the total number of passengers who switch to it is as large as possible.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Statement
Huge numbers of people ride commuter trains every day. This problem covers the line from the station "Zhilye Mytishchi" (ZhM for short) to the station "Bolshaya Tsentralnaya" (BTs for short). The line has stations. Station ZhM has number , and station BTs has number . Stations and are connected by a track segment for every , and no other pairs of stations are connected. Every train starts at ZhM and goes to BTs. Station has passengers each day who want to reach BTs, and there are no other passengers. An ordinary train needs minutes to cover the segment between station and station . Passengers board instantly. The board of directors has decided to run an express from the ZhM depot to BTs. The new train is called "Putnik". Each segment has its own track for Putnik, and it takes Putnik minutes to travel from station to station . As often happens in practice, can be less than or greater than it. Putnik is an express, so each stop takes one minute for the attendants to give a safety briefing on high-speed trains. Passengers follow a simple rule. They switch to the new express only if it makes their travel time from their station to station strictly smaller. A passenger boards Putnik at the very start of its stop, so the passenger spends that minute on the greeting and the briefing. The board wants to maximize the number of people who ride the new train. Choose the stations where the express stops so that as many people as possible ride it. Putnik starts from the ZhM depot, but it does not have to stop at ZhM.
Input
The first line contains (), the number of segments between ZhM and BTs. The second line contains integers (), the minutes an ordinary train needs to travel from station to station . The third line contains integers (), the number of people each day who try to get from station to station . The fourth line contains integers (), the minutes Putnik needs to travel the segment between stations and .
Output
Print one integer, the maximum possible number of passengers who ride the new express.
Hints
In the first sample, Putnik reaches BTs one minute faster than the ordinary train if it makes no stops. However, if it stops anywhere, that advantage disappears and nobody rides the new train. In the second sample, the express must stop at stations and . It cannot stop at station , because that would make the trips from station unprofitable.