建图跑最小割,加当前弧优化。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define N 3000
#define inf 0x3f3f3f3f
#define M 4000005
using namespace std;
int n,m;
int tot;
int head[N],nxt[M],ver[M],f[M];
void add(int a,int b,int c)
{
tot++;nxt[tot]=head[a];head[a]=tot;ver[tot]=b;f[tot]=c;
tot++;nxt[tot]=head[b];head[b]=tot;ver[tot]=a;f[tot]=;
}
int S,T;
int ch[N];int cur[N];
int zeng(int a,int b)
{
if(a==T)return b;
int r=;
for(int i=cur[a];i&&b>r;i=nxt[i])
{
if(ch[ver[i]]==ch[a]+&&f[i])
{
int t=zeng(ver[i],min(b-r,f[i]));
r+=t;f[i]-=t;f[i^]+=t;
if(f[i]>)cur[a]=i;
}
}
if(!r)ch[a]=-;
return r;
}
bool tell()
{
memset(ch,-,sizeof(ch));
ch[S]=;
queue<int>q;
q.push(S);
while(!q.empty())
{
int tmp=q.front();q.pop();
for(int i=head[tmp];i;i=nxt[i])
{
if(f[i]&&ch[ver[i]]==-)
{
ch[ver[i]]=ch[tmp]+;
q.push(ver[i]);
}
}
}
return ch[T]!=-;
}
int dinic()
{
int r=,t;
while(tell())
{
for(int i=;i<=T;i++)cur[i]=head[i];
while(t=zeng(S,inf))r+=t;
}
return r;
}
int v[N];
int ans;
int main()
{
tot=;
scanf("%d%d",&n,&m);int t1,t2;
S=;T=n+m+;
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d%d",&v[i],&tmp);ans+=v[i];
add(i+m,T,v[i]);
for(int j=;j<=tmp;j++)
{
scanf("%d%d",&t1,&t2);
add(t1,i+m,t2);
}
}
for(int i=;i<=m;i++)
{
scanf("%d",&t1);
add(S,i,t1);
}
printf("%d\n",ans-dinic());
return ;
}