Against Mammoths

Time limit1sMemory limit128 MB

Summary
Assign each human planet to at most one alien planet and pick a launch year so the fleet wins on arrival, minimizing the year the last alien falls.
Level

Hard8 of 10

Topics
Binary search, Greedy, Sorting, Math
Solved
No attempts yet

Problem

In the year 3024, humanity finally developed the technology to challenge the alien races. Massive warships called Saber Tooth ships, as powerful as the aliens' defending mammoths, could now be built. Humans ruled several planets, and so did the aliens. Using Saber Tooth ships, humanity fought and won the first Planet War in history. Your task is to simulate that ancient war to test a few historical hypotheses.

Each human planet builds ships at its own constant rate. The production rate of a planet is the number of ships it builds per year. Every planet also starts with some ships before the simulation begins. Counting years from 00, a planet that starts with nn ships and has production rate pp owns n+pn + p ships at the start of year 11, and n+i×pn + i \times p ships at the start of year ii.

Commander Bradley Bennett fixes a strategy: for every alien planet he picks one human planet, lets it build ships, and at a chosen moment sends all of that planet's ships to invade the alien planet. No alien planet is attacked by two human planets, and no human planet attacks two alien planets.

Each alien planet is defended by mammoths. It starts with some mammoths and breeds more every year at its own production rate, so at the start of year ii it holds (initial mammoths) + i×(rate)+\, i \times (\text{rate}) mammoths. When ships meet mammoths, the larger army wins; if the two counts are equal, the ships win. If the ships win, the alien planet is destroyed.

Ships take time to travel. The travel time, in whole years, between every human planet and every alien planet is given. A fleet may leave only at the start of a year (right after that year's ships are produced) and arrives only at the start of a year (right after that year's mammoths are produced). So if a fleet leaves a human planet at the start of year LL and the travel time is tt, it arrives at the start of year L+tL + t carrying n+L×pn + L \times p ships and faces (initial mammoths) + (L+t)×(rate)+\,(L + t) \times (\text{rate}) mammoths.

For example, a human planet with 22 initial ships and production rate 33 attacks an alien planet with 22 initial mammoths and production rate 22; the travel time is 22 years and the fleet is ordered to leave at year 11. Then 2+3×1=52 + 3 \times 1 = 5 ships depart, and on arrival at year 33 they face 2+2×3=82 + 2 \times 3 = 8 mammoths, so the fleet is destroyed.

Bennett wants a plan that destroys every alien planet as early as possible; that is, he minimizes the year in which the last alien planet falls. Output that earliest possible year.

Input

The input contains several test cases. The first line of each test case has two integers HH and AA: the number of human and alien planets respectively (each between 11 and 250250).

The second line has HH pairs of non-negative integers n1 m1 n2 m2…nH mHn_1\ m_1\ n_2\ m_2 \dots n_H\ m_H, where nin_i is the initial number of ships and mim_i is the production rate of the ii-th human planet.

The third line has AA pairs of non-negative integers in the same format, giving the initial number of mammoths and the production rate of each alien planet.

Then follow HH lines, each with AA positive integers; the jj-th number on the ii-th line is the travel time in years from the ii-th human planet to the jj-th alien planet.

The input ends with a line containing two zeros. Every number other than HH and AA is between 00 and 4000040000.

Output

For each test case, print a single line: the minimum number of years in which all alien planets can be destroyed. If destroying them all is impossible, print IMPOSSIBLE instead.

Examples3

  1. Example 1

    Input
    2 1
    2 3 0 3
    2 2
    2
    2
    0 0
    
    Expected output
    6
    
  2. Example 2

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

    Input
    2 2
    5 3 0 3
    2 2 2 2
    2 5
    2 3
    0 0
    
    Expected output
    11