This page is still under construction.

Parts of this page are still being built. What you see may change.

Buses

Time limit1sMemory limit128 MB

Summary
Given bus timetables at every stop, pick an outbound bus and an inbound bus to minimize John's total waiting time before his friend arrives, returning by the friend's arrival time.
Level

Medium7 of 10

Topics
Sorting, Binary search, Array, Implementation
Solved
No attempts yet

Problem

John arrives at a bus depot early to meet a friend. The friend will arrive later, and it is cold outside, so instead of waiting the whole time John wants to ride a bus a few stops down the route and take another bus back to the depot before his friend shows up.

He wants to minimize the total time he spends waiting, which is the sum of three parts:

  • the time he waits at the depot before boarding the outbound bus,
  • the time he waits to change buses at the stop where he turns around, and
  • the time he waits for his friend after returning to the depot.

John is punctual and must not be late: he has to be back at the depot no later than his friend's arrival time. Write a program that reads the arrival times and the bus timetable and prints the shortest possible total waiting time.

Input

The first line contains five integers t1t_1, t2t_2, mm, n1n_1, n2n_2 separated by single spaces, where 0≤t1≤t2≤1090 \le t_1 \le t_2 \le 10^9, 2≤m≤10002 \le m \le 1000, n1,n2≥1n_1, n_2 \ge 1, and m×(n1+n2)≤106m \times (n_1 + n_2) \le 10^6:

  • t1t_1: the moment John arrives at the depot,
  • t2t_2: the moment his friend arrives at the depot,
  • mm: the number of stops on the route, including the depot,
  • n1n_1: the number of buses that depart from the depot,
  • n2n_2: the number of buses that arrive at the depot.

The next mm lines give the timetable, one line per stop, in order: stop 11 is the depot, then stops 2,…,m2, \dots, m. Outbound buses run 1→2→⋯→m1 \to 2 \to \dots \to m; inbound buses run m→⋯→2→1m \to \dots \to 2 \to 1. Each stop's line has n1+n2n_1 + n_2 integers xijx_{ij} with 0≤xij≤1090 \le x_{ij} \le 10^9: the first n1n_1 values are the times of the n1n_1 outbound buses at that stop, and the remaining n2n_2 values are the times of the n2n_2 inbound buses at that stop.

Every bus needs at least one time unit to travel between two consecutive stops. John boards a bus at a stop only if he is present at that bus's time for the stop. Changing from one bus to another at a stop, where the two buses are there at times tat_a and tbt_b, is possible only if ta≤tbt_a \le t_b.

Output

Print a single integer: the minimum total time John has to spend waiting. If no pair of buses lets him make the round trip and return by his friend's arrival, he waits at the depot the whole time, so the answer is t2−t1t_2 - t_1.

Note

In the example, the optimal plan is: board the outbound bus at the depot at time 00, get off at stop 22, board the inbound bus at time 44, and arrive back at the depot at time 99. The waiting time is 00 at the depot, 11 to change buses, and 11 for the friend, which is 22 in total.

Examples1

  1. Example 1

    Input
    0 10 3 1 2
    0 9 10
    3 4 8
    4 3 7
    
    Expected output
    2