RoBa, an undergraduate student, is preparing for his thesis. He collects many papers about artificial intelligence, the field that he is very interested in. However, he doesn't know how to start his work. You know at the tail of every paper, there is a list of reference papers, describing the relative work of other researchers. So these papers may form a very complicated net. Now, RoBa turns to you for help.
RoBa has given every paper an interesting value (which can be negative). A greater value indicates a more interesting paper. He wants to select a paper set S, such that, for every paper in this set, all the papers in its reference list are also contained in the set S. And what's more, he wants to make the sum of all the interesting value in the set maximum.
Input
There are multiple test cases in the input. The first line of each test case contains an integer N,(N ≤ 100) indicating the amount of papers. Then N lines followed. Each line contains two integers Vi (|Vi| ≤ 10,000) and Pi at first. Vi indicating the interesting value of the i-th paper, Pi indicating the amount of reference papers of the i-th paper. Then Pi numbers followed, indicating all the reference papers. The papers are numbered from 1 to N.
The input is terminated with N = 0.
Output
If the maximum possible sum is greater than zero, output two lines for each test case. The first line contains two integers, the maximum sum of interesting value, and the amount of papers in the selected set. The next line contains all the papers, separated by space. If there are more than one valid set to get the maximum sum, anyone will be OK. Please note the set cannot be empty.
If the maximum possible sum is no more than zero, you should only output one line says "Refused" instead, which means RoBa will refuse to do this research.
Sample Input
4 -10 1 2 10 2 3 4 -3 0 -3 0 4 -10 1 2 -10 2 3 4 -3 0 -3 0 0
Sample Output
4 3 2 3 4 Refused
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=55010;
const int M=950000;
const int inf=(1<<28);
int head[N];
struct Edge
{
int v,next,w;
} edge[M];
int cnt,n,s,t;//n从0开始 0->n-1
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int sap()
{
int pre[N],cur[N],dis[N],gap[N];
int flow=0,aug=inf,u;
bool flag;
for(int i=0; i<n; i++)
{
cur[i]=head[i];
gap[i]=dis[i]=0;
}
gap[s]=n;
u=pre[s]=s;
while(dis[s]<n)
{
flag=0;
for(int &j=cur[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[u]==dis[v]+1)
{
flag=1;
if(edge[j].w<aug) aug=edge[j].w;
pre[v]=u;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
edge[cur[u]].w-=aug;
edge[cur[u]^1].w+=aug;
}
aug=inf;
}
break;
}
}
if(flag) continue;
int mindis=n;
for(int j=head[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[v]<mindis)
{
mindis=dis[v];
cur[u]=j;
}
}
if((--gap[dis[u]])==0)
break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return flow;
}
//初始化 cnt=0;memset(head,-1,sizeof(head));
int vis[N],a[N],ln;
void dfs(int u)
{
vis[u]=1;
a[ln++]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(vis[edge[i].v]||edge[i].w==0) continue;
dfs(edge[i].v);
}
}
int main()
{
int m;
while(scanf("%d",&m)==1&&m)
{
cnt=0;
memset(head,-1,sizeof(head));
n=m+2;
s=0,t=n-1;
int sum=0;
for(int i=1;i<=m;i++)
{
int cost;scanf("%d",&cost);
if(cost>0)
{
sum+=cost;
addedge(s,i,cost);
}
else addedge(i,t,-cost);
int p;scanf("%d",&p);
while(p--)
{
int b;scanf("%d",&b);
addedge(i,b,inf);
}
}
int mc=sap();
int maxp=sum-mc;
if(maxp==0)
{
printf("Refused\n");
continue;
}
ln=0;
memset(vis,0,sizeof(vis));
dfs(0);sort(a,a+ln);
printf("%d %d\n",maxp,ln-1);
printf("%d",a[1]);
for(int i=2;i<ln;i++) printf(" %d",a[i]);printf("\n");
}
return 0;
}