// Arup Guha
// 7/12/2015
// Solution to 2013 USACO Silver Problem A: Milk Scheduling

import java.util.*;
import java.io.*;

public class msched {

	public static void main(String[] args) throws Exception {

		Scanner stdin = new Scanner(new File("msched.in"));
		int n = stdin.nextInt();

		// Read and sort cows - by deadline.
		cow[] cows = new cow[n];
		for (int i=0; i<n; i++) {
			int milk = stdin.nextInt();
			int last = stdin.nextInt();
			cows[i] = new cow(milk, last);
		}
		Arrays.sort(cows);

		// Set up priority queue for skipping cows.
		PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
		int sub = 0, sum = 0;

		// Loop through cows in order.
		for (int i=0; i<n; i++) {
			sum += cows[i].milk;
			pq.offer(cows[i].milk);

			// If there are too many cows before this deadline, skip the cow
			// who milks the least.
			if (pq.size() > cows[i].last)
				sub += pq.poll();
		}

		// Here is our result.
		PrintWriter out = new PrintWriter(new FileWriter("msched.out"));
		out.println(sum-sub);
		out.close();
		stdin.close();
	}
}

class cow implements Comparable<cow> {

	public int milk;
	public int last;

	public cow(int m, int l) {
		milk = m;
		last = l;
	}

	public int compareTo(cow other) {
		if (this.last != other.last)
			return this.last - other.last;
		return other.milk - this.milk;
	}
}
