#include using namespace std; vector> find_cycle(vector to) { int n = to.size(); vector> ret; vector vec(n, -1); for (int v = 0; v < n; v++) { if (vec.at(v) == -1) { int nv = v; do { vec.at(nv) = v; nv = to.at(nv); } while (vec.at(nv) == -1); if (vec.at(nv) == v) { vector tmp; int end = nv; do { tmp.push_back(nv); nv = to.at(nv); } while (nv != end); ret.push_back(tmp); } } } return ret; } int main() { int N; cin >> N; vector L(N), S(N); int sum = 0; for (int i = 0; i < N; i++) { cin >> L.at(i) >> S.at(i); sum += L.at(i); S.at(i)--; } for (auto C : find_cycle(S)) { int mi = INT_MAX; for (auto c : C) mi = min(mi, L.at(c)); sum += mi; } cout << fixed << setprecision(1) << sum / 2.0 << "\n"; }