Submission #872532

#TimeUsernameProblemLanguageResultExecution timeMemory
872532theghostkingKnapsack (NOI18_knapsack)C++14
49 / 100
21 ms32600 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX_W = 2005; int dp[MAX_W][MAX_W]; int search(int i, int remW, vector<pair<int, int>>& kp, int total) { if (i == total || remW == 0) return 0; if (dp[i][remW] != -1) return dp[i][remW]; if (kp[i].first <= remW) { return dp[i][remW] = max(search(i + 1, remW, kp, total), search(i + 1, remW - kp[i].first, kp, total) + kp[i].second); } else { return dp[i][remW] = search(i + 1, remW, kp, total); } } void initializeDP() { for (int i = 0; i < MAX_W; ++i) { for (int j = 0; j < MAX_W; ++j) { dp[i][j] = -1; } } } signed main() { initializeDP(); int s, n; cin >> s >> n; vector<pair<int, int>> arr[MAX_W]; for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; w--; arr[w].push_back({v, k}); } int total = 0; vector<pair<int, int>> kp; for (int i = 0; i < MAX_W; i++) { sort(arr[i].begin(), arr[i].end()); reverse(arr[i].begin(), arr[i].end()); int lft = MAX_W; int sz = arr[i].size(); int pt = 0; while (lft > 0 && pt != sz) { int dec = min(lft, arr[i][pt].second); lft -= dec; for (int j = 0; j < dec; j++) { int val = arr[i][pt].first; kp.push_back({i + 1, val}); } pt++; } total += MAX_W - lft; } cout << search(0, s, kp, total) << '\n'; 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...