CRB and his Birthday Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 72
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 of 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
Hint CRB ' s mom buys presents of first kind, and receives 2x10 + 1 = candies.
Author KUT (DPRK)
Source multi-university Training Contest 10 knapsack problem.
Whether each state can be transferred by Dp[i-cost]
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace
Std
#define MAXN int DP[MAXN];
int RES[MAXN];
int CHECK[MAXN];
int main () {int T, m,n;
scanf ("%d", &t);
while (t--) {scanf ("%d%d", &m,&n);
Memset (Dp,0,sizeof (DP));
int cost,a,b;
for (int i = 0;i < n; i++) {scanf ("%d%d%d", &cost,&a,&b);
memset (check,0,sizeof (check));
for (int i = 0;i <= m; i++) res[i] = Dp[i];
for (int i = cost; I <= m; i++) {if (Res[i-cost] + A + b>= dp[i]) check[i] = 1;
Res[i] = max (res[i],res[i-cost]+a); } for (int i = m;i >= 0; i--) if (check[i] = = 1) res[i] = max (res[i],res[
I-COST]+A+B);
for (int i = 0;i <= m; i++) dp[i] = max (dp[i],res[i]);
} int ans = 0; for (int i = 0;i <= m; i++) ans = max (ans,dp[i]);
printf ("%d\n", ans);
} return 0;
}