Submission #986224

#TimeUsernameProblemLanguageResultExecution timeMemory
986224vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
61 ms3776 KiB
#include <bits/stdc++.h>
using i64 = long long;

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

    int S, N;
    std::cin >> S >> N;
    
    std::vector item(1 + S, std::vector<std::tuple<int, int>>());
    for (int i = 0; i < N; i++) {
        int v, w, k;
        std::cin >> v >> w >> k;

        item[w].push_back({v, k});
    }

    std::vector<int> dp(1 + S);
    for (int w = 1; w <= S; w++) {
        if (item[w].empty()) {
            continue;
        }

        std::sort(item[w].begin(), item[w].end(), std::greater<std::tuple<int, int>>());
        for (int j = S; j >= 0; j--) {
            [&]{
                int c = 0, val = 0;
                for (auto [v, k] : item[w]) {
                    for (int i = 0; i < k; i++) {
                        val += v, c++;
                        if (j + c * w > S) {
                            return;
                        }
                        dp[j + c * w] = std::max(dp[j + c * w], dp[j] + val);
                    }
                }
            }();
        }
    }
    std::cout << dp[S] << "\n";

    return 0;
}
#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...