n = int(input()) l, s = [], [] ans = 0 for _ in range(n): li, si = map(int, input().split()) l.append(li) s.append(si - 1) ans += li visited = [False] * n for i in range(n): if visited[i]: continue seq = [] while not visited[i]: visited[i] = True seq.append(i) i = s[i] if i not in seq: continue ans += min(l[j] for j in seq[seq.index(i):]) print(ans / 2)