Description
Joi has n a pendant on the phone, numbered 1 ... N. Joi can put some of them on the phone.
Joi's ornaments are somewhat different-some of them have hooks that can hang other pendants. Each pendant is either hung directly on the phone or hung on the hook of the other pendant. There are up to 1 hooks hanging directly on the phone.
In addition, each pendant has a joy value to be obtained during installation, expressed as an integer. If Joi hates a pendant, the joy of the pendant is a negative number.
Joi wants to maximize the sum of the joy values of all ornaments. Note that you do not have to hang all the hooks on the pendant, and you can do it without hanging it.
Input
The first line is an integer n, which represents the number of ornaments.
Next N lines, line I (1<=i<=n) has two space-delimited integer ai and bi, which indicates that the pendant i has an AI hook, which will get the joy value of bi after installation.
Output
Outputs a single integer that represents the maximum value of the total number of ornaments connected on the phone
Sample Input
5
0 4
2-2
1-1
0 1
0 3
Sample Output
5
HINT
Hang the pendant 2 directly on the phone, then the pendant 1 and the pendant 5 are hung on the pendant 2 two hooks, you can get the maximum joy value 4-2+3=5.
1<=n<=2000
0<=ai<=n (1<=i<=n)
-10^6<=bi<=10^6 (1<=i<=n)
Source
JOI 2013~2014 Spring Training Athletic 4 by POPOQQQ
It's not hard to see the backpack.
So start reading into the volume of 0 items as 1, not 0 as-v+1 to do the backpack.
Some questions to note:
The first Direct violence Backpack wa ... Claris found the problem ...
And the initialization time with negative infinity, but the infinity does not directly define the macro 0x7fffffff and then add a minus sign in front of it will WA.
Then there's the stupid backpack.
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define MAXN 4100#define MAXINT 2100000000using namespace STD;intF[maxn>>1][MAXN];intN,ans=-maxint;intV,c;structpack{intV,c;BOOL operator< (Constpack& a)Const{returnv>a.v; }}S[MAXN];intMain () {scanf("%d", &n); for(intI=0; i<=n;i++) f[0][i]=f[i][n+1]=-maxint; for(intI=1; i<=n;i++)scanf("%d%d", &S[I].V,&S[I].C); f[0][1]=0; Sort (s+1, s+n+1); for(intI=1; i<=n;i++) for(intj=0; j<=n;j++) F[i][j]=max (f[i-1][max (J-S[I].V,0)+1]+s[i].c,f[i-1][J]); for(intI=0; i<=n;i++) Ans=max (Ans,f[n][i]);printf("%d\n", ans);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"BZOJ4247" Pendant