STEAD  Steady Cow Assignment
Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.
FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.
Each cow gives FJ the order in which she prefers the barns. A cow's happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn's capacity is exceeded and the size of the range (i.e., one more than the positive difference between the the highestranked barn chosen and that lowestranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.
Input
Line 1: Two spaceseparated integers, N and B
Lines 2..N+1: Each line contains B spaceseparated 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 topchoice barn, the second integer on that line is the number of the i'th cow's secondchoice barn, and so on.
Line N+2: B spaceseparated 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
One integer, the size of the minimum range of barn rankings the cows give their assigned barns, including the endpoints
Example
Input: 6 4 1 2 3 4 2 3 1 4 4 2 3 1 3 1 2 4 1 3 4 2 1 4 2 3 2 1 3 2 Output: 2
hide comments
~!(*(@*!@^&:
20090408 03:26:08
Explanation of the sample:

Added by:  Nguyen Van Quang Huy 
Date:  20060216 
Time limit:  0.243s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: NODEJS PERL6 VB.NET 
Resource:  USACO FEB06 Gold Division 