Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
998 B
3
Indexable
Never
#include <iostream>
#define N 31
using namespace std;
int n,m,l,k;
int a[N];
int b[N];
int c[N][N];
int d[N];
int dd[N];
int ans;
void doc()
{
	ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>b[i];
		cin>>c[i][0];
		for(int j=1;j<=c[i][0];j++)cin>>c[i][j];
	}
	cin>>l;
	for(int i=1;i<=l;i++)
		{
			cin>>d[i];
			ans+=a[d[i]];
	}

}
int x[N];

void backtrack(int u, int sum)
{
	if(u==1)sum=0;
	if(u==m+1)
	{
		
		for(int i=1;i<=n;i++)dd[i]=0;
		for(int i=1;i<=m;i++)if(x[i])
		{
			for(int j=1;j<=c[i][0];j++)dd[c[i][j]]=1;
		}
		for(int i=1;i<=l;i++)if(dd[d[i]]==0)sum+=a[d[i]];
		if(ans>sum)ans=sum;
		return;
	}
	x[u]=0;backtrack(u+1,sum);
	x[u]=1;
	if(sum>=ans)return;
	else 
		backtrack(u+1,sum+b[u]);

}
int main()
{
	freopen("input.txt","r",stdin);
	int TC;
	int k;
	cin>>TC;
	for(int tc=1;tc<=TC;tc++)
	{
		doc();
		backtrack(1,0);
		cout<<"#"<<tc<<" "<<ans<<"\n";
	}
	return 0;
}