View Code of Problem 4045

#include <bits/stdc++.h>
using namespace std;

int main(){

    int n,V;
    while(cin>>V>>n){
        int w[1010], v[1010];
        int dp[3000]={0};
        for(int i = 0; i<n ;i++){
            cin>>w[i]>>v[i];
        }
        for(int i = 0; i<n; i++){
            for(int j =V; j>=w[i]; j--){
                dp[j] =  max(dp[j], dp[j-w[i]]+v[i]);

            }
        }
        cout<<dp[V]<<endl;
    }


    return 0;
}

Double click to view unformatted code.


Back to problem 4045