Problem Description
Today is CRB ' s birthday. His mom decided-buy many presents for her lovely son.
She went to the nearest shop with M Won (currency unit).
At the shop, there is N kinds of presents.
It costs Wi Won to buy one present of i-th kind. (So it costs Kxwi Won to buy K of them.)
But as the counter of the shop are her friend, the counter would give Aixx + Bi candies if she buys X (x> 0) presents O F i-th kind.
She wants to receive maximum candies. Your task is for help.
1≤t≤20
1≤m≤2000
1≤n≤1000
0≤ai, bi≤2000
1≤wi≤2000
Input
There is multiple test cases. The first line of input contains an integer T, indicating the number of the test cases. For each test case:
The first line contains the integers M and N.
Then N lines follow, i-th line contains three space separated integers Wi, Ai and Bi.
Output
For each test case, output the maximum candies she can gain.
Sample Input
1
100 2
10 2 1
20 1 1
Sample Output
21st
Hint
CRB ' s mom buys presents of first kind, and receives 2x10 + 1 = candies.
The topic is like a knapsack problem, the item can be placed infinitely, but the gain is the number of Aix +bi.
The key is more B, or it is an ordinary backpack, can dispose of B, the problem can be done.
Then considered such a state P[flag][j]:
Flag as a true means to let the best of the article I, flag is false to express the best of the first item.
J is the backpack capacity.
So p[1][j] = max (P[0][j-w[i]]+a[i]+b[i], p[1][j-w[i]]+a[i]);
Then to I+1, the initial state is not spared i+1 items, so need to initialize P[0][J]:
P[0][i] = max (P[0][i], p[1][i]);
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<Set>#include<map>#include<queue>#include<string>#include<algorithm>#defineLL Long Longusing namespacestd;Const intMAXN =1005;intN, M, p[2][2005];intW[MAXN], A[MAXN], B[MAXN];voidinput () {scanf ("%d%d", &m, &N); for(inti =0; I < n; ++i) scanf ("%d%d%d", &w[i], &a[i], &B[i]); Memset (P,0,sizeof(P));}voidWork () { for(inti =0; I < n; ++i) { for(intj = W[i]; J <= M; ++j) p[1][J] = max (p[0][j-w[i]]+a[i]+b[i], p[1][j-w[i]]+A[i]); for(intj =0; J <= M; ++j) p[0][J] = max (p[0][J], p[1][j]); } intAns =0; for(inti =0; I <= m; ++i) {ans= Max (ans, p[0][i]); Ans= Max (ans, p[1][i]); } printf ("%d\n", ans);}intMain () {//freopen ("test.in", "R", stdin); intT; scanf ("%d", &T); for(intTimes =0; Times < T; ++Times ) {input (); Work (); } return 0;}
ACM Learning process-hdu5410 CRB and his Birthday (dynamic planning)