#P4138. [JOISC 2014] 挂饰 / Straps
[JOISC 2014] 挂饰 / Straps
Description
JOI-kun has ornaments, numbered . He can attach some of them to his phone. Some ornaments are special—they have hooks that can hold other ornaments. Each ornament is either attached directly to the phone or to a hook of another ornament. At most one ornament can be attached directly to the phone. Each ornament has an integer joy value gained when it is installed. If JOI-kun dislikes an ornament, its joy value is negative. JOI-kun wants to maximize the sum of the joy values of all ornaments connected to the phone. It is not necessary to use all hooks, and attaching none is also allowed.
Input Format
The first line contains an integer , the number of ornaments. The next lines, the -th line (), contain two space-separated integers and , meaning ornament has hooks and yields joy when installed.
Output Format
Output a single integer, the maximum total joy value of the ornaments connected to the phone.
5
0 4
2 -2
1 -1
0 1
0 3
5
Hint
- .
- ().
- ().
Translated by ChatGPT 5
京公网安备 11011102002149号