Submission #756477

#TimeUsernameProblemLanguageResultExecution timeMemory
756477asdasdqwerKnapsack (NOI18_knapsack)C++14
100 / 100
135 ms3788 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<vector<pair<int,int>>> pairs(s+1, vector<pair<int,int>>()); for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; int m = min(div[w], k); pairs[w].push_back(make_pair(v, m)); } for (int i = s; i > 0; i--) { sort(pairs[i].begin(), pairs[i].end(), greater<pair<int, int>>()); int c = 0, pos = 0; value[i] = vector<int>(div[i], 0); int size = pairs[i].size(); while ((c < div[i]) && (pos < size)) { for (int j = 0; j < pairs[i][pos].second && c < div[i]; j++) { value[i][c] = pairs[i][pos].first; c++; } pos++; } } for (int i = s; i > 0; i--) { for (int j = 0; j < div[i]; j++) { for (int k = s; k >= i; k--) { if (dp[k] < dp[k - i] + value[i][j]) { dp[k] = max(dp[k], dp[k - i] + value[i][j]); } } } } 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...