Submission #674692

#TimeUsernameProblemLanguageResultExecution timeMemory
674692ZuraKnapsack (NOI18_knapsack)C++14
100 / 100
119 ms7364 KiB
#include <bits/stdc++.h> #define int long long int #define mod 1000000007 using namespace std; signed main() { // ios_base::sync_with_stdio(NULL); // cin.tie(NULL); // freopen("feast.in", "r", stdin); // freopen("feast.out", "w", stdout); int s, n; cin >> s >> n; int v[n], w[n], k[n]; vector<vector<pair<int, int>>> vs(s + 1); for (int i = 0; i < n; i++) { cin >> v[i] >> w[i] >> k[i]; vs[w[i]].push_back({v[i], k[i]}); } vector<int> val, wt; for (int i = 1; i <= s; i++) { sort(vs[i].begin(), vs[i].end()); int x = s / i, y = (int)vs[i].size() - 1; while (x && y >= 0) { if (vs[i][y].second > x) vs[i][y].second = x; x -= vs[i][y].second; while (vs[i][y].second--) { val.push_back(vs[i][y].first); wt.push_back(i); } y--; } } n = (int)val.size(); int dp[s + 1] = {}; for (int i = 0; i < n; i++) { for (int j = s; j >= wt[i]; j--) { dp[j] = max(dp[j], val[i] + dp[j - wt[i]]); } } cout << dp[s] << "\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...