#include using namespace std; #define REP(i,a,b) for(i=a;i'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);} void reader(int *x, int *y){reader(x);reader(y);} void* setDirectEdge(int N, int M, int A[], int B[], int **es, int ***edge, void *workMemory){int i;*es=(int*)workMemory;*edge=(int**)((*es)+N);(*edge)[0]=(int*)((*edge)+N);rep(i,N)(*es)[i]=0;rep(i,M)(*es)[A[i]]++;REP(i,1,N)(*edge)[i]=(*edge)[i-1]+(*es)[i-1];workMemory=(void*)((*edge)[N-1]+(*es)[N-1]);rep(i,N)(*es)[i]=0;rep(i,M)(*edge)[A[i]][(*es)[A[i]]++]=B[i];return workMemory;} void* getReverseGraph(int N, int *es, int **edge, int **r_es, int ***r_edge, void *workMemory){int i,j,k;*r_es=(int*)workMemory;*r_edge=(int**)((*r_es)+N);(*r_edge)[0]=(int*)((*r_edge)+N);rep(i,N)(*r_es)[i] = 0;rep(i,N)rep(j,es[i])(*r_es)[edge[i][j]]++;REP(i,1,N)(*r_edge)[i]=(*r_edge)[i-1]+(*r_es)[i-1];workMemory=(void*)((*r_edge)[N-1]+(*r_es)[N-1]);rep(i,N)(*r_es)[i]=0;rep(i,N)rep(j,es[i])k=edge[i][j],(*r_edge)[k][(*r_es)[k]++]=i;return workMemory;} int StronglyConnectedComponentsDFS(int *es, int **edge, int num[], int st, int mx){int i,j;num[st]=-2;rep(i,es[st]){j=edge[st][i];if(num[j]==-1)mx=StronglyConnectedComponentsDFS(es,edge,num,j,mx);}num[st]=mx;return mx+1;} int StronglyConnectedComponents(int N, int *es, int **edge, int res[], void *workMemory){int i,j,k,t=0,*s,**e,*q,z,*n,*r;workMemory=getReverseGraph(N,es,edge,&s,&e,workMemory);q=(int*)workMemory;n=q+N;r=n+N;rep(i,N)res[i]=n[i]=-1;k=0;rep(i,N)if(n[i]==-1)k=StronglyConnectedComponentsDFS(es,edge,n,i,k);rep(i,N)r[n[i]]=i;for(k=N-1;k>=0;k--){i=r[k];if(res[i]>=0)continue;res[i]=t;z=0;q[z++]=i;while(z){i=q[--z];rep(j,s[i])if(res[e[i][j]]==-1)res[e[i][j]]=t,q[z++]=e[i][j];}t++;}return t;} int N; int A[222], B[222], L[222]; int com[222], deg[222], mn[222]; int main(){ int i, j, k, nn; int res; int *es, **edge; void *mem = malloc(10000000); reader(&N); rep(i,N) reader(L+i,A+i), A[i]--, B[i]=i; mem = setDirectEdge(N, N, A, B, &es, &edge, mem); nn = StronglyConnectedComponents(N, es, edge, com, mem); res = 0; rep(i,N) res += L[i]; rep(i,N) if(com[A[i]] != com[B[i]]) deg[com[B[i]]]++; rep(i,nn) mn[i] = 10000; rep(i,N) mn[com[i]] = min(mn[com[i]], L[i]); rep(i,nn) if(!deg[i]) res += mn[i]; printf("%.1f\n",res/2.0); return 0; }