/** * @FileName a.cpp * @Author kanpurin * @Created 2020.10.13 15:28:30 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; struct StronglyConnectedComponents { private: vector> g, rg; vector order; vector visited; int V; void dfs(int v) { if (visited[v]) return; visited[v] = true; for (int u : g[v]) dfs(u); order.push_back(v); } void rdfs(int v, int c) { if (comp[v] != -1) return; comp[v] = c; for (int u : rg[v]) rdfs(u, c); } public: vector> Graph; vector comp; StronglyConnectedComponents(int v) : V(v) { g.resize(v); rg.resize(v); visited.resize(v); comp.resize(v); } void add_edge(int from, int to) { g[from].push_back(to); rg[to].push_back(from); } void build() { Graph = vector>(); order = vector(); visited.assign(V, false); comp.assign(V, -1); for (int i = 0; i < V; i++) dfs(i); reverse(order.begin(), order.end()); int number = 0; for (int i = 0; i < V; i++) { if (comp[order[i]] == -1) { rdfs(order[i], number++); } } Graph.resize(number); for (int i = 0; i < V; i++) { for (int v : g[i]) { if (comp[i] == comp[v]) continue; Graph[comp[i]].push_back(comp[v]); } } } }; int main() { int n;cin >> n; vector> p(n); int ans = 0; StronglyConnectedComponents g(n); for (int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; ans += p[i].first; g.add_edge(i,p[i].second-1); } g.build(); int sz = g.Graph.size(); vector in(sz); for (int i = 0; i < sz; i++) { for(auto v : g.Graph[i]) { in[i]++; } } constexpr int INF = 1e9 + 6; vector saishou(sz,INF); for (int i = 0; i < n; i++) { if (in[g.comp[i]] != 0) continue; saishou[g.comp[i]] = min(saishou[g.comp[i]],p[i].first); } for (int i = 0; i < sz; i++) { if (in[i] != 0) continue; ans += saishou[i]; } printf("%.1f",(double)ans/2); return 0; }