Problem list
Log in
#icpc00391 · ICPC GCPC 2025

Bustling Busride

Platinum I 5.0 s 2048 MB

The university of Bithampton is served by exactly one bus line. On its way to the city centre, it serves several stops at which students may exit. Every student has a fixed bus stop where they want to exit.

It is Friday afternoon, $4$ PM, and as always, all the students want to go home, leading to quite a long queue at the bus stop. Fortunately, the bus line is served in regular intervals, with the first bus arriving at $4$ PM.

Whenever a bus arrives at the university, everyone in the queue tries to enter the bus, which makes the buses very crowded. This has led to numerous complaints where people tried to exit buses but were unable to because of the sheer amount of people. As a consequence, the bus company decided that at every stop which someone in the bus has as destination, everyone in the bus must exit it. Those who want to travel further enter again. For every time a passenger enters or exits the bus, the bus needs to wait $w$ seconds.

To offer the best service, the bus company wants to minimize the maximum time it takes anybody from 4 PM until they reach their destination. For each bus, the bus driver can decide how many people from the front of the queue enter the bus. The number of people that can enter a bus is unlimited. Help the bus drivers make the optimal decisions to achieve the company's goal.

Input

The input consists of:

  • One line with four integers $n$, $b$, $r$, and $w$ ($1\leq n, b \leq 10^5$, $1\leq r, w \leq 10^6$), the number of passengers, the number of bus stops, the time between the arrival of two buses at the university, and the delay for exiting and entering.
  • One line with (b) integers (d_{i}) ((1 \le d_{i}, \sum d_{i} \leq 10^6)), the travel time from the ((i-1))th bus stop to the (i)th bus stop (the (0)th bus stop is the university).
  • One line with (n) integers (t_{i}) ((1 \leq t_{i} \leq b)), indicating that the destination of the (i)th person in line is the $t_i$th bus stop.

All times are given in seconds.

Output

Output the minimum number of seconds until every person in line has exited at their destination.

Example 1

Input
3 3 20 1
2 2 2
2 3 1
Output
18

Example 2

Input
4 2 1 10
3 2
1 2 2 1
Output
27

Example 3

Input
5 3 3 3
2 2 1
3 3 2 1 1
Output
17
Source
ICPC GCPC 2025
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code