// Arup Guha
// 8/2/2022
// Solution to 2022 March Silver USACO Problem: Visits

import java.util.*;
import java.io.*;

public class visits {

	public static int n;
	public static long[] nextPts;
	public static int[] next;
	public static long total;
	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(stdin.readLine());
		
		next = new int[n];
		nextPts = new long[n];
		total = 0;
		
		// Read data, keep track of sum.
		for (int i=0; i<n; i++) {
			StringTokenizer tok = new StringTokenizer(stdin.readLine());
			next[i] = Integer.parseInt(tok.nextToken())-1;
			nextPts[i] = Long.parseLong(tok.nextToken());
			total += nextPts[i];
		}

		// Mark where we've visited.
		boolean[] used = new boolean[n];
		
		// "DFS" from each unvisited node.
		for (int i=0; i<n; i++) {
			if (used[i]) continue;
			
			// Parallel stacks for nodes and points.
			Stack<Integer> nodeS = new Stack<Integer>();
			Stack<Long> ptS = new Stack<Long>();
			
			int cur = i;
			
			// Keep going until we repeat something.
			do {
				used[cur] = true;
				nodeS.push(cur);
				ptS.push(nextPts[cur]);
				cur = next[cur];
			} while (!used[cur]);
			
			// Find the minimum edge in the cycle - these are the points we can't have.
			long minInLoop = nextPts[cur];
			int saveCur = cur;
			while (nodeS.size() > 0 && nodeS.peek() != saveCur) {
				cur = nodeS.pop();
				minInLoop = Math.min(minInLoop, ptS.pop());
			}
			
			// If there's something left in the stack, then we actually identified a new cycle.
			if (nodeS.size() > 0) total -= minInLoop;
		}
		
		// Ta da!
		System.out.println(total);
	}
}