#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
template <class T> inline T read(const T sample) {
T x=0; int f=1; char s;
while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x*f;
}
ll n,m;
string s;
int hd[1009],cur[1009],dep[1009],tot=1,ss,tt,ans;
bitset<1009> vis;
struct Edge{
int to,nxt,v;
}e[(int)2e5+9];
void add(int u,int v,int w){
e[++tot]={v,hd[u],w},hd[u]=tot;
e[++tot]={u,hd[v],0},hd[v]=tot;
}
void dfs(int u){
vis[u]=1;
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!vis[e[eg].to]&&e[eg].v>0)dfs(e[eg].to);
}
}
bool bfs(int st,int en){
for(int i=0;i<=n+m+2;++i)dep[i]=0;
queue<int>q;
memcpy(cur,hd,sizeof(hd));
q.push(st);
dep[st]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!dep[e[eg].to]&&e[eg].v>0){
dep[e[eg].to]=dep[u]+1;
q.push(e[eg].to);
}
}
}
return !!dep[en];
}
int dfs(int u,int en,int flow){
if(u==en)return flow;
int r=flow;
for(int eg=cur[u];eg&&r;eg=e[eg].nxt){
cur[u]=eg;
if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){
int c=dfs(e[eg].to,en,min(r,e[eg].v));
r-=c;
e[eg].v-=c,e[eg^1].v+=c;
}
}
return flow-r;
}
int dinic(int st,int en){
int ret=0;
while(bfs(st,en)){
ret+=dfs(st,en,0x3f3f3f3f);
}
return ret;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>m>>n;
ss=n+m+1,tt=ss+1;
//while(getchar()!='\n');
getline(cin,s);
for(int i=1;i<=m;++i){
vi v;
getline(cin,s);
ll x=0,f=1,lst=0;
for(auto it:s){
if(lst){
if(it>='0'&&it<='9')x=(x<<1)+(x<<3)+(it^48),lst=1;
else {
v.push_back(f*x);
x=0,f=1,lst=0;
}
}else{
if(it=='-')f=-1,lst=1;
else if(it>='0'&&it<='9')x=(x<<1)+(x<<3)+(it^48),lst=1;
else lst=0;
}
}
if(lst)v.push_back(f*x);
add(ss,i,v[0]);
ans+=v[0];
for(int j=1;j<v.size();++j)add(i,m+v[j],0x3f3f3f3f);
}
for(int i=1,x;i<=n;++i){
cin>>x;
add(m+i,tt,x);
}
ans-=dinic(ss,tt);
dfs(ss);
f(i,1,m)if(vis[i])cout<<i<<" ";
cout<<"\n";
f(i,m+1,n+m)if(vis[i])cout<<i-m<<' ';
cout<<"\n";
cout<<ans<<"\n";
return 0;
}