#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x>N; FOR(i,N) cin>>L[i]>>S[i], L[i]*=2, S[i]--; int ret=0; FOR(i,N) if(vis[i]!=2) { FOR(j,N) if(vis[j]!=2) vis[j]=0; x=dfs(S[i],i); if(x>=0) ret += L[x], done[x]=1; } FOR(i,N) { FOR(j,N) if(done[S[j]] && done[j]==0) done[j]=1, ret+=L[j]/2; } cout << ret*0.5 << endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); solve(); return 0; }