Submission #642444

#TimeUsernameProblemLanguageResultExecution timeMemory
642444burak_ozzkanKnapsack (NOI18_knapsack)C++14
37 / 100
108 ms262144 KiB
#include <bits/stdc++.h> #define nl '\n' #define int long long using namespace std; struct item { int value; int weight; int piece; }; void solve(){ int s, n; cin >> s >> n; vector<item> v(n); int max_k = -1; for(int i = 0; i < n; i++){ item tmp; cin >> tmp.value >> tmp.weight >> tmp.piece; v[i] = tmp; max_k = max(max_k, tmp.piece); } vector< vector< vector<int> > > dp(n+10, vector< vector<int> >(s+10, vector<int>(max_k+10, -1))); function<int(int, int, int)> f = [&](int i, int j, int k){ if(i < 0 || j <= 0 || k < 0) return 0LL; if(dp[i][j][k] != -1) return dp[i][j][k]; if(k == 0 && i == 0) return 0LL; if(k == 0 && i >= 1) return (dp[i][j][k] = f(i-1, j, v[i-1].piece)); int t1 = 0; if(j-v[i].weight >= 0) t1 = v[i].value + f(i, j-v[i].weight, k-1); int t2 = 0; if(i-1 >= 0) t2 = f(i-1, j, v[i-1].piece); return (dp[i][j][k] = max(t1, t2)); }; cout << f(n-1, s, v[n-1].piece) << nl; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; /*cin >> t;*/ while(t--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...