P2857 [USACO06FEB] Steady Cow Assignment G

Description

Farmer John's N (1

Input Format

Line 1: Two space-separated integers, N and B Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i's top-choice barn, the second integer on that line is the number of the i'th cow's second-choice barn, and so on. Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.

Output Format

Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints.

Explanation/Hint

Explanation of the sample: Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.