Submission #987028

#TimeUsernameProblemLanguageResultExecution timeMemory
987028shokal_kishanKnapsack (NOI18_knapsack)C++17
12 / 100
2 ms432 KiB
#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; #define int long long void solve() { int s, n; cin >> s >> n; unordered_map<int, vector<pair<int, int>>> mp; // Reading input for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; mp[w].push_back({v, k}); } vector<int> dp(s + 1, 0); for (auto& w : mp) { int weight = w.first; auto& items = w.second; sort(items.begin(), items.end(), [](const pair<int, int>& a, const pair<int, int>& b) { return a.first > b.first; // Sort by value descending }); for (int j = s; j >= 0; j--) { int pr = 0, wt = 0; for (auto& item : items) { int value = item.first; int count = item.second; int rm = j - wt; if (rm < weight) break; int max_items = rm / weight; int num_items = min(max_items, count); pr += num_items * value; wt += num_items * weight; } dp[j]=max(dp[j],dp[j-wt]+pr); } } cout << dp[s] << endl; } int32_t main() { int t = 1; // cin >> t; while (t--) solve(); 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...