제출 #1184011

#제출 시각아이디문제언어결과실행 시간메모리
1184011ayushtiwari110Knapsack (NOI18_knapsack)C++20
37 / 100
1 ms328 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int S, N;
    cin >> S >> N;
    
    // DP array - dp[w] is maximum value with weight w
    vector<long long> dp(S + 1, 0);
    
    for (int i = 0; i < N; i++) {
        int v, w, k;
        cin >> v >> w >> k;
        
        // Skip items that are too heavy
        if (w > S) continue;
        
        // Binary representation approach
        int count = k;
        for (int p = 1; count > 0; p <<= 1) {
            int take = min(p, count);
            count -= take;
            
            int bundle_weight = take * w;
            
            // Skip if bundle is too heavy
            if (bundle_weight > S) continue;
            
            long long bundle_value = static_cast<long long>(take) * v;
            
            // Update DP array
            for (int weight = S; weight >= bundle_weight; weight--) {
                dp[weight] = max(dp[weight], dp[weight - bundle_weight] + bundle_value);
            }
        }
    }
    
    cout << dp[S] << endl;
    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...