1 #include <cstdio>
2 #include <cstring>
3 #include<queue>
4 #include <iostream>
5 #include <algorithm>
6 #define maxn 310
7 #define S 0
8 #define T (n+1)
9
10 using namespace std;
11
12 int ans=0,n,m;
13
14 namespace Min_Cost_Max_Flow
15 {
16 int head[maxn],from[maxn],dis[maxn],inq[maxn],cnt=1;
17
18 struct ss
19 {
20 int c;
21 int v;
22 int next;
23 int to;
24 int from;
25 }e[1001001];
26 void add(int u,int v,int w,int c)
27 {
28 e[++cnt].next=head[u];
29 e[cnt].v=w;
30 e[cnt].c=c;
31 e[cnt].from=u;
32 head[u]=cnt;
33 e[cnt].to=v;
34 }
35 void insert(int u,int v,int w,int c)
36 {
37 add(u,v,w,c);
38 add(v,u,0,-c);
39 }
40 bool spfa()
41 {
42 for (int i=1;i<=T;i++) dis[i]=0x7fffffff;
43 dis[S]=0;
44 inq[S]=0;
45 queue<int>que;
46 que.push(S);
47 while (!que.empty())
48 {
49 int now=que.front();
50 que.pop();
51 inq[now]=0;
52 for (int i=head[now];i;i=e[i].next)
53 if (e[i].v&&dis[e[i].to]>dis[now]+e[i].c)
54 {
55 dis[e[i].to]=dis[now]+e[i].c;
56 from[e[i].to]=i;
57 if (!inq[e[i].to]) inq[e[i].to]=1,que.push(e[i].to);
58 }
59 }
60 if (dis[T]==0x7fffffff) return 0;
61 return 1;
62 }
63 void mcf()
64 {
65 int x=0x7fffffff;
66 for (int i=from[T];i;i=from[e[i].from])
67 x=min(x,e[i].v);
68 for (int i=from[T];i;i=from[e[i].from])
69 {
70 e[i].v-=x;
71 e[i^1].v+=x;
72 ans+=x*e[i].c;
73 }
74 }
75 }
76 int main()
77 {
78 using namespace Min_Cost_Max_Flow;
79 scanf("%d",&n);
80 for (int i=1;i<=n;i++)
81 {
82 int k;
83 scanf("%d",&k);
84 insert(i,T,k,0);
85 for (int j=1;j<=k;j++)
86 {
87 int y,z;
88 scanf("%d%d",&y,&z);
89 insert(i,y,0x7fffffff,z);
90 insert(S,y,1,z);
91 }
92 if (i!=1) insert(i,1,0x7fffffff,0);
93 }
94 while (spfa()) mcf();
95 printf("%d\n",ans);
96 }