#P4138. [JOISC 2014] 挂饰 / Straps

    ID: 3074 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp贪心2014剪枝背包JOI

[JOISC 2014] 挂饰 / Straps

Description

JOI-kun has NN ornaments, numbered 1,2,,N1, 2, \cdots, N. 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 NN, the number of ornaments. The next NN lines, the ii-th line (1iN1 \le i \le N), contain two space-separated integers AiA_i and BiB_i, meaning ornament ii has AiA_i hooks and yields joy BiB_i 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

  • 1N20001 \leq N \leq 2000.
  • 0AiN0 \leq A_i \leq N (1iN1 \leq i \leq N).
  • 106Bi106-10^6 \leq B_i \leq 10^6 (1iN1 \leq i \leq N).

Translated by ChatGPT 5