제출 #520320

#제출 시각아이디문제언어결과실행 시간메모리
520320nguyentuKnapsack (NOI18_knapsack)C++14
12 / 100
2 ms1868 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; f[i][j] = f[i - 1][j]; if (x > f[i - 1][j] && x > (f[i - 1][j - a[i].w] + a[i].v) && (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 ((f[i - 1][j - a[i].w] + a[i].v) > f[i - 1][j] && (f[i - 1][j - a[i].w] + a[i].v) >= x) { f[i][j] = f[i - 1][j - a[i].w] + a[i].v; g[j] = 1; } } else f[i][j] = f[i - 1][j]; } } 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...