//cogs [网络流24题] 太空飞行计划
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 1e9+7
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
queue <int> q;
const int mxn=100005;
int n,m,cnt,ans,t=10000;
int head[mxn],dis[mxn],get[mxn];
struct node {int to,next,flow;} f[mxn<<2];
inline void add(int u,int v,int flow)
{
f[++cnt].to=v,f[cnt].next=head[u],f[cnt].flow=flow,head[u]=cnt;
f[++cnt].to=u,f[cnt].next=head[v],f[cnt].flow=0,head[v]=cnt;
}
inline bool bfs()
{
int i,j,u,v,flow;
memset(dis,-1,sizeof dis);
q.push(0);
dis[0]=0;
while(!q.empty())
{
u=q.front();
q.pop();
for(i=head[u];i;i=f[i].next)
{
v=f[i].to,flow=f[i].flow;
if(dis[v]==-1 && flow>0)
dis[v]=dis[u]+1,q.push(v);
}
}
if(dis[t]>0) return 1;
return 0;
}
inline int find(int u,int low)
{
int v,i,j,a=0,sum=0,flow;
if(u==t) return low;
for(i=head[u];i;i=f[i].next)
{
v=f[i].to,flow=f[i].flow;
if(flow>0&& dis[v]==dis[u]+1 && (a=find(v,min(low-sum,flow))))
{
f[i].flow-=a;
if(i&1) f[i+1].flow+=a;
else f[i-1].flow+=a;
sum+=a;
}
}
if(!sum) dis[u]=-1;
return sum;
}
int main()
{
freopen("shuttle.in","r",stdin);
freopen("shuttle.out","w",stdout);
char ch;
int i,j,u,v,x,tans,flow,now,sum=0;
scanf("%d%d",&m,&n); //m个实验,n个仪器
fo(i,1,m) //辣鸡读入
{
scanf("%d",&x);
sum+=x;get[i]=x;
add(0,i,x);
ch=getchar();
while(ch!='\r' && ch!='\n')
{
now=0;
while(ch>='0'&&ch<='9')
{
x=ch-'0';
now=now*10+x;
ch=getchar();
}
add(i,now+300,inf);
if(ch=='\r' || ch=='\n') break;
ch=getchar();
}
}
fo(i,1,n)
{
scanf("%d",&x);
add(i+300,t,x);
}
while(bfs())
while(tans=find(0,0x7f7f))
ans+=tans;
bfs();
fo(u,1,m) if(dis[u]>0) printf("%d ",u);printf("\n");
fo(u,301,n+300) if(dis[u]>0) printf("%d ",u-300);printf("\n");
printf("%d\n",sum-ans);
return 0;
}
//2 3
//1 1 2
//25 2 3
//5 6 7