Submission #959831

#TimeUsernameProblemLanguageResultExecution timeMemory
959831susanthenerdKnapsack (NOI18_knapsack)C++17
100 / 100
62 ms35164 KiB
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <climits>


using namespace std;
using ll = long long;

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

    int s, n, m = 0;
    cin >> s >> n;

    vector v(s + 1, vector<pair<int, int>>());

    for (int i = 0; i < n; ++i) {
        int pret, greu, cnt;
        cin >> pret >> greu >> cnt;

        if (greu <= s && cnt > 0) {
            if (v[greu].empty())
                ++m;
            v[greu].emplace_back(pret, cnt);
        }
    }

    vector dp(m + 1, vector<ll>(s + 1, LLONG_MIN));
    dp[0][0] = 0;

    for (int a = 0, i = 0; a <= s; ++a) {
        if (!v[a].empty()) {
            ++i;

            sort(v[a].begin(), v[a].end(), greater<>());
            for (int j = 0; j <= s; ++j) {
                dp[i][j] = dp[i - 1][j];

                int cnt = 0, tip = 0, used = 0;
                ll profit = 0;

                while ((cnt + 1) * a <= j && tip < v[a].size()) {
                    ++cnt;
                    profit += v[a][tip].first;

                    if (dp[i - 1][j - cnt * a] != LLONG_MIN)
                        dp[i][j] = max(dp[i][j], dp[i - 1][j - cnt * a] + profit);

                    ++used;
                    if (used == v[a][tip].second) {
                        used = 0;
                        ++tip;
                    }
                }
            }
        }
    }

    cout << *max_element(dp.back().begin(), dp.back().end()) << '\n';
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:45:50: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |                 while ((cnt + 1) * a <= j && tip < v[a].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...