#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, n) for (int i = 0; i < int(n); i++) using namespace std; long long MOD = 1000000007; long long INF = 1000000000000000; //10^15 typedef long long ll; typedef unsigned long long ull; ll powMod(ll x, ll n) { if (n == 0) return 1; ll r = powMod(x, n / 2); if (n % 2 == 0) return (r * r) % MOD; return (r * r % MOD) * x % MOD; } int n, s; double l[110]; vector g[110]; bool visited[110], minflag; double sum, Min; void dfs(int i, double mi, int start) { visited[i] = true; sum += l[i]; rep(j, g[i].size()) { if (g[i][j] == start) { minflag = true; Min = mi; } if (!visited[g[i][j]]) { dfs(g[i][j], min(mi, l[g[i][j]]), start); } } } int main(void) { cin >> n; vector list; rep(i, n) { cin >> l[i] >> s; s--; if (i == s) list.push_back(s); else g[s].push_back(i); } double ans = 0; rep(i, list.size()) { sum = 0; dfs(list[i], l[list[i]], list[i]); ans += sum / 2 + l[list[i]] / 2; } rep(i, n) { minflag = false; sum = 0; if (!visited[i]) { bool vi[110] = {}; rep(i, n) vi[i] = visited[i]; dfs(i, l[i], i); if (minflag) ans += sum / 2 + Min / 2; else { rep(i, n) visited[i] = vi[i]; } } } cout << std::fixed; cout << std::setprecision(1) << ans << endl; return 0; }