#include "bits/stdc++.h" using namespace std; #define OUT(x) cout << #x << " = " << x << endl #define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++) #define rer(i, l, r) for (int (i) = (int)(l); (i) <= (int)(r); (i)++) #define reu(i, l, r) for (int (i) = (int)(l); (i) < (int)(r); (i)++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void pv(T a, T b) { for (T i = a; i != b; i ++) cout << *i << " "; cout << endl; } template inline void amin(T &x, U y) { if (y < x) x = y; } template inline void amax(T &x, U y) { if (x < y) x = y; } int in() { int x; scanf("%d", &x); return x; } long long lin() { long long x; scanf("%lld", &x); return x; } int n; vector g[110]; vector rg[110]; vector order; bool used[110]; int cmp[110]; void add_edge(int from, int to) { g[from].push_back(to); rg[to].push_back(from); } void dfs(int v) { used[v] = true; for (auto i : g[v]) if (!used[i]) dfs(i); order.push_back(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; for (auto i : rg[v]) if (!used[i]) rdfs(i, k); } int StronglyConnectedComponent() { memset(used, 0, sizeof(used)); order.clear(); for (int i = 0; i < n; i ++) if (!used[i]) dfs(i); memset(used, 0, sizeof(used)); int k = 0; for (int i = order.size() - 1; i >= 0; i --) if (!used[order[i]]) rdfs(order[i], k ++); return k; } signed main() { cin >> n; vector L(n); rep(i, n) { cin >> L[i]; int s; cin >> s; s --; if (s - i) add_edge(s, i); } int k = StronglyConnectedComponent(); vector> cmmp(n); rep(i, n) { cmmp[i].first = cmp[i]; cmmp[i].second = i; } sort(all(cmmp)); double ans = 0.0; rep(i, n) { vector can; can.push_back(cmmp[i].second); while (i < n - 1 && cmmp[i].first == cmmp[i + 1].first) { can.push_back(cmmp[i + 1].second); i ++; } if (can.size() == 1) { bool f = false; rep(j, rg[cmmp[i].second].size()) { if (cmp[cmmp[i].second] > cmp[rg[cmmp[i].second][j]]) { f = true; } } if (f) { ans += (double)L[cmmp[i].second] / 2.0; } else { ans += (double)L[cmmp[i].second]; } } else { vector l; rep(j, can.size()) { int ll = L[can[j]]; l.push_back(ll); } sort(all(l)); ans += (double)l[0]; reu(j, 1, l.size()) ans += (double)l[j] / 2.0; } } cout << ans << endl; return 0; }