Submission #1305179

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

int main(){
    int S, N;
    cin >> S >> N;

    vector<vector<int>> v(N, vector<int>(3));
    for(int i = 0; i < N; i++){
        cin >> v[i][0] >> v[i][1] >> v[i][2]; 
        // value, weight, count
    }

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

    for(int i = 0; i < N; i++){
        int val = v[i][0];
        int wt  = v[i][1];
        int k   = v[i][2];

        // binary splitting
        for(int p = 1; k > 0; p <<= 1){
            int take = min(p, k);
            k -= take;

            int total_wt = take * wt;
            long long total_val = 1LL * take * val;

            for(int w = S; w >= total_wt; w--){
                dp[w] = max(dp[w], dp[w - total_wt] + total_val);
            }
        }
    }

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