1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #define maxn 105
7 #define maxm 5105
8 using namespace std;
9 int n,m,a,b,tim[maxn],sum[maxn],flag[maxn];
10 double c,ans;
11 int tmp,best[maxn],bel[maxn],idx,vis[maxn];
12 struct Graph{
13 int tot,now[maxn],fa[maxm],son[maxm],pre[maxm];
14 double val[maxm];
15 void init(){tot=0;memset(now,0,sizeof(now));}
16 void put(int a,int b,double c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c,fa[tot]=a;}
17 void prepare(){
18 for (int u=0;u<=n;u++) if (!flag[u])
19 for (int p=now[u],v=son[p];p;p=pre[p],v=son[p])
20 if (best[v]==-1||val[best[v]]>val[p]) best[v]=p;
21 memset(vis,-1,sizeof(vis));
22 tmp=n,vis[0]=0;
23 for (int i=1;i<=n;i++) if (!flag[i]&&vis[i]==-1){
24 int u,v; ++idx;
25 for (u=i;vis[u]==-1;u=fa[best[u]]) vis[u]=idx;
26 if (vis[u]<idx) continue;
27 v=u,bel[u]=++tmp,vis[v]=0;
28 for (v=fa[best[v]];vis[v]==idx;v=fa[best[v]]) vis[v]=0,bel[v]=tmp;
29 }
30 }
31 }G[2];
32 void work(){
33 for (int t=1;;t^=1){
34 for (int i=0;i<=n;i++) best[i]=-1;
35 G[t].prepare(),G[t^1].init();
36 if (tmp==n){
37 for (int i=1;i<=n;i++) if (!flag[i]) ans+=G[t].val[best[i]];
38 break;
39 }
40 for (int u=0;u<=n;u++) if (!flag[u]){
41 for (int p=G[t].now[u],v=G[t].son[p];p;p=G[t].pre[p],v=G[t].son[p]){
42 if (bel[u]&&bel[v]&&bel[u]==bel[v]) continue;
43 int a;
44 if (bel[u]) a=bel[u]; else a=u;
45 if (bel[v]) G[t^1].put(a,bel[v],G[t].val[p]-G[t].val[best[v]]);
46 else G[t^1].put(a,v,G[t].val[p]);
47 }
48 if (bel[u]) ans+=G[t].val[best[u]],flag[u]=1;
49 }
50 n=tmp;
51 }
52 printf("%.2lf\n",ans);
53 }
54 double low[maxn];
55 int main(){
56 scanf("%d",&n);
57 for (int i=1;i<=n;i++){
58 scanf("%lf%d",&c,&tim[i]);
59 if (tim[i]) G[1].put(0,i,c),low[i]=c; else flag[i]=1;
60 }
61 scanf("%d",&m);
62 for (int i=1;i<=m;i++){
63 scanf("%d%d%lf",&a,&b,&c);
64 if (flag[a]||flag[b]) continue;
65 low[b]=min(low[b],c);
66 G[1].put(a,b,c);
67 }
68 for (int i=1;i<=n;i++) if (!flag[i]) ans+=low[i]*(tim[i]-1);
69 work();
70 return 0;
71 }