Submission #1259854

#TimeUsernameProblemLanguageResultExecution timeMemory
1259854tareqKnapsack (NOI18_knapsack)C++20
73 / 100
1093 ms8624 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<pair<int,int>> items; 
    for (int i = 0; i < N; i++) {
        long long V, W, K;
        cin >> V >> W >> K;

        long long maxCopies = min(K, (long long)S / W);

        long long power = 1;
        while (maxCopies > 0) {
            long long take = min(power, maxCopies);
            items.push_back({(int)(W * take), (int)(V * take)});
            maxCopies -= take;
            power <<= 1;
        }
    }

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

    for (auto [w, v] : items) {
        for (int cap = S; cap >= w; cap--) {
            dp[cap] = max(dp[cap], dp[cap - 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...