Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3062 Accepted: 1060

Description

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 highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.

Input

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

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

Sample Input

`6 41 2 3 42 3 1 44 2 3 13 1 2 41 3 4 21 4 2 32 1 3 2`

Sample Output

`2`

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.

#include <stdio.h>
#include
<string.h>
#include
<algorithm>
#include
<iostream>
#define Min(a, b) (a) < (b) ? a : b
#define Max(a, b) (a) > (b) ? a : b
using  namespace std;
const  int MAXN = 2005;
const  int MAXM = 210000;
const  int INF = 1100000000;
struct  Edge
{

int  st, ed;

int  next;

int  flow;

int cap;
}edge[MAXM];
void  add(int u, int v, int w)
{

//printf("add %d %d %d\n", u, v, w);
edge[E].flow = 0;
edge[E].cap
= w;
edge[E].st
= u;
edge[E].ed
= v;
edge[E].next
= E++;
edge[E].flow
= 0
edge[E].cap
= 0
edge[E].st
= v;
edge[E].ed
= u;
edge[E].next
= E++;
}
int  dinic_bfs(int src, int dest, int ver)
{

int i, j;

for (i = 0; i <= ver; i++)
{
level[i]
= -1;
}

int rear = 1;
que[
0= src; level[src] = 0;

for(i = 0; i < rear; i++
{

for(j = head[que[i]]; j != -1; j = edge[j].next)
{

if(level[edge[j].ed] == -1 && edge[j].cap > edge[j].flow)
{
level[edge[j].ed]
= level[que[i]]+1;
que[rear
++= edge[j].ed;
}
}
}

return  level[dest] >= 0;
}

int dinic_dfs(int src, int dest, int ver)
{

int stk[MAXN], top = 0;

int ret = 0, cur, ptr, pre[MAXN], minf, i;

int del[MAXN];

for (i = 0; i <= ver; i++
{
del[i]
= 0;
}
stk[top
++= src;
pre[src]
= src;
cur
= src;

while(top)
{

while(cur != dest && top)
{

for(i = head[cur]; i != -1; i = edge[i].next)
{

if(level[edge[i].ed] == level[cur] + 1 && edge[i].cap > edge[i].flow  && !del[edge[i].ed])
{
stk[top
++= edge[i].ed;
cur
= edge[i].ed;
pre[edge[i].ed]
= i;

break;
}
}

if(i == -1)
{
del[cur]
= 1;
top
--;

if(top) cur = stk[top-1];
}
}

if(cur == dest)
{
minf
= INF;

while(cur != src)
{
cur
= pre[cur];

if(edge[cur].cap - edge[cur].flow < minf) minf = edge[cur].cap - edge[cur].flow;
cur
= edge[cur].st;
}
cur
= dest;

while(cur != src)
{
cur
= pre[cur];
edge[cur].flow
+= minf;
edge[cur
^1].flow -= minf;

if(edge[cur].cap - edge[cur].flow == 0)
{
ptr
= edge[cur].st;
}
cur
= edge[cur].st;
}

while(top > 0&& stk[top-1!= ptr) top--;

if(top)  cur = stk[top-1];
ret
+= minf;
}
}

return ret;
}
int Dinic(int src, int dest, int ver)
{

int  ret = 0, t;

while(dinic_bfs(src, dest, ver))
{
t
= dinic_dfs(src, dest, ver);

if(t) ret += t;

else  break;
}

return ret;
}
int map[MAXN][30], vec[30];
void build (int l, int r, int n, int b)//用喜爱之在l跟r之间的边建图
{

int s = 0, t = n + b + 1, ver = t + 1;
E
= 0;

int i, j, x, y;

for (i = 0; i <= ver; i++)
{
= -1;
}

for (i = 1; i <= n; i++)
{

for (j = l; j <= r; j++)
{
+ n, INF);
}
}

for (i = 1; i <= n; i++)
{
1);
}

for (i = 1; i <= b; i++)
{
+ n, t, vec[i]);
}
}
int main()
{

int n, b, i, j;
scanf(
"%d%d"&n, &b);

for (i = 1; i <= n; i++)
{

for (j = 1;  j <= b; j++)
{
scanf(
"%d"&map[i][j]);
}
}

for (i = 1; i <= b; i++)
{
scanf(
"%d"&vec[i]);
}

int s = 0, t = n + b + 1, ver = t + 1, flow, sign;

int l = 0, r = b + 1;

while (l < r)//枚举差距
{
i
= (l + r) >> 1;

for (j = 1, sign = 0; j <= b - i + 1; j++)//暴力喜爱值起点
{
build (j, j
+ i - 1, n, b);
flow
= Dinic(0, t, ver);

if (flow == n)
{
sign
= 1;

break;
}
}

if (sign)
{
r
= i;
}

else
{
l
= i + 1;
}
}
printf(
"%d\n", r);

return 0;
}
/*
2 2
1 2
1 2
2 1

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
*/