Reversible Lane

Time limit1sMemory limit128 MB

Summary
Simulate traffic queues on a reversible bridge lane over all possible switch times to find the switch time minimizing total waiting cars.
Level

Medium5 of 10

Topics
Simulation, Prefix sum, Brute force
Solved
No attempts yet

Problem

A new bridge crosses a river between two busy communities. The bridge is the bottleneck: it has nn lanes in total, shared by both directions, while the approach roads on each side are wider.

Traffic is not symmetric. In the morning most cars travel from the left bank to the right bank; in the evening most travel from the right bank to the left bank. The bridge is therefore configured with n1n_1 lanes permanently reserved for left-to-right traffic, n2n_2 lanes permanently reserved for right-to-left traffic, and one central reversible lane, so that n1+1+n2=nn_1 + 1 + n_2 = n. In the morning the central lane is open for left-to-right traffic; at some moment it is switched to right-to-left traffic. Your task is to choose the best moment to switch it.

The day is split into equal time intervals, chosen so that a single lane lets exactly one car begin crossing per interval. Intervals are numbered from 11 in the morning to mm in the evening. For each interval you are given how many cars arrive at the bridge on the left bank and on the right bank.

During each interval, and in each direction, three things happen in this order:

  1. New cars arrive at the bridge.
  2. Cars begin crossing in their direction, as many as there are lanes currently open for that direction (one car per open lane).
  3. Cars that cannot start crossing wait in the queue for the next interval.

No new cars arrive after interval mm. If cars are still waiting after interval mm, further intervals are simulated in the same way (with no arrivals) until every car has started crossing.

Reversing the central lane is not instantaneous: it must be cleared before traffic can use it in the other direction, so it stays closed for rr intervals. If the switch is triggered at interval tt (with 1≤t≤m1 \le t \le m), then the open lanes are:

  • before interval tt: n1+1n_1 + 1 lanes left-to-right and n2n_2 lanes right-to-left;
  • from interval tt through interval t+r−1t + r - 1 (inclusive): n1n_1 lanes left-to-right and n2n_2 lanes right-to-left (the central lane is closed);
  • from interval t+rt + r onward: n1n_1 lanes left-to-right and n2+1n_2 + 1 lanes right-to-left.

The total waiting time is the sum, over every interval and both directions, of the number of cars still waiting in the queue at the end of the interval (the count from step 3). Choose the interval tt (with 1≤t≤m1 \le t \le m) that minimizes the total waiting time. If several intervals achieve the minimum, report the earliest one.

Input

The first line contains four integers n1n_1, n2n_2, mm, and rr, where 1≤n1,n2≤101 \le n_1, n_2 \le 10, 1≤m≤1000001 \le m \le 100000, and 1≤r≤m1 \le r \le m.

Each of the following mm lines describes one interval, in order from 11 to mm, with two integers: the number of cars arriving on the left bank and the number arriving on the right bank during that interval. At most 100100 cars arrive on each bank during each interval.

Output

Output a single integer tt — the earliest interval at which switching the central lane minimizes the total waiting time.

Explanation

The table below traces the model on the first example, using the optimal switch interval t=4t = 4. For each interval it lists the lanes open in each direction, the cars that arrive (step 1), the cars that begin crossing (step 2), and the cars left waiting (step 3). The total waiting time is 2020 intervals — 1010 for left-to-right cars and 1010 for right-to-left cars. Interval 1111 is shown to make clear that nothing is waiting from that point on.

time1234567891011
left-to-right lanes33322222222
left-to-right cars12343210100
left-to-right cross12322222100
left-to-right queue00023320000
right-to-left lanes22222333333
right-to-left cars01223353210
right-to-left cross01222333330
right-to-left queue00001133200

Examples2

  1. Example 1

    Input
    2 2 10 2
    1 0
    2 1
    3 2
    4 2
    3 3
    2 3
    1 5
    0 3
    1 2
    0 1
    
    Expected output
    4
    
  2. Example 2

    Input
    3 3 12 2
    10 0
    10 0
    10 0
    8 0
    5 2
    2 5
    0 8
    0 10
    0 10
    0 10
    0 6
    0 3
    
    Expected output
    4