#P6346. Kevin's Friendship Dilemma

    ID: 19562 Type: Default 1000ms 256MiB

Kevin's Friendship Dilemma

Kevin's Friendship Dilemma

Kevin is trying to build his professional network in a community. Unfortunately, as an outsider, he initially does not know anyone in the community. However, he aims to become friends with n people. Each potential friend i has a unique criterion: if Kevin already knows at least \(a_i\) people in the community, then person i will be willing to befriend him for free; otherwise, Kevin must pay a cost of \(b_i\) to establish that friendship.

Your task is to help Kevin befriend all n people while minimizing the total cost paid.

Note: The order in which Kevin makes these friendships matters. Initially, he knows 0 people. At any moment, if there exists at least one person whose friendship criterion \(a_i\) is met (i.e. \(a_i \leq current\ count\)), Kevin can befriend them for free (increasing his known count by 1). If no such friend is available, he must choose one person to pay the corresponding cost \(b_i\) to befriend, which again increases his count by 1.

Design an algorithm to compute the minimum total cost required for Kevin to befriend all n people.

inputFormat

The first line contains a single integer n (the number of people).

Each of the following n lines contains two integers ai and bi representing the requirement and cost for the i-th person respectively.

You can assume that 0 ≤ ai ≤ n and 0 ≤ bi ≤ 10^9.

outputFormat

Output a single integer indicating the minimum total cost Kevin must pay to befriend all n people.

sample

3
0 5
1 3
2 10
0