Under construction

Still being built. Things may change

Bumper to bumper traffic

Interview

Time limit4sMemory limit512 MB

Summary
Two cars 4.4 m long start stopped on a line and alternate between stopping and driving at 1 m/s at given times; decide whether they ever touch, giving the first contact second rounded up.
Level

Medium6 of 10

Topics
Implementation, Simulation, Math, Two pointers
Solved
No attempts yet

Problem

Rush hour moves at a crawl. At any moment each vehicle is either stopped or driving at the very slow speed of 1 meter per second. Cars now carry a simple black box that records every change in a car's speed. In this problem speeds change instantaneously.

The road is the real line, measured in meters, so a car is identified by its position on that line. Every car is 4.4 meters long.

You are given the initial positions of two cars driving along the line in the positive direction, together with a transcript of their speed changes. Decide whether the two cars ever collided. A collision at this speed is only a bumper tap, but it can corrupt the black box readings that follow, so the part of a transcript after a collision may not make sense.

Input

There is a single test case. The first line has two integers X1X_1 and X2X_2, the initial positions of the rears of the two cars in meters (0≤X1,X2≤1060 \le X_1, X_2 \le 10^6). Either X1+5≤X2X_1 + 5 \le X_2 or X2+5≤X1X_2 + 5 \le X_1 holds. At time 0 both cars are stopped.

The second line begins with N1N_1, the number of times the first car changed speed (0≤N1≤1050 \le N_1 \le 10^5), followed by N1N_1 integers T1<T2<⋯<TN1T_1 < T_2 < \cdots < T_{N_1} giving those times in seconds (0<Ti≤1060 < T_i \le 10^6). At time T1T_1 the first car starts driving at 1 meter per second, at time T2T_2 it stops, at time T3T_3 it starts again, and so on.

The third line begins with N2N_2 (0≤N2≤1050 \le N_2 \le 10^5), followed by N2N_2 integers T1′<T2′<⋯<TN2′T'_1 < T'_2 < \cdots < T'_{N_2} (0<Ti′≤1060 < T'_i \le 10^6) that describe the times the second car starts and stops.

Output

If the two cars collide, print bumper tap at time S on one line, where SS is the time in seconds from time 0 to the first contact, rounded up to a whole second. If they never collide, print safe and sound on one line.

Examples3

  1. Example 1

    Input
    0 5
    3 1 4 5
    3 1 4 6
    
    Expected output
    bumper tap at time 6
    
  2. Example 2

    Input
    10 0
    2 1 2
    1 1
    
    Expected output
    bumper tap at time 8
    
  3. Example 3

    Input
    2 13
    1 1
    3 4 7 10
    
    Expected output
    safe and sound