#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int INF=0x3fff3fff;
const int MAXN=1000010;
const int maxn=2010;
struct EDGE
{
int to,next,flow;
}edge[MAXN];
int head[maxn],cnt;
int pre[maxn];
int cur[maxn];
int level[maxn];
int gap[maxn];
int s,t,ss,tt,personsum,sum;
int w[maxn],id[MAXN],l[MAXN];//id用于记录可行流答案在第几条边
inline void addedge(int u,int v,int flow)
{
edge[cnt].to=v;
edge[cnt].flow=flow;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].flow=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
void build_graph(int n,int m)
{
s=n+m+1,t=s+1; //1----n,n+1----m+n
ss=t+1,tt=ss+1;
personsum=0,sum=0,cnt=0;
memset(head,-1,sizeof(head));
memset(w,0,sizeof(w));
//以下为建图
for(int i=1;i<=m;i++)//n+i
{
int g;
scanf("%d",&g);
w[n+i]-=g;
w[t]+=g;
addedge(n+i,t,INF-g);
}
for(int i=1;i<=n;i++)
{
int c,d,x,g,h;
scanf("%d %d",&c,&d);
w[s]-=0;
w[i]+=0;
addedge(s,i,d);
while(c--)
{
scanf("%d %d %d",&x,&g,&h);
w[x+1+n]+=g;
w[i]-=g;
addedge(i,x+1+n,h-g);
l[personsum]=g;
id[personsum++]=cnt-1;
}
}
for(int i=1;i<=t;i++)
if(w[i]>0)
addedge(ss,i,w[i]);
else if(w[i]<0)
addedge(i,tt,-w[i]),sum+=-w[i];
addedge(t,s,INF);//变成无源汇
}
int sap(int start,int end,int tot)
{
memset(gap,0,sizeof(gap));
memset(level,0,sizeof(level));
memset(pre,0,sizeof(pre));
gap[0]=tot;
for(int i=1;i<=tot;i++)
cur[i]=head[i];
int u=start,ans=0,inf=INF;
while(level[start]<tot)
{
bool flag=0;
for(int &i=cur[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].flow&&level[u]==level[v]+1)
{
flag=1;
inf=min(edge[i].flow,inf);
pre[v]=u;
u=v;
if(u==end)
{
while(u!=start)
{
u=pre[u];
edge[cur[u]].flow-=inf;
edge[cur[u]^1].flow+=inf;
}
ans+=inf;
inf=INF;
}
break;
}
}
if(!flag)
{
if(--gap[level[u]]==0)
break;
int minn=tot;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].flow&&level[v]<minn)
{
minn=level[v];
cur[u]=i;
}
}
level[u]=minn+1;
gap[minn+1]++;
if(u!=start)
u=pre[u];
}
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
build_graph(n,m);
if(sum!=sap(ss,tt,tt))
puts("-1");
else
{
int ans=sap(s,t,tt);//解可行流大小
printf("%d\n",ans);
for(int i=0;i<personsum;i++)
printf("%d\n",l[i]+edge[id[i]].flow);
}
puts("");
}
return 0;
}
/*
2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 0 3
1 3 6
2 6 9
ans:
36
9
6
3
3
6
9
*/