{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"Farmer John\u0027s N (1 \u0026lt;\u003d N \u0026lt;\u003d 1000) cows each reside in one of B (1 \u0026lt;\u003d B \u0026lt;\u003d 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.\r\u003cbr\u003e\r\u003cbr\u003eFJ 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.\r\u003cbr\u003e\r\u003cbr\u003eEach cow gives FJ the order in which she prefers the barns. A cow\u0027s 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\u0027s capacity is exceeded and the size of the range (i.e., one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible."}},{"title":"Input","value":{"format":"HTML","content":"Line 1: Two space-separated integers, N and B\r\u003cbr\u003e\r\u003cbr\u003eLines 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\u0027s top-choice barn, the second integer on that line is the number of the i\u0027th cow\u0027s second-choice barn, and so on.\r\u003cbr\u003e\r\u003cbr\u003eLine 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."}},{"title":"Output","value":{"format":"HTML","content":"Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints."}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e6 4\r\n1 2 3 4\r\n2 3 1 4\r\n4 2 3 1\r\n3 1 2 4\r\n1 3 4 2\r\n1 4 2 3\r\n2 1 3 2\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Hint","value":{"format":"HTML","content":"Explanation of the sample:\r\u003cbr\u003e\r\u003cbr\u003eEach 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."}}]}