HDU 5410 CRB and his Birthday

Source: Internet
Author: User

Main topic:

A person is going to buy a gift, there is M yuan. There are n kinds of gifts, the value of each gift is WI, you buy the first I-piece gift k is can get Ai * k + Bi candy. Ask how you can get the most candy number. It's a complete backpack, actually. The number of items is more than one.            dp[nth items] [already cost M-ary] DP formula: dp[n][m] = max (Dp[n-1][m-w[n]] + a[i]+b[i], Dp[n][m-w[i]] + a[i]); At the end of the day, move the result. for (i=0; i<=m;i++) dp[n][i] = max (Dp[n-1][i], dp[n][i]);
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>#include<queue>#include<cmath>using namespacestd;#defineINF 0X3FFFFFFFtypedef __int64 LL;ConstLL MAXN =2005;ConstLL mod = 1e9+7;intA[MAXN], B[MAXN], W[MAXN], dp[maxn][maxn*2];intT, N, M, W;intMain () {scanf ("%d", &T);  while(t--) {scanf ("%d%d", &m, &N);  for(intI=1; i<=n; i++) scanf (" %d%d%d", &w[i], &a[i], &B[i]); Memset (DP,-1,sizeof(DP)); dp[0][0] =0; intAns =0;  for(intI=1; i<=n; i++)        {             for(intJ=w[i]; j<=m; J + +)            {                if(dp[i-1][j-w[i]]! =-1) Dp[i][j]= dp[i-1][j-w[i]] + a[i] +B[i]; if(Dp[i][j-w[i]]! =-1) Dp[i][j]= Max (Dp[i][j], Dp[i][j-w[i]] +A[i]); Ans=Max (ans, dp[i][j]); }             for(intj=0; j<=m; J + +) Dp[i][j]= Max (Dp[i][j], dp[i-1][j]); } printf ("%d\n", ans); }    return 0;}

HDU 5410 CRB and his Birthday

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.