Submission #877740

#TimeUsernameProblemLanguageResultExecution timeMemory
877740Beerus13Knapsack (NOI18_knapsack)C++14
100 / 100
46 ms3984 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define val first #define sl second #define pii pair<int, int> const int N = 1e5 + 5; int n, S; int dp[2005]; vector<pii> w[N]; bool cmp(pii &a, pii &b) { return a.val > b.val; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> S >> n; for(int i = 1; i <= n; ++i) { int val, W, sl; cin >> val >> W >> sl; w[W].emplace_back(val, sl); } for(int i = 1; i <= S; ++i) { sort(w[i].begin(), w[i].end(), cmp); int cnt = 0; for(pii res : w[i]) { if(cnt >= S / i) break; for(int j = 1; j <= res.sl; ++j) { for(int k = S; k >= i; --k) { dp[k] = max(dp[k], dp[k - i] + res.val); } ++cnt; if(cnt >= S / i) break; } } } int ans = 0; for(int i = 1; i <= S; ++i) ans = max(ans, dp[i]); cout << ans << '\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...