#include using namespace std; const int INF = 100000; struct strongly_connected_components{ int cnt; vector scc; void dfs(vector> &E, vector &used, vector &ord, int v){ used[v] = true; for (int w : E[v]){ if (!used[w]){ dfs(E, used, ord, w); } } ord.push_back(v); } strongly_connected_components(vector> &E){ int N = E.size(); vector> E2(N); for (int i = 0; i < N; i++){ for (int j : E[i]){ E2[j].push_back(i); } } vector ord; vector used1(N, false); for (int i = 0; i < N; i++){ if (!used1[i]){ dfs(E2, used1, ord, i); } } reverse(ord.begin(), ord.end()); cnt = 0; scc = vector(N, -1); for (int i = 0; i < N; i++){ if (scc[ord[i]] == -1){ scc[ord[i]] = cnt; queue Q; Q.push(ord[i]); while (!Q.empty()){ int v = Q.front(); Q.pop(); for (int w : E[v]){ if (scc[w] == -1){ scc[w] = cnt; Q.push(w); } } } cnt++; } } } int size(){ return cnt; } int operator [](int k){ return scc[k]; } }; int main(){ cout << fixed << setprecision(20); int N; cin >> N; vector L(N), S(N); for (int i = 0; i < N; i++){ cin >> L[i] >> S[i]; S[i]--; } vector> E(N); for (int i = 0; i < N; i++){ E[S[i]].push_back(i); } strongly_connected_components G(E); int M = G.size(); vector d(M, 0); for (int i = 0; i < N; i++){ if (G[S[i]] != G[i]){ d[G[i]]++; } } vector mn(M, INF); for (int i = 0; i < N; i++){ mn[G[i]] = min(mn[G[i]], L[i]); } double ans = 0; for (int i = 0; i < N; i++){ ans += (double) L[i] / 2; } for (int i = 0; i < M; i++){ if (d[i] == 0){ ans += (double) mn[i] / 2; } } cout << ans << endl; }