#include #include #include #include #include using namespace std; #define REP(i, n) for(int(i)=0;(i)<(n);++(i)) void rc(int v,int mn,int mx){if(v &v){ set s; s.insert(i); while(1){ i = S[i]; if(s.count(i)){ v.insert(v.end(), s.begin(), s.end()); return i; } s.insert(i); } } int main(){ cin >> N; rc(N,1,100); REP(i,N){ cin >> L[i] >> S[i]; rc(L[i],1,100); rc(S[i],1,N); L[i]*=2; S[i]--; } int total = 0; REP(i,N){ if(f[i]) continue; vector v; int ret = check(i, v); if(ret != i){ total += L[i]/2; f[i] = true; } else { int s = v.size(); int minv = INF; REP(j,s){ total += L[v[j]]/2; minv = min(minv, L[v[j]]); f[v[j]] = true; } total += minv/2; } } cout << fixed << setprecision(1) << total/2.0 << endl; return 0; }