Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int dp[100000];
- int n;
- vector<int>w1;
- vector<int>v1;
- int rec(int weight1){
- if(weight1==0){
- return 0;
- }
- if(dp[weight1]!=-1){
- return(dp[weight1]);
- }
- int result=0;
- for(int j=0; j<n; j++){
- if(weight1-w1[j]>=0){
- result=max(result, rec(weight1-w1[j])+v1[j]);
- }
- }
- dp[weight1]=result;
- return result;
- }
- int main()
- {
- int w;
- cin>>n>>w;
- for(int i=0; i<n; i++){
- int a, b;
- cin>>a>>b;
- w1.push_back(a);
- v1.push_back(b);
- }
- for(int i=0; i<100000; i++){
- dp[i]=-1;
- }
- cout<<rec(w);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement