Submission #1300279

#TimeUsernameProblemLanguageResultExecution timeMemory
1300279javahirbekKnapsack (NOI18_knapsack)C++20
37 / 100
1 ms580 KiB
#include <bits/stdc++.h>
using namespace std;

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

    int S, N;
    cin >> S >> N;

    vector<int> Weights(N), Values(N), Cnt(N);
    for (int i = 0; i < N; i++) {
        cin >> Values[i] >> Weights[i] >> Cnt[i];
    }

    vector<pair<int,int>> items;
    for (int i = 0; i < N; i++) {
        int w = Weights[i];
        int v = Values[i];
        int c = Cnt[i];
        int k = 1;
        while (c > 0) {
            int take = min(k, c);
            items.push_back({w * take, v * take});
            c -= take;
            k *= 2;
        }
    }

    vector<int> dp(S + 1, 0);

    for (auto &[w, v] : items) {
        for (int j = S; j >= w; j--) {
            dp[j] = max(dp[j], dp[j - w] + v);
        }
    }

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