Submission #491185

#TimeUsernameProblemLanguageResultExecution timeMemory
491185tarcheKnapsack (NOI18_knapsack)C++17
0 / 100
1 ms972 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); 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); 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...