"BZOJ4247" Pendant

Source: Internet
Author: User

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", &AMP;S[I].V,&AMP;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

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.