#P6346. [CCO 2017] 专业网络

[CCO 2017] 专业网络

Description

Kevin is building his professional network in a community. Unfortunately, he is an outsider and does not know anyone in the community yet. However, he can form friendships with nn people.

However, not many people in the community want to be friends with an outsider. Each of the nn people Kevin wants to befriend has a similar but different rule for becoming friends with an outsider. After Kevin already directly knows aia_i people in the community, person ii is willing to become friends with Kevin; otherwise, Kevin must pay a cost of bib_i to become friends with them.

Your task is to make Kevin become friends with all nn people while minimizing the total cost he pays.

Input Format

The first line contains an integer nn.

The next nn lines each contain two integers ai,bia_i, b_i.

Output Format

Output one integer on a single line, representing the minimum total cost Kevin needs to pay.

4
3 3
1 2
0 5
3 4
3
5
0 9
1 8
2 7
3 6
4 5
0
3
0 6
2 7
3 8
8

Hint

Sample Explanation

For Sample 11: Kevin can immediately become friends with person 33. After establishing this friendship, he can also become friends with person 22. He needs to pay a cost of 33 to become friends with person 11. Then he has 33 friends in total, which allows him to become friends with person 44.

For Sample 22: Kevin does not need to pay anything to become friends with everyone.

For Sample 33: Kevin should immediately become friends with person 11, then pay a cost of 88 to become friends with person 33, and finally establish a friendship with person 22.

Constraints and Notes

This problem uses bundled multi-test evaluation, with 44 subtasks in total.

  • Subtask 1 (15 points): all bib_i are equal to 11;
  • Subtask 2 (30 points): 1n101 \le n \le 10;
  • Subtask 3 (30 points): 1n1031 \le n \le 10^3.
  • Subtask 4 (25 points): 1n2×1051 \le n \le 2 \times 10^5.

For all testdata, it is guaranteed that 1n2×1051 \le n \le 2 \times 10^5, 1in1 \le i \le n, 0ain0 \le a_i \le n, and 0bi1040 \le b_i \le 10^4.

Source: CCO 2017 Day2 “Professional Network”.

Note: This translation is from LOJ.

Translated by ChatGPT 5