Submission #756392

#TimeUsernameProblemLanguageResultExecution timeMemory
756392asdasdqwerKnapsack (NOI18_knapsack)C++14
73 / 100
457 ms262144 KiB
#include <bits/stdc++.h> using namespace std; int main() { int s, n; cin >> s >> n; vector<int64_t> dp(s+1, 0); vector<int> div(s+1); if (n == 1) { int64_t v, w, k; cin >> v >> w >> k; int times = min(s / w, k); cout << v * times << "\n"; return 0; } // calculate div for (int i = 1; i <= s; i++) { div[i] = s / i; } vector<vector<int>> value(s+1, vector<int>()); for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; int m = min(div[w], k); for (int j = 0; j < m; j++) { value[w].push_back(v); } } for (int i = 1; i <= s; i++) { sort(value[i].begin(), value[i].end(), greater<int>()); div[i] = min(div[i], (int)value[i].size()); } for (int i = s; i > 0; i--) { for (int j = 0; j < div[i]; j++) { int v = value[i][j]; for (int k = s; k > 0; k--) { if ((k - i) >= 0) { dp[k] = max(dp[k], dp[k - i] + v); } } } } 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...