#Q164. 「一本通 5.2 练习 5」骑士

「一本通 5.2 练习 5」骑士

Description

Original source: ZJOI 2008

The knight order of country Z is a powerful organization, gathering elites from all over. They rob the rich to help the poor, punish evil and promote good, earning praise from all walks of life.

However, a terrible event has recently occurred: the evil country Y launched an aggressive war against country Z. The flames of war spread across five hundred miles, and country Z, which has lived in peace for hundreds of years, is no match for Y's army. Thus, people place all their hopes on the knight order, as if expecting the birth of a true dragon emperor to lead justice in defeating evil.

The knight order certainly has the ability to defeat the forces of evil, but the knights often have conflicts among themselves. Each knight has exactly one knight they despise the most (certainly not themselves), and they will absolutely not go to battle alongside the one they despise the most.

With the war raging and people suffering, organizing a knight legion to join the battle is urgent! The king has assigned you a daunting task: select a knight legion from all the knights such that there are no two conflicting members in the legion—meaning no knight is chosen alongside the one they despise the most—and ensure this legion has the greatest combat effectiveness.

To describe combat effectiveness, we number the knights from 11 to NN and assign each knight an estimated combat effectiveness. The combat effectiveness of a legion is the sum of the combat effectiveness of all selected knights.

Input Format

The first line of input contains a positive integer NN, describing the number of knights in the order;

The next NN lines each contain two positive integers, describing in order the combat effectiveness of each knight and the knight they despise the most.

Output Format

Output consists of one line, a single integer representing the combat effectiveness of the selected knight legion.

Sample 1

3
10 2
20 3
30 1

30

Data Range and Hints

For 30%30\% of the data, N10N\le 10;

For 60%60\% of the data, N100N\le 100;

For 80%80\% of the data, N104N\le 10^4;

For 100%100\% of the data, N106N\le 10^6, and each knight's combat effectiveness is a positive integer not exceeding 10610^6.