#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; pair dfs(int a, const vector& level, vector& s) { if(s[a] == -1) return make_pair(a, level[a]); int b = s[a]; s[a] = -1; pair p = dfs(b, level, s); if(p.first == -1) return p; if(p.first == a){ p.first = -1; return p; } p.second = min(p.second, level[a]); return p; } int main() { int n; cin >> n; vector level(n), s(n); for(int i=0; i> level[i] >> s[i]; -- s[i]; } int ret = accumulate(level.begin(), level.end(), 0); for(int i=0; i p = dfs(i, level, s); if(p.first == -1) ret += p.second; } cout << (ret / 2); if(ret % 2 != 0) cout << ".5" << endl; else cout << ".0" << endl; return 0; }