题解:UVA1025 A Spy in the Metro

original title

translate

Topic

A city subway is linear, with n (2≤n≤50) stations, numbered 1~n from left to right. There are M1 trains that start from the first station to the right, and M2 trains that start from the nth station to the left. The time required for a train to travel between adjacent platforms is fixed because all trains travel at the same speed. At time 0, Mario departs from station 1 to meet a spy at station n at time T (0≤T≤200). It is easy to be caught when waiting for the train at the station, so she decided to try to hide from the moving train and keep the waiting time at the station as short as possible. The stopping time of the train at the station is negligible, and Mario is agile. Even if two trains with different directions arrive at the station at the same time, Mario can also complete the transfer.

input format

The input file contains several cases, each of which contains the following 7 lines:

The first line is a positive integer n, which means that there are n stations. The second line is T, which means that Mario sees a spy at station n at time T. The third line has n-1 integers t1,t2,...,tn- 1, where ti represents the travel time of the subway from station i to i+1. The fourth row is M1, and the number of trains heading to the right from the first station. The fifth row contains M1 positive integers a1, a2,...,aM1, That is, the departure time of each train is M2 in the sixth row, and the number of trains leaving from the first station to the right

The last case ends with a line of 0s.

output format

There are several lines, each line first outputs "Case Number XXX: " (XXX is the case number, starting from 1), and then outputs the minimum waiting time or "impossible" (no solution).

Sample Input

4

55

5 10 15

4
0 5 10 20
4
0 5 10 15
4
18
1 2 3
5
0 3 6 10 12
6
0 3 5 7 12 15
2
30
20
1
20
7
1 3 5 7 11 13 17
0

Sample Output

Case Number 1: 5

Case Number 2: 0

Case Number 3: impossible

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324612945&siteId=291194637