Under construction

Still being built. Things may change

Topical

Interview

Time limit1sMemory limit1024 MB

Summary
Given knowledge requirements and gains for n modules across k topics, find the maximum number of modules Benson can complete in some order.
Level

Medium5 of 10

Topics
Greedy, Sorting, Array
Solved
No attempts yet

Statement

Benson the Rabbit is attending pilot school.

He has nn modules to complete, numbered from 11 to nn. There are kk topics in flying, numbered from 11 to kk. Benson is new to flying, so his knowledge starts at zero in every topic.

Each module has a knowledge requirement. To complete module ii, Benson needs at least r[i][j]r[i][j] knowledge in each topic jj. Completing module ii gives him u[i][j]u[i][j] more knowledge in each topic jj.

Formally, let p[j]p[j] be Benson's knowledge in topic jj. Initially, p[j]=0p[j] = 0 for all jj. He can complete module ii only if p[j]≥r[i][j]p[j] \ge r[i][j] for every topic jj. After he completes module ii, p[j]p[j] increases by u[i][j]u[i][j] for each topic jj.

Benson may complete the modules in any order, but he may complete each module at most once. Find the maximum number of modules he can complete.

Input

The first line contains two space-separated integers nn and kk.

The next nn lines describe the requirements. The ii-th of these lines contains kk space-separated integers r[i][1],r[i][2],…,r[i][k]r[i][1], r[i][2], \dots, r[i][k].

The following nn lines describe the gains. The ii-th of these lines contains kk space-separated integers u[i][1],u[i][2],…,u[i][k]u[i][1], u[i][2], \dots, u[i][k].

Output

Print one integer, the maximum number of modules Benson can complete.

Constraints

1≤n,k≤1061 \le n, k \le 10^6

n⋅k≤106n \cdot k \le 10^6

0≤u[i][j],r[i][j]≤1090 \le u[i][j], r[i][j] \le 10^9 for all 1≤i≤n1 \le i \le n and 1≤j≤k1 \le j \le k.

Examples3

  1. Example 1

    Input
    3 3
    0 0 0
    7 9 2
    7 8 9
    7 8 2
    7 7 7
    8 10 9
    
    Expected output
    1
    
  2. Example 2

    Input
    4 3
    5 1 0
    0 1 5
    0 0 0
    7 7 7
    0 5 6
    1 1 1
    8 2 0
    8 1 4
    
    Expected output
    4
    
  3. Example 3

    Input
    5 5
    14 11 15 7 15
    0 0 0 0 0
    9 9 14 2 13
    4 3 6 1 0
    2 4 7 0 0
    5 5 0 0 13
    4 4 7 1 0
    4 1 0 2 1
    2 5 0 2 1
    4 0 7 2 12
    
    Expected output
    4