Under construction

Still being built. Things may change

Assignments

Interview

Time limit1sMemory limit256 MB

Summary
Given deadlines and scores for N assignments, pick a subset schedulable within their deadlines to maximize total score.
Level

Medium5 of 10

Topics
Greedy, Sorting, Heap, Intervals
Solved
No attempts yet

Problem

Ungchan has many assignments to finish. He can finish one assignment per day, and each assignment has a deadline, so he may not be able to finish all of them. Finishing an assignment earns its score, but an assignment whose deadline has passed earns nothing.

Find the maximum total score Ungchan can earn.

Input

The first line contains the number of assignments NN (1≤N≤10001 \le N \le 1000).

Each of the next NN lines describes one assignment with two integers dd (1≤d≤10001 \le d \le 1000) and ww (1≤w≤1001 \le w \le 100). Here dd is the number of days left until the deadline, so the assignment can only be finished on one of days 1 through dd. The value ww is the score earned by finishing it.

Output

Print the maximum total score on the first line.

Hint

In the first example, finishing the fifth, fourth, second, first, and seventh assignments in that order and giving up the third and sixth assignments earns 185 points.

Examples6

  1. Example 1

    Input
    7
    4 60
    4 40
    1 20
    2 50
    3 30
    4 10
    6 5
    
    Expected output
    185
    
  2. Example 2

    Input
    1
    1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    1000 100
    
    Expected output
    100
    
  4. Example 4

    Input
    4
    1 10
    1 20
    1 5
    1 30
    
    Expected output
    30
    
  5. Example 5

    Input
    5
    5 1
    5 2
    5 3
    5 4
    5 5
    
    Expected output
    15
    
  6. Example 6

    Input
    3
    1 5
    2 5
    3 5
    
    Expected output
    15