Submission #1112267

#TimeUsernameProblemLanguageResultExecution timeMemory
1112267shfvbhwbrvjKnapsack (NOI18_knapsack)C++17
100 / 100
132 ms4044 KiB
#include <bits/stdc++.h>
using namespace std;

int s, n, maxv;
map<int, vector<pair<int, int>>> item;
vector<int> dp(2001);
vector<bool> vis(2001);

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> s >> n;
    for (int i = 1; i <= n; i++) {
        int v, w, k;
        cin >> v >> w >> k;
        item[w].push_back(make_pair(v, k));
    }

    vis[0] = true;
    for (auto [w, i] : item) {
        sort(i.begin(), i.end(), greater<pair<int, int>>());
        int cnt = 0;
        for (int k = 0; k * w < s; k++) {
            if (cnt >= i.size()) {
                break;
            }
            for (int j = s; j >= w; j--) {
                if (vis[j - w]) {
                    dp[j] = max(dp[j], dp[j - w] + i[cnt].first);
                    vis[j] = true;
                    maxv = max(maxv, dp[j]);
                }
            }
            i[cnt].second--;
            if (!i[cnt].second) {
                cnt++;
            }
        }
    }

    cout << maxv << endl;

    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:26:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |             if (cnt >= i.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...