"I hate buses!" Dragon raged at the 50th wrong answer he had recieved on
M2321 - Go to School By Bus.
Unfortunately for him, he goes to school by bus every day.
The bus route Dragon takes to school contains $N+1$ bus stops
where the first and last stations are stop $0$ and stop $N$ respectively.
There is a bus that goes from stop $0$ to stop $N$, and passengers can get on the bus at stop $i$ when the bus arrives at that stop.
It takes $t_i$ ($0 \le i \le N-1$) minutes to go from stop $i$ to stop $i+1$.
The following table shows the arrival time of the bus at each stop.
Bus Stop | Time (Minutes) |
---|---|
$0$ | $0$ |
$1$ | $t_0$ |
$2$ | $t_0+t_1$ |
$3$ | $t_0+t_1+t_2$ |
⋮ | ⋮ |
$N$ | $t_0+t_1+t_2+\dots+t_{N-1}$ |
Dragon is at stop $0$ initially and school is at stop $N$. As he hates taking the bus, he would rather
walk between adjacent stops instead of taking the bus. He needs $w_i$ minutes to walk from stop $i$ to stop $i+1$.
However, school starts exactly when the bus arrives at station $N$, and there is only one thing Dragon hates more than
taking the bus: being late for school.
Therefore, Dragon would like to arrive at stop $N$ earlier or at the same time as the bus does.
Dragon would like to minimize the total number of routes between adjacent stops the bus travels
while he is on the bus without being late for school.
So he may try to get off/on the bus at particular stops to do so.
Dragon may only get off/on the bus at stop $i$ exactly at the time the bus arrives at stop $i$.
Dragon may also wait at stops for the bus to arrive.
Note that getting off/on the bus takes negligible time, and the bus does not spend time waiting at bus stops.
Please help Dragon overcome his fear of buses by finding the minimum total number of routes between adjacent stops the bus travels while he is on the bus without being late for school.
Input
The first line of input contains an integer $N$, the number of bus stops.
The second line of input contains $N$ integers: $[t_0, t_1, \dots, t_{N-1}]$, the time the bus takes to go from stop $i$ to stop $i+1$.
The last line of input contains $N$ integers: $[w_0, w_1, \dots, w_{N-1}]$, the time Dragon takes to walk from stop $i$ to stop $i+1$.
Output
Output the minimum total number of routes between adjacent stops the bus travels while he is on the bus without being late for school.
Constraints
For all subtasks, $1\le N\le 3\times 10^5$, $1\le t_i,w_i\le 10^9$
Subtask 1 (5%): $t_i\ge w_i$
Subtask 2 (45%): $1\le N\le 1000$, $|t_i-w_i|\le 1$
Subtask 3 (20%): $1\le N\le 1000$
Subtask 4 (30%): No additional constraints
Sample Test Cases
Input | Output | |
---|---|---|
5 2 3 1 6 5 1 2 1 4 2 |
0 | |
Dragon does not need to take the bus to reach bus stop $N$ in time. | ||
8 3 3 3 3 3 3 3 3 2 2 5 5 2 2 5 5 |
2 | |
Dragon only needs to take the bus from bus stop $N-2$ to bus stop $N$. | ||
12 6 1 2 2 5 2 5 10 3 10 2 8 7 9 7 3 2 8 2 10 6 3 10 1 |
3 |
Scoring: Per Subtask
Authored by s19x17
Appeared in 2023 Mini Contest 3 (Pre-TFT)