제출 #491184

#제출 시각아이디문제언어결과실행 시간메모리
491184tarcheKnapsack (NOI18_knapsack)C++17
0 / 100
1 ms204 KiB
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; struct Item { int value, weight, amount; }; int solve(const vector<Item>& items, vector<vector<int>>& dp, int i, int j, int k) { if (i < 0 || j < 0 || k < 0) return -INF; if (dp[i][j] != -1) return dp[i][j]; int lhs = solve(items, dp, i, j - items[i].weight, k - 1) + items[i].value; int rhs = solve(items, dp, i - 1, j, items[i - 1].amount); dp[i][j] = max(lhs, rhs); cout << i << ' ' << j << ' ' << k << " : " << lhs << ", " << rhs << '\n'; return dp[i][j]; } int main() { int S, N; cin >> S >> N; vector<Item> items(N); for (int i = 0; i < N; i++) cin >> items[i].value >> items[i].weight >> items[i].amount; sort(items.begin(), items.end(), [&](const Item& lhs, const Item& rhs) { return lhs.value < rhs.value; }); vector<vector<int>> dp(N, vector<int>(S + 1, -1)); dp[0][0] = 0; int ans = solve(items, dp, N - 1, S, items[N - 1].amount); for (int i = 0; i < N; i++) { for (int j = 0; j <= S; j++) { cout << dp[i][j] << ' '; } cout << '\n'; } cout << ans << '\n'; }
#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...