from collections import defaultdict import sys sys.setrecursionlimit(10 ** 6) def scc(n, adj) -> list: def dfs(v, used, acc): used[v] = True for to in adj[v]: if used[to]: continue dfs(to, used, acc) acc.append(v) def r_dfs(v, used, g): used[v] = True g.append(v) for to in r_adj[v]: if used[to]: continue r_dfs(to, used, g) r_adj = defaultdict(list) # 逆辺 for k, vs in adj.items(): for v in vs: r_adj[v].append(k) acc = [] used = [False] * n for i in range(n): if used[i]: continue dfs(i, used, acc) cc = [] used = [False] * n for node in reversed(acc): if used[node]: continue g = [] r_dfs(node, used, g) cc.append(g) return cc N = int(input()) adj = defaultdict(list) ls =[] for i in range(N): L, S = map(int, input().split()) S -= 1 # S がクリア済みだと i の難易度が半分になる adj[S].append(i) ls.append(L) cc = scc(N, adj) ans = 0 indegs = [0] * N for c in cc: t = [ls[p] for p in c] if any(indegs[p] > 0 for p in c): ans += sum(t) / 2 else: t.sort() ans += t[0] ans += sum(t[1:]) / 2 for p in c: for to in adj[p]: indegs[to] += 1 print(ans)