Submission #1014995

#TimeUsernameProblemLanguageResultExecution timeMemory
1014995matus192Knapsack (NOI18_knapsack)C++14
100 / 100
99 ms4988 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define int ll

signed main() {
    int s, n;
    cin >> s >> n;
    vector<vector<pair<int, int>>> items(s + 1);
    for (int i = 0; i < n; i++) {
        int val, w, amount;
        cin >> val >> w >> amount;
        items[w].push_back({val, amount});
    }
    for (auto & vec : items) {
        sort(vec.begin(), vec.end(), greater<pair<int, int>>());
    }
    vector<int> prv(s + 1, 0), akt(s + 1, 0);
    for (int item = 1; item <= s; item++) {
        for (int capacity = 0; capacity <= s; capacity++) {
            akt[capacity] = prv[capacity];
            ll val = 0, w = 0, group = 0, amo = 0;

            while (capacity >= w + item) {
                if (items[item].size() <= group) break;
                if (items[item][group].second <= amo) {
                    amo = 0;
                    group++;
                    continue;
                }

                val += items[item][group].first;
                w += item;
                amo++;

                akt[capacity] = max(akt[capacity], val + prv[capacity - w]);
            }
        }
        prv = akt;
    }
    cout << akt[s] << '\n';

    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:27:40: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   27 |                 if (items[item].size() <= group) break;
      |                     ~~~~~~~~~~~~~~~~~~~^~~~~~~~
#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...