Creation August: Dark World (darkness), creationaugust
Dark World
Darkness. c/cpp/pas
-- On the Santu River, he and Jun are on the other side of the Oblique sun.
DarknessAugust is a cool girl and sister of LightAugust. To be more beautiful,
She has always made a tough selection of accessories (although she is already pretty)
There are now N ornaments, some of which Darkness can wear.
Some ornaments are distinctive, because these ornaments have hooks that can be attached with other ornaments.
Each ornament can be either worn directly or hung on the hook of other ornaments. A maximum of one ornament can be worn directly.
Each ornament has a value of joy during wearing, expressed as an integer. if Darkness hates a decoration, its joy value is a negative number. darkness wants to maximize the sum of joy values of all ornaments.
Of course, because she is beautiful, she can not wear any ornaments.
Input
The first line is an integer N, indicating the number of ornaments.
In the next N rows, there are two numbers of Ai and Bi in each line, indicating that I have an Ai hook, and there is a value of joy in Bi after installation.
Output
The sum of joy values for maximum output.
Hint
For 10% of data, Ai = 0, N is not limited.
For another 30% of data, 1 ≤ N ≤ 10, A, and B are not limited.
For 100% of the data, 0 ≤ Ai ≤ N.-10 ^ 6 ≤ Bi ≤ 10 ^ 6.1 ≤ N ≤ 2000.
Sample Input
5
0 4
2-2
1-1
0 1
0 3
Sample Output
5
The question can be thought of as a backpack problem, but the only difference is the appearance of a hook. That is to say, if a product has a hook, it will reduce the actual capacity. When we design a backpack, the number of idle hooks can be used as the second dimension of the backpack, that is, to avoid the appearance of the negative table, a N can be added to the second dimension uniformly. In this way, when the second question is N, instead, it indicates that the number of hooks is 0, so we can get a transfer. F [I] [j] = f [I − 1] [j − cocould [I] Here, the cocould array indicates a reduction in the number of hooks, that is, the actually increased number of hooks, because it also occupies a hook.
The program included below is written using recursive thinking.
TA provides code:
# Include <cstdio> # include <cstring> # include <iostream> using namespace std; # include <algorithm> int f [2005] [4005]; int main () {freopen ("darkness. in "," r ", stdin); freopen (" darkness. out "," w ", stdout); int N, I, A, B, j; memset (f [0],-127, sizeof (f [0]); scanf ("% d", & N); f [0] [N + 1] = 0; for (I = 1; I <= N; ++ I) {scanf ("% d", & A, & B); -- A; memcpy (f [I], f [I-1], sizeof (f [I]); if (~ A) f [I] [A] = max (f [I] [A], f [I-1] [0] + B); // prevents the occurrence of A negative submark. For (j = 1; j <= N <1; ++ j) f [I] [min (j + A, N <1)] = max (f [I] [min (j + A, N <1)], f [I-1] [j] + B );} printf ("% d \ n", * max_element (f [N] + N, f [N + 1]);}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.