from collections import deque def scc(N, G, RG): """ https://tjkendev.github.io/procon-library/python/graph/scc.html から拝借しています。 入力: : 頂点サイズ, : 順方向の有向グラフ, : 逆方向の有向グラフ 出力: (<ラベル数>, <各頂点のラベル番号>) """ order = [] used = [0] * N group = [None] * N def dfs(s): used[s] = 1 for t in G[s]: if not used[t]: dfs(t) order.append(s) def rdfs(s, col): group[s] = col used[s] = 1 for t in RG[s]: if not used[t]: rdfs(t, col) for i in range(N): if not used[i]: dfs(i) used = [0] * N label = 0 for s in reversed(order): if not used[s]: rdfs(s, label) label += 1 return label, group # 縮約後のグラフを構築 def construct(N, G, label, group): G0 = [set() for i in range(label)] GP = [[] for i in range(label)] for v in range(N): lbs = group[v] for w in G[v]: lbt = group[w] if lbs == lbt: continue G0[lbs].add(lbt) GP[lbs].append(v) return G0, GP N = int(input()) L2 = [0] * N G, RG = [[] for _ in range(N)], [[] for _ in range(N)] for i in range(N): L, S = map(int, input().split()) L2[i] = L * 2 S -= 1 G[S].append(i) RG[i].append(S) label, group = scc(N, G, RG) G0, GP = construct(N, G, label, group) diff = [[] for _ in range(label)] diff_cont = [0] * label for i, d in zip(group, L2): diff[i].append(d) for i in range(label): diff_cont[i] = sum(diff[i]) // 2 + min(diff[i]) // 2 sources = set(range(label)) for st in G0: sources -= st # dfs ans = 0 nonvisited = [True] * label for s0 in sources: ans += diff_cont[s0] d = deque([s0]) while d: v = d.pop() for x in G0[v]: if nonvisited[x]: ans += diff_cont[x] // 2 d.append(x) nonvisited[x] = False print(f'{ans // 2}.{5 if ans % 2 else 0}')