Bessie has gone to the mall’s jewelry store and spies a charm bracelet. Of course, she’d like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight W i (1 ≤ W i ≤ 400), a ‘desirability’ factor D i (1 ≤ D i ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
Input
- Line 1: Two space-separated integers: N and M
- Lines 2… N +1: Line i +1 describes charm i with two space-separated integers: W i and D i
Output
- Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
4 6
1 4
2 6
3 12
2 7
思路:
赤裸裸的01背包,背就完了
Sample Output
23
思路:
赤裸裸的01背包,背就完了
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define mes(x,y); memset(x,y,sizeof(x))
#define mv 2147483648+30
using namespace std;
ll gar(ll a,ll b){//最大公约数
return b==0?a:gar(b,a%b);
}
struct node{
int x,y;
}a[21000];
int dp[21000];
int main(){
int n,m;
while(cin>>n>>m){
mes(a,0);mes(dp,0);
for(int i=1;i<=n;i++){
cin>>a[i].x>>a[i].y;
}
for(int i=1;i<=n;i++){
for(int j=m;j>=a[i].x;j--){
dp[j]=max(dp[j],dp[j-a[i].x]+a[i].y);
}
}
cout<<dp[m]<<endl;
}
}