Submission #642423

#TimeUsernameProblemLanguageResultExecution timeMemory
642423burak_ozzkanKnapsack (NOI18_knapsack)C++14
0 / 100
113 ms262144 KiB
#include <bits/stdc++.h> #define nl '\n' #define int long long using namespace std; /* f(i, j, k) = max value where i can use k item from ith type, using first i items, sack size = j f(i, j, k){ max(v[i].value+f(i, j-v[i].weight, k-1), f(i-1, j, k)) } 15 5 4 12 1 2 1 1 10 4 1 1 1 1 2 2 1 20 3 5000 15 1 100 1 3 50 1 4 */ 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 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...