1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 500+100
14 #define maxm 250000
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=n;i++)
19 #define for1(i,n) for(int i=1;i<=n;i++)
20 #define for2(i,x,y) for(int i=x;i<=y;i++)
21 using namespace std;
22 inline int read()
23 {
24 int x=0,f=1;char ch=getchar();
25 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
26 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
27 return x*f;
28 }
29 ll n,m,k,tot=1,s,t,a[maxn],b[maxn],head[maxn],q[maxm],from[2*maxm];
30 ll mincost,d[maxn];
31 bool v[maxn];
32 struct edge{int from,next,go;ll v,c;}e[2*maxm];
33 void ins(int x,int y,int z,int w)
34 {
35 e[++tot].go=y;e[tot].from=x;e[tot].v=z;e[tot].c=w;e[tot].next=head[x];head[x]=tot;
36 }
37 void insert(int x,int y,int z,int w)
38 {
39 ins(x,y,z,w);ins(y,x,0,-w);
40 }
41 bool spfa()
42 {
43 for (int i=s;i<=t;i++){v[i]=0;d[i]=inf;}
44 int l=0,r=1,y;q[1]=s;d[s]=0;v[0]=1;
45 while(l!=r)
46 {
47 int x=q[++l];if(l==maxn)l=0;v[x]=0;
48 for (int i=head[x];i;i=e[i].next)
49 if(e[i].v&&d[x]+e[i].c<d[y=e[i].go])
50 {
51 d[y]=d[x]+e[i].c;from[y]=i;
52 if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn)r=0;}
53 }
54 }
55 return d[t]!=inf;
56 }
57 void mcf()
58 {
59 while(spfa())
60 {
61 ll tmp=inf;
62 for(int i=from[t];i;i=from[e[i].from]) tmp=min(tmp,e[i].v);
63 mincost+=(ll)d[t]*tmp;
64 for(int i=from[t];i;i=from[e[i].from]){e[i].v-=tmp;e[i^1].v+=tmp;}
65 }
66 }
67 int main()
68 {
69 freopen("input.txt","r",stdin);
70 freopen("output.txt","w",stdout);
71 n=read();m=read();
72 s=0;t=m+n+1;
73 int x;
74 for1(i,m)x=read(),insert(s,i,x,0);
75 for1(i,n)
76 for1(j,m)
77 {
78 x=read();if(x)insert(j,m+i,inf,0);
79 }
80 for1(i,n)
81 {
82 x=read();
83 a[0]=0;
84 for1(j,x)a[j]=read();
85 for1(j,x+1)b[j]=read();
86 for1(j,x)insert(m+i,t,a[j]-a[j-1],b[j]);
87 insert(m+i,t,inf,b[x+1]);
88 }
89 mcf();
90 printf("%lld\n",mincost);
91 return 0;
92 }