JOI Spring Training Camp 2013/14
Original Statement / Submission Site
JOI-kun has N straps for attaching to his mobile phone. The straps are numbered from 1 to N. He wants to attach some of these straps to his phone.
The straps are somewhat unusual. Some straps have several connectors to which other straps can be attached. Each strap can be attached either directly to the mobile phone or to a connector on another strap. At most one strap can be attached directly to the mobile phone.
Each strap has a fixed happiness value that JOI-kun gains when the strap is attached. This value is represented by an integer. JOI-kun dislikes some straps, so their happiness values may be negative.
JOI-kun wants to maximize the sum of the happiness values of all straps connected to the mobile phone. It is not necessary to attach a strap to every connector, and he may also choose not to attach any straps at all.
Given information about JOI-kun's N straps, determine the maximum possible total happiness of the straps connected to the mobile phone.
The input is given from standard input in the following format:
N A1 B1 A2 B2 ... AN BN
N, the number of straps.N lines contains two integers Ai and Bi. Strap i has Ai connectors, and attaching it gives a happiness value of Bi.Print one integer: the maximum possible sum of the happiness values of all straps connected to the mobile phone.
1 ≤ N ≤ 20000 ≤ Ai ≤ N-1000000 ≤ Bi ≤ 1000000N ≤ 15Bi = 0 for every i.Ai ≤ 15 for every i.There are no additional constraints.
5 0 4 2 -2 1 -1 0 1 0 3
5
In this case, the maximum total happiness is 5, achieved by attaching the straps as follows:
2 directly to the mobile phone.1 to one connector of strap 2.5 to another connector of strap 2.6 2 -3 3 -1 0 -4 0 -2 1 -3 4 -1
0
Every strap has a negative happiness value. Therefore, the maximum total happiness is obtained by attaching no straps.
15 1 -4034 1 3406 0 6062 4 -6824 0 9798 0 4500 0 -1915 1 2137 0 9786 0 7330 0 -9365 2 2730 0 -5797 0 6129 0 8925
43417