[Bzoj00007] ornaments, bzoj00007 Ornaments

Source: Internet
Author: User

[Bzoj00007] ornaments, bzoj00007 Ornaments

Description

JOI Jun has N ornaments mounted on his mobile phone, numbered 1... N. JOI can install some of them on a mobile phone.
JOI Jun's ornaments are somewhat different-some of them are associated with hooks that can be mounted with other pendants. Each pendant is either directly mounted on the mobile phone or mounted on the hook of other pendants. Up to one pendant can be directly mounted on a mobile phone.
In addition, each pendant has a joy value that will be obtained during installation, expressed by an integer. If JOI really hates a decoration, the joy of the decoration is a negative number.
JOI wants to maximize the sum of joy values of all ornaments. Note that you do not need to mount all the hooks with ornaments, and it is also possible to disable one.
Input

The first line is an integer N, indicating the number of ornaments.
In the next N rows, line I (1 <= I <= N) has two spaces to separate the integers "Ai" and "Bi", indicating that the ornaments "I" has an Ai hook, after the installation, you will get the joy of Bi.
Output

An integer is output to indicate the maximum total number of ornaments connected to the mobile phone.
Sample Input

5

0 4

2-2

1-1

0 1

0 3
Sample Output

5
HINT

You can directly attach the ornaments 2 to your mobile phone, and then attach the ornaments 1 and 5 to the two hooks of the ornaments 2 to obtain the maximum value of joy: 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 and lodging competitions 4 By PoPoQQQ

It is not hard to see that it is a backpack.
Consider the volume of the item whose size is 0 at the beginning as 1, instead of-v + 1 as a backpack.
Pay attention to the following issues:

At first, we went straight into the violent backpack WA... Ask Claris to find this problem...
And use negative infinity during initialization, but do not directly define the macro value 0x7 fffffff, and add a negative number before it; otherwise, it will WA.
The next step is the dumb backpack.

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 4100#define MAXINT 2100000000using namespace std;int f[MAXN>>1][MAXN];int n,ans=-MAXINT;int v,c;struct pack{    int v,c;    bool operator <(const pack& a)const    {        return v>a.v;    }}s[MAXN];int main(){    scanf("%d",&n);    for (int i=0;i<=n;i++)  f[0][i]=f[i][n+1]=-MAXINT;    for (int i=1;i<=n;i++)  scanf("%d%d",&s[i].v,&s[i].c);    f[0][1]=0;    sort(s+1,s+n+1);    for (int i=1;i<=n;i++)        for (int j=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 (int i=0;i<=n;i++)  ans=max(ans,f[n][i]);    printf("%d\n",ans);}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.