HDU 5410 CRB and his Birthday (01 backpack, full backpack, mixed)

Source: Internet
Author: User

Test instructions: There are n kinds of goods, there is a candy in each commodity, if buy this kind of goods to send more B candy, only the first time to buy the time to send. Now there are M yuan, how many sweets can I buy?

Idea: to buy a product for the first time to send candy, for this time to carry out a 01 backpack, that is, can only buy once. Then the product to a complete backpack, at this time do not send candy, can also buy more.

1#include <bits/stdc++.h>2 #definePII pair<int,int>3 #defineINF 0x7f7f7f7f4 #defineLL Long Long5 using namespacestd;6 Const intn=2050;7 LL Dp[n];8 intN, M, W[n], a[n], b[n];9 Ten LL cal () One { AMemset (DP,0,sizeof(DP)); -      for(intI=1; i<=n; i++)//Gifts -     { the          for(intJ=m; j-w[i]>=0; j--)//first one, 01 backpacks. -         { -             if(dp[j-w[i]]+ A[i] + b[i] >Dp[j]) -dp[j]=dp[j-w[i]]+ A[i] +B[i]; +         } -          for(intj=0; j+w[i]<=m; J + +)//one more full backpack. +         { A             if(Dp[j+w[i]] < dp[j]+A[i]) atdp[J+w[i]] = dp[j]+A[i]; -         } -     } -LL sum=-1; -      for(intI=m; I>0; i--) sum=max (sum, dp[i]); -     returnsum; in } -  to intMain () + { -Freopen ("Input.txt","R", stdin); the     intT; *Cin>>T; $      while(t--)Panax Notoginseng     { -scanf"%d%d", &m, &n); the          for(intI=1; i<=n; i++) scanf ("%d%d%d", &w[i], &a[i], &b[i]); +printf"%lld\n", Cal ()); A     } the  +     return 0; -}
AC Code

HDU 5410 CRB and his Birthday (01 backpack, full backpack, mixed)

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.