Submission #799655

#TimeUsernameProblemLanguageResultExecution timeMemory
799655rocketsriKnapsack (NOI18_knapsack)C++17
0 / 100
1 ms468 KiB
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    vector<int> w(n);
    vector<int> freq(n);

    unordered_map<int, vector<pair<int, int>>> pairs; // (weight, {{value, frequency}})
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> w[i] >> freq[i];
    }

    for (int i = 0; i < n; i++) {
        if (pairs.find(w[i]) != pairs.end()) {
            pairs[w[i]].push_back({a[i], freq[i]});
        }
        else {
            pairs[w[i]] = {{a[i], freq[i]}};
        }
    }

    vector<long long> dp(m + 1, -1);
    dp[0] = 0;

    for (auto& entry : pairs) {
        int weight = entry.first;
        auto& curr = entry.second;
        sort(curr.begin(), curr.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.second < b.second;
        });
    }

    for (auto& entry : pairs) {
        int weight = entry.first;
        auto& curr = entry.second;
        long long sum = 0;
        for (const auto& j : curr) {
            sum += j.second;
        }
        for (int i = m; i >= weight; i--) {
            long long f = i / weight;
            if (dp[i - min(f, sum) * weight] != -1) {
                long long c = dp[i - min(f, sum) * weight];
                for (const auto& j : curr) {
                    long long fr = j.second, val = j.first;
                    if (fr > f) {
                        c += f * val;
                        f = 0;
                        break;
                    }
                    else {
                        c += fr * val;
                        f -= fr;
                    }
                }
                dp[i] = max(dp[i], c);
            }
        }
    }

    long long max_val = 0;
    for (int i = 0; i <= m; i++) {
        max_val = max(max_val, dp[i]);
    }

    cout << max_val << endl;

    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:32:13: warning: unused variable 'weight' [-Wunused-variable]
   32 |         int weight = entry.first;
      |             ^~~~~~
#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...