Submission #520317

#TimeUsernameProblemLanguageResultExecution timeMemory
520317nguyentuKnapsack (NOI18_knapsack)C++14
12 / 100
456 ms2852 KiB
#include <bits/stdc++.h> using namespace std; #define int long long struct seg { int v , w , k; }; signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int s , n; cin >> s >> n; vector<seg> a(n + 1); for (int i = 1 ; i <= n ; i++) { cin >> a[i].v >> a[i].w >> a[i].k; } vector<vector<int>> f; vector<int> g; f.assign(n + 1 , vector<int> (s + 1 , 0)); for (int i = 1 ; i <= n ; i++) { g.assign(s + 1 , 0); for (int j = 0 ; j <= s ; j++) { if (a[i].w <= j) { int x = f[i][j - a[i].w] + a[i].v; int y = f[i - 1][j - a[i].w] + a[i].v; int z = f[i - 1][j]; if (y >= z && y >= x) { f[i][j] = f[i - 1][j - a[i].w] + a[i].v; g[j] = 1; } if (x > z && x > y && (g[j - a[i].w] + 1) <= a[i].k) { f[i][j] = f[i][j - a[i].w] + a[i].v; g[j] = g[j - a[i].w] + 1; } if (z >= y && z >= x) f[i][j] = f[i - 1][j]; } else f[i][j] = f[i - 1][j]; } } for (int i = 1 ; i <= n ; i++) { for (int j = 0 ; j <= s ; j++) { cerr << f[i][j] << " "; } cerr << '\n'; } int ans = 0; for (int i = 1 ; i <= s ; i++) { ans = max(ans , f[n][i]); } cout << ans; return 0; }
#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...