Joe runs along two river banks with obstacles that force crossings; find the earliest time he intercepts a drifting bottle, or report impossibility.
Medium7Shortest pathGraphNo attempts yetTime limit2sMemory limit512 MBJoe is on a multi-day tramping trip. While he is wading across a river, his empty water bottle falls out of his pack and drifts off downstream. He watches it for a while, decides it is worth chasing, returns to the left bank and puts his pack down. Making up his mind and getting back to the left bank takes 2 minutes, and the river flows at a steady 0.5 metres per second, so the bottle is already 60 metres downstream when he starts to run.
Take the moment Joe starts running as time 0 and the point where the bottle fell as distance 0. At time t seconds the bottle is at 60+0.5t metres.
On the left bank Joe runs at 2 metres per second. The right bank is rougher and he runs at 1 metre per second there. Both banks carry obstacles such as cliff faces that he cannot pass. When he meets one he has to cross to the opposite bank. He always crosses on a line perpendicular to the current, so a crossing carries him no distance downstream, and a crossing from one bank to the other takes 40 seconds.
An obstacle given by d and l covers its own bank from d to d+l. Joe can stand at any point of his bank that is not strictly inside an obstacle, and the two end points d and d+l are usable. He runs only through such points, and he crosses the river only at a point that is usable on both banks. So if an obstacle on one bank ends exactly where an obstacle on the other bank begins, Joe crosses at that point. If one point lies strictly inside an obstacle on both banks at once, Joe cannot get past it.
To pick the bottle up, Joe wades from the point where he stands into the centre of the river. Reaching the centre takes 20 seconds and carries him no distance downstream, so entering at point p at time T puts him in the centre of the river at p at time T+20. If the bottle has already passed p by then, he misses it. If it has not, he waits there and takes it the instant it reaches p.
Compute the earliest time at which Joe picks up his water bottle.
The input holds a single test case.
The first line has two integers nl and nr, the number of obstacles on the left bank and on the right bank (0≤nl≤2000, 0≤nr≤2000).
Each of the next nl lines has two integers d and l describing a left-bank obstacle (0<d≤6000, 0<l≤6000). Here d is the distance downstream from where the bottle was dropped to the start of the obstacle and l is the length of the obstacle, both in metres.
The following nr lines describe the right-bank obstacles in the same format.
On each bank the obstacles are given in increasing order of d and do not overlap, though they may touch.
Print the earliest time in seconds at which Joe picks up his bottle, rounded to exactly three digits after the decimal point. The exact value is always an integer multiple of 31 of a second, so the rounded value is unique. For an exact value of 6632 seconds, print 66.667.
If Joe cannot pick the bottle up, print IMPOSSIBLE.