2014
01-26

# The Mussels

"To be or not to be, that is the question." Now FJ( Frank&John) faces a serious problem.

FJ is breeding mussels these days. The mussels all want to be put into a culturist with a grade no little than their own grades. FJ accept their requirements.

FJ provides culturists of different grades, all having a certain capacity. FJ first put mussels into culturists with the same grade until they are full. Then he may put some mussels into some potential culturists that still have capacity.

Now, FJ wants to know how many mussels can be put into the culturists.

For each data set:
The first line contains two integers, n and m(0<n<=100000,0<m<=1000000), indicating the number of culturists and the number of mussels. The ith culturist has a grade i, and grade 1 is considered the highest.

The second line contains n integers indicating the culturists’ capacity in order.

The third line contains m integers all in the range 1~n, indicating the mussels’ grade in order.

Proceed to the end of file.

For each data set:
The first line contains two integers, n and m(0<n<=100000,0<m<=1000000), indicating the number of culturists and the number of mussels. The ith culturist has a grade i, and grade 1 is considered the highest.

The second line contains n integers indicating the culturists’ capacity in order.

The third line contains m integers all in the range 1~n, indicating the mussels’ grade in order.

Proceed to the end of file.

4 4
100 4 4 4
1 2 3 4
4 3
1 1 1 1
4 2 2

4
3

#include<iostream>
#include<cstdio>
#include<memory.h>
#include<iostream>
using namespace std;
const int MAX=1100005;
const int inf=1<<30;
struct node
{
int v,c,next;
}g[MAX+10000];
int s,t,vn,e,n,m,cnt;
{
}
int sap()
{
int i,u,v,flow=0,aug=inf,flag;
for(i=0;i<=vn;i++)
{
dis[i]=num[i]=0;
}
num[0]=vn;
pre[s]=u=s;
while(dis[s]<vn)
{
flag=0;
for(i=cur[u];i!=-1;i=g[i].next)
{
v=g[i].v;
if(g[i].c&&dis[u]==dis[v]+1)
{
flag=1;
pre[v]=u;
cur[u]=i;
aug=aug<g[i].c?aug:g[i].c;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
g[cur[u]].c-=aug;
g[cur[u]^1].c+=aug;
}
aug=inf;
}
break;
}
}
if(flag)
continue;
if(--num[dis[u]]==0)
break;
{
v=g[i].v;
if(g[i].c&&dis[v]<dis[u])
{
dis[u]=dis[v];
cur[u]=i;
}
}
dis[u]++;
num[dis[u]]++;
u=pre[u];
}
return flow;
}
int main()
{
int i,j,maxx=-1;
while(scanf("%d%d",&n,&m)!=EOF)
{
e=cnt=0;
t=0;
for(i=1;i<=n;i++)
{
scanf("%d",&j);
}
for(i=1;i<=m;i++)
{
scanf("%d",&j);
if(j>maxx)
maxx=j;
cnt++;
}
s=n+cnt+1;
vn=s+1;
for(i=1,cnt=0;i<=maxx;i++)
{
{
cnt++;
for(j=i;j>=1;j--)
}