/* -*- coding: utf-8 -*- * * 19.cc: No.19 ステージの選択 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100; const int INF = 1 << 30; /* typedef */ typedef vector vi; /* global variables */ int ls[MAX_N]; vi nbrs[MAX_N]; bool used[MAX_N], loop[MAX_N]; /* subroutines */ int rec(int u, int minl = INF) { used[u] = loop[u] = true; if (minl > ls[u]) minl = ls[u]; int sum = ls[u]; vi &nbru = nbrs[u]; for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int &v = *vit; if (! used[v]) sum += rec(v, minl); else if (loop[v]) sum += minl; } loop[u] = false; return sum; } /* main */ int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> ls[i] >> x; x--; nbrs[x].push_back(i); } int sum = 0; for (int i = 0; i < n; i++) if (! used[i]) sum += rec(i); printf("%.1lf\n", 0.5 * sum); return 0; }