#include using namespace std; using ll = long long; using vi = vector; using vb = vector; using vd = vector; using vl = vector; using vvi = vector; using vvb = vector; using vvd = vector; using vvl = vector; #define REP(i,n) for(ll i=0; i<(n); ++i) #define FOR(i,b,n) for(ll i=(b); i<(n); ++i) #define ALL(v) (v).begin(), (v).end() using Graph = vvi; vvi scc(Graph & g, Graph & rg) { ll n = g.size(); vb used(n, false); function dfs = [&](int v, vi & order) { if (used[v]) return; used[v] = true; for (auto &e : g[v]) dfs(e, order); order.push_back(v); }; vi order; { REP(i, n) dfs(i, order); reverse(ALL(order)); } vvi cmp; { g.swap(rg); used = vb(n, false); REP(i, n) if (!used[order[i]]) { cmp.push_back(vi()); dfs(order[i], cmp.back()); } g.swap(rg); } return cmp; } Graph create_rg(const Graph & g) { Graph rg(g.size()); REP(from, g.size()) for (auto& to : g[from]) rg[to].push_back(from); return rg; } int main() { cout << fixed << setprecision(5); ll n; cin >> n; vl l(n); vl s(n); REP(i,n) cin >> l[i] >> s[i]; Graph g(n); REP(i, n) g[s[i]-1].push_back(i); auto rg = create_rg(g); auto v = scc(g, rg); ll sum1 = 0, sum2 = 0; REP(i, v.size()) { if (v[i].size() == 1 && s[v[i][0]] - 1 != v[i][0]) { sum1 += l[v[i][0]]; } else { vi lv; for (auto j : v[i]) lv.push_back(l[j]); sort(ALL(lv)); sum1 += lv[0]; FOR(j, 1, lv.size()) sum2 += lv[j]; } } cout << sum1 + sum2 / 2.0 << endl; return 0; }