PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000451. Cuộc chiến chống Mammoth (Against Mammoths)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cuộc chiến chống Mammoth (Against Mammoths)

Against Mammoths

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

In a fictional interplanetary war, humans control HH planets and their opponents control AA. Human planet ii starts with nin_i spaceships and produces pip_i more each year. Opponent planet jj starts with mjm_j mammoths and produces qjq_j more annually. At the start of year tt, starting with year 0, a planet has its initial force plus tt times its annual production.

Assign exactly one human planet to each opponent planet. Each human planet may attack at most one opponent planet. If planet ii launches at the beginning of year tt, all its ships arrive at planet jj at the start of year t+dijt+d_{ij}. Mammoths continue to be produced during the journey. The attack succeeds when the number of ships is at least the number of mammoths upon arrival. Different planets may launch in different years.

Find the earliest year by which every opponent planet can be defeated, or report that it is impossible.

Input

The first line contains H,AH,A. The second contains 2H2H integers n1,p1,…,nH,pHn_1,p_1,\ldots,n_H,p_H. The third contains 2A2A integers m1,q1,…,mA,qAm_1,q_1,\ldots,m_A,q_A. The following HH lines each contain AA integers di,1,…,di,Ad_{i,1},\ldots,d_{i,A}.

Output

Print the earliest year as an integer, or IMPOSSIBLE if no assignment works.

Subtasks

  • Subtask 1 (20 points): 1≤H,A≤81\le H,A\le8, 0≤ni,pi,mj,qj≤400000\le n_i,p_i,m_j,q_j\le40000, 1≤dij≤400001\le d_{ij}\le40000, 1≤i≤H1\le i\le H, 1≤j≤A1\le j\le A.
  • Subtask 2 (30 points): 1≤H,A≤401\le H,A\le40, 0≤ni,pi,mj,qj≤400000\le n_i,p_i,m_j,q_j\le40000, 1≤dij≤400001\le d_{ij}\le40000, 1≤i≤H1\le i\le H, 1≤j≤A1\le j\le A.
  • Subtask 3 (50 points): 1≤H,A≤2501\le H,A\le250, 0≤ni,pi,mj,qj≤400000\le n_i,p_i,m_j,q_j\le40000, 1≤dij≤400001\le d_{ij}\le40000, 1≤i≤H1\le i\le H, 1≤j≤A1\le j\le A.

Examples

Input 1

2 1
2 3 0 3
2 2
2
2

Output 1

6

Explanation 1

Planet 1 leaves in year 4 with 14 ships and arrives in year 6 to face 14 mammoths. Neither human planet can win before year 6.