Submission #985828

#TimeUsernameProblemLanguageResultExecution timeMemory
985828vjudge1Knapsack (NOI18_knapsack)C++17
29 / 100
3 ms472 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::map<int, std::vector<std::pair<int, int>>> item;
    for (int i = 0; i < N; i++) {
        int v, w, k;
        std::cin >> v >> w >> k;
        
        item[w].push_back({v, k});
    }

    std::vector<i64> dp(1 + S);

    for (auto &[w, vec] : item) {
        std::sort(vec.begin(), vec.end(), std::greater<std::pair<int, int>>());

        for (int j = S; j >= 0; j--) {
            i64 val = 0;
            for (int c = 0, i1 = 0, i2 = 0; c * w <= S; c++) {
                if (j - c * w >= 0) {
                    dp[j] = std::max(dp[j], dp[j - c * w] + val);
                }

                if (i2 == vec[0].second) {
                    i1++, i2 = 0;
                }

                if (i1 == (int)vec.size()) {
                    break;
                }
                val += vec[i1].first, i2++;
            }
        }
    }

    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...