CRB and His BirthdayTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Today is CRB's birthday. His mom decided to buy many presents for her lovely son.
She went to the nearest shop with
Won(currency unit).
At the shop, there are
kinds of presents.
It costs
Won to buy one present of
-th kind. (So it costs
×
Won to buy
of them.)
But as the counter of the shop is her friend, the counter will give
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3b53332e8d9fb6415172877be4e818de.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0b9bdfc72ee92408a30df8ff0b36cbd1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c629cea079cc83e4987a8ad3e62b02d6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
candies if she buys
(
>0) presents of
-th kind.
She wants to receive maximum candies. Your task is to help her.
1 ≤
≤ 20
1 ≤
≤ 2000
1 ≤
≤ 1000
0 ≤
![](https://i-blog.csdnimg.cn/blog_migrate/cd2a8c747025d72415048fecfeb53227.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
≤ 2000
1 ≤
≤ 2000
She went to the nearest shop with
![](https://i-blog.csdnimg.cn/blog_migrate/2b362ac2e39aeef924b88c5ac6e667c8.png)
At the shop, there are
![](https://i-blog.csdnimg.cn/blog_migrate/c81279c779a69c142e4d087d51238c72.png)
It costs
![](https://i-blog.csdnimg.cn/blog_migrate/7d5aecd1412688501fd1ab7a8e237913.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c4e1b007f502e86c63a9f3d0bb853440.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3b546d71da80809e110df149f5a02712.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7d5aecd1412688501fd1ab7a8e237913.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3b546d71da80809e110df149f5a02712.png)
But as the counter of the shop is her friend, the counter will give
![](https://i-blog.csdnimg.cn/blog_migrate/b38190075844108b2c9d5e7c10c12e7c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3b53332e8d9fb6415172877be4e818de.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0b9bdfc72ee92408a30df8ff0b36cbd1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c629cea079cc83e4987a8ad3e62b02d6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c5a6f30e1165fc5d28fc5ae2d00fa869.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0b9bdfc72ee92408a30df8ff0b36cbd1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0b9bdfc72ee92408a30df8ff0b36cbd1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c4e1b007f502e86c63a9f3d0bb853440.png)
She wants to receive maximum candies. Your task is to help her.
1 ≤
![](https://i-blog.csdnimg.cn/blog_migrate/fd3bb5e4d8b74c8c5891428f5fff131a.png)
1 ≤
![](https://i-blog.csdnimg.cn/blog_migrate/2b362ac2e39aeef924b88c5ac6e667c8.png)
1 ≤
![](https://i-blog.csdnimg.cn/blog_migrate/c81279c779a69c142e4d087d51238c72.png)
0 ≤
![](https://i-blog.csdnimg.cn/blog_migrate/b38190075844108b2c9d5e7c10c12e7c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cd2a8c747025d72415048fecfeb53227.png)
![](https://i-blog.csdnimg.cn/blog_migrate/81b0ef0125fef2f4420c33043dd77c6a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c5a6f30e1165fc5d28fc5ae2d00fa869.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
1 ≤
![](https://i-blog.csdnimg.cn/blog_migrate/7d5aecd1412688501fd1ab7a8e237913.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
Input
There are multiple test cases. The first line of input contains an integer
, indicating the number of test cases. For each test case:
The first line contains two integers
and
.
Then
lines follow,
-th line contains three space separated integers
,
and
.
![](https://i-blog.csdnimg.cn/blog_migrate/fd3bb5e4d8b74c8c5891428f5fff131a.png)
The first line contains two integers
![](https://i-blog.csdnimg.cn/blog_migrate/2b362ac2e39aeef924b88c5ac6e667c8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c81279c779a69c142e4d087d51238c72.png)
Then
![](https://i-blog.csdnimg.cn/blog_migrate/c81279c779a69c142e4d087d51238c72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c4e1b007f502e86c63a9f3d0bb853440.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7d5aecd1412688501fd1ab7a8e237913.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b38190075844108b2c9d5e7c10c12e7c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c5a6f30e1165fc5d28fc5ae2d00fa869.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9230b09fa862f98b4625077b72db9439.png)
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
21
Hint
CRB's mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.
解题:先跑一遍01背包,价值为a+b的,然后再按价值为a的完全背包跑。(换过来也一样)
<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int dp[2005];
int n,m,t;
int a[1005],b[1005],v[1005];
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d %d",&m,&n);
for(int i=1;i<=n;i++)
scanf("%d %d %d",&v[i],&a[i],&b[i]);
memset(dp,0,sizeof(dp));
int ans=0;
for(int i=1;i<=n;i++){
for(int j=v[i];j<=m;j++){
dp[j]=max(dp[j],dp[j-v[i]]+a[i]);
ans=max(ans,dp[j]);
}
for(int j=m;j>=v[i];j--){
dp[j]=max(dp[j],(dp[j-v[i]]+a[i]+b[i]));
ans=max(ans,dp[j]);
}
}
printf("%d\n",ans);
}
return 0;
}</span>