www.acmicpc.net/problem/1781
# 컵라면
# https://www.acmicpc.net/problem/1781 (탐욕)
import sys
import heapq
# problems: [(1, 6), (1, 7), (2, 4), (2, 5), (3, 2), (3, 1), (6, 1)]
def run(N, problems):
problems.sort(key=lambda x: x[0]) # ((데드라인 순서로 선택해서)) 문제를 푼다
solved = []
for d, cnt in problems:
heapq.heappush(solved, cnt)
while len(solved) > d:
heapq.heappop(solved)
return sum(solved)
N = int(input())
problems = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N)]
print(run(N, problems))