题意:某*丝要在n天内给m个女神拍照,对于第 ii 个女神,这n天拍照的总数量不得小于 G\[i\]
*丝第 jj 天要给 $ c_j $ 个女神拍照,并且第 jj 天有一个拍照上限 DjD_j
而且这 cjc_j 个女神中的第 kk 个女神在今天的照片数量必须要在 LjkL_{jk}RjkR_{jk} 之间。问*丝满足这些条件并且能拍的最大数量的照片,如果不满足输出1-1

建图加上一条边e(et,st,0,+oo)e(et,st,0,+oo),然后按照无汇源上下界网络流建图,按sstsst,eeteet跑出的流量若不为所有du[i]>0du[i]>0之和,那么说明无解,否则说明有解,然后再按stst,etet为源汇跑流,最后输出反向边的流量加上这条边的下界为解,

//author: CHC
//First Edit Time: 2015-09-06 10:45
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+4;
const int MAXM=1e+6;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
struct Edge {
int to,ci,next;
Edge(){}
Edge(int _to,int _ci,int _next):to(_to),ci(_ci),next(_next){}
}e[MAXM];
int head[MAXN],tot;
int dis[MAXN],sta[MAXN],top,cur[MAXN];
int n,m;
inline void init(){
memset(head,-1,sizeof(head));
tot=0;
}
inline void AddEdge(int u,int v,int ci0,int ci1=0){
e[tot]=Edge(v,ci0,head[u]);
head[u]=tot++;
e[tot]=Edge(u,ci1,head[v]);
head[v]=tot++;
}
inline bool bfs(int st,int et){
memset(dis,0,sizeof(dis));
dis[st]=1;
queue <int> q;
q.push(st);
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=head[now];~i;i=e[i].next){
if(e[i].ci&&!dis[e[i].to]){
dis[e[i].to]=dis[now]+1;
q.push(e[i].to);
}
}
}
return dis[et]!=0;
}
LL Dinic(int st,int et){
LL ans=0;
while(bfs(st,et)){
top=0;
memcpy(cur,head,sizeof(head));
int u=st,i;
while(1){
if(u==et){
int pos,minn=INF;
for(i=0;i<top;i++){
if(minn>e[sta[i]].ci){
minn=e[sta[i]].ci;
pos=i;
}
}
for(i=0;i<top;i++){
e[sta[i]].ci-=minn;
e[sta[i]^1].ci+=minn;
}
top=pos;
u=e[sta[top]^1].to;
ans+=minn;
}
for(i=cur[u];~i;cur[u]=i=e[i].next)
if(e[i].ci&&dis[u]+1==dis[e[i].to])break;
if(cur[u]!=-1){
sta[top++]=cur[u];
u=e[cur[u]].to;
}
else {
if(top==0)break;
dis[u]=0;
u=e[sta[--top]^1].to;
}
}
}
return ans;
}
struct point {
int t,l,r;
}cs[1000000+100];
int du[MAXN],A[MAXN],num[1000000+100];
void print(int u,int v){printf("%d-->%d\n",u,v);}
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<m;i++)scanf("%d",&A[i]);
init();
memset(du,0,sizeof(du));
int st=n+m+2,et=n+m+3,ctot=0;
for(int i=1,x,y;i<=n;i++){
scanf("%d%d",&x,&y);
AddEdge(st,m+i,y);
for(int j=0;j<x;j++){
scanf("%d%d%d",&cs[ctot].t,&cs[ctot].l,&cs[ctot].r);
num[ctot]=tot;
AddEdge(m+i,cs[ctot].t,cs[ctot].r-cs[ctot].l);
du[m+i]-=cs[ctot].l;
du[cs[ctot].t]+=cs[ctot].l;
++ctot;
}
}
int sst=n+m+4,eet=n+m+5;
for(int i=0;i<m;i++){
AddEdge(i,et,INF-A[i]);
du[i]-=A[i];
du[et]+=A[i];
}
LL ntot=0;
//printf("sst:%d eet:%d\n",sst,eet);
for(int i=0;i<m;i++){
//AddEdge(sst,i,du[i]);ntot+=du[i];
if(du[i]>0)AddEdge(sst,i,du[i]),ntot+=du[i];//,print(sst,i);
else AddEdge(i,eet,-du[i]);//,print(i,eet);
}
for(int i=1;i<=n;i++){
//AddEdge(m+i,eet,-du[m+i]);
if(du[m+i]>0)AddEdge(sst,m+i,du[m+i]),ntot+=du[m+i];//,print(sst,m+i);
else AddEdge(m+i,eet,-du[m+i]);//,print(m+i,eet);
}
AddEdge(et,st,INF);
if(du[st]>0)AddEdge(sst,st,du[st]),ntot+=du[st];
else AddEdge(st,eet,-du[st]);
if(du[et]>0)AddEdge(sst,et,du[et]),ntot+=du[et];
else AddEdge(et,eet,-du[et]);

LL ans=Dinic(sst,eet);
//printf("%I64d %I64d\n",ntot,ans);
if(ans!=ntot)puts("-1");
else {
ans=Dinic(st,et);
printf("%lld\n",ans);
for(int i=0;i<ctot;i++){
printf("%d\n",e[num[i]^1].ci+cs[i].l);
}
}
puts("");
}
return 0;
}