제출 #1083855

#제출 시각아이디문제언어결과실행 시간메모리
1083855tkvinhtruongquangKnapsack (NOI18_knapsack)C++14
17 / 100
1 ms604 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int S, N; cin >> S >> N; vector<vector<pair<int, int>>> items(S + 1); // items grouped by weight // Read input and group by weight for (int i = 0; i < N; i++) { int V, W, K; cin >> V >> W >> K; items[W].emplace_back(V, K); } vector<long long> dp(S + 1, 0); for (int w = 1; w <= S; w++) { if (!items[w].empty()) { // Sort by value in descending order sort(items[w].rbegin(), items[w].rend()); // Process the most valuable ⌊S/w⌋ items int limit = S / w; for (int j = 0; j < min((int)items[w].size(), limit); j++) { int value = items[w][j].first; int count = items[w][j].second; for (int k = 1; count > 0 && k <= count; k *= 2) { int use = min(k, count); count -= use; for (int s = S; s >= use * w; s--) { dp[s] = max(dp[s], dp[s - use * w] + use * value); } } } } } cout << dp[S] << endl; 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...