Submission #724048

#TimeUsernameProblemLanguageResultExecution timeMemory
724048BodishaKnapsack (NOI18_knapsack)C++17
100 / 100
164 ms33060 KiB
#include <bits/stdc++.h>
#define ll long long int

using namespace std;

map<int, vector<pair<int, int>>> by_weight;

int main() {
    int s, n;
    cin >> s >> n;
    for(int i = 0; i < n; i++) {
        int temp_v, temp_w, temp_k;
        cin >> temp_v >> temp_w >> temp_k;
        if(temp_w <= s && temp_k > 0) {
            by_weight[temp_w].push_back({temp_v, temp_k});
        }
    }
    ll dp[by_weight.size() + 1][s + 1];
    for(int i = 0; i <= by_weight.size(); i++) {
        for(int j = 0; j <= s; j++) {
            dp[i][j] = 0;
        }
    }
    dp[0][0] = 0;
    int at = 1;
    for(auto &[w, items] : by_weight) {
        sort(items.begin(), items.end(), greater<pair<int, int>>());
        for(int i = 0; i <= s; i++) {
            dp[at][i] = dp[at - 1][i];
            int curr_type = 0;
            int used_weight = 0;
            int copy_count = 0;
            ll profit = 0;
            while(used_weight + w <= i && curr_type < items.size()) {
                copy_count++;
                if(copy_count > items[curr_type].second) {
                    copy_count = 0;
                    curr_type++;
                    continue;
                }
                used_weight += w;
                profit += items[curr_type].first;
                dp[at][i] = max(dp[at][i], dp[at - 1][i - used_weight] + profit);
            }
        }
        at++;
    }
    ll ans = 0;
    for(int i = 0; i <= s; i++) {
        ans = max(ans, dp[by_weight.size()][i]);
    }
    cout << ans;
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:19:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for(int i = 0; i <= by_weight.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~~~~
knapsack.cpp:34:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |             while(used_weight + w <= i && curr_type < items.size()) {
      |                                           ~~~~~~~~~~^~~~~~~~~~~~~~
#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...