Submission #680154

#TimeUsernameProblemLanguageResultExecution timeMemory
680154WobertKnapsack (NOI18_knapsack)C++17
100 / 100
114 ms3988 KiB
#include "bits/stdc++.h" #define fox(i, n) for (int i = 0; i < n; ++i) #define rep(i, a, b) for (int i = a; i <= b; ++i) #define pb push_back #define eb emplace_back #define tomax(a, b) ((a) = max((a), (b))) #define tomin(a, b) ((a) = min((a), (b))) #define F first #define S second using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int MAXW = 2005, MEMSET_INF = 127; int n, s; vector<pii> items[MAXW]; // {value, copies} vector<pii> e; // {value, weight} int wv[MAXW]; int main() { cin >> s >> n; fox(i, n) { int v, w, k; cin >> v >> w >> k; items[w].eb(v, k); } rep(i, 1, s) { sort(items[i].rbegin(), items[i].rend()); int cnt = s/i + 1, idx = 0; while (cnt && idx != (int)items[i].size()) { if (items[i][idx].S == 0) { ++idx; continue; } items[i][idx].S--; cnt--; e.eb(items[i][idx].F, i); } } wv[0] = 0; for (auto [val, wt] : e) { for (int i = s; i-wt >= 0; --i) { tomax(wv[i], wv[i-wt] + val); } } cout << wv[s] << '\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...