题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=49

思路:01背包

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x7f7f7f7f;//2139062143
typedef long long ll;
using namespace std;

int dp[30010];
int cost[30];
int value[30];

int main()
{
int t;
int n,m;
int b;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d",&cost[i],&b);
value[i] = cost[i] * b;
}
for(int i=0; i<m; i++)
{
for(int j=n; j>=cost[i]; j--)
{
dp[j] = max(dp[j],dp[j-cost[i]] + value[i]);
}
}
printf("%d\n",dp[n]);
}
return 0;
}