# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
440999 | asdf1234coding | Knapsack (NOI18_knapsack) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
#define MOD (ll) (1e9+7)
int main() {
ios_base::sync_with_stdio(false);
ll s,n; cin>>s>>n;
vector<ll> v; // cost
vector<ll> w; // weight
vector<ll> k; // copies
ll maxW = 0;
for(ll i=0; i<n; i++) {
ll a,b,c; cin>>a>>b>>c;
v.push_back(a); w.push_back(b); k.push_back(c);
maxW += w[i]*k[i];
}
vector<vector<int> > dp(n+1, vector<int> (s+1));
dp[0][0] = 0;
for(ll i=1; i<=n; i++) {
for(ll j=0; j<=s; j++) {
for(ll m=0; m<=k[i-1]; m++) {
if(j>=(m*w[i-1])) { // if our current weight is greater than the weight of m copies of i
dp[i][j] = max(dp[i][j], dp[i-1][j-(m*w[i-1])] + m*v[i-1]);
//cout<<dp[i][j]<<'\t'<<i<<'\t'<<j<<'\t'<<endl;
} else {
break;
}
}
}
}
cout<<dp[n][s]<<endl;
}