제출 #1291855

#제출 시각아이디문제언어결과실행 시간메모리
1291855atharva0300Knapsack (NOI18_knapsack)C++20
37 / 100
2 ms580 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int S, N;
    cin >> S >> N;
    
    vector<pair<int, int>> items; // {weight, value}
    
    for (int i = 0; i < N; i++) {
        int V, W, K;
        cin >> V >> W >> K;
        
        // Binary representation optimization
        int cnt = 1;
        while (cnt <= K) {
            items.push_back({W * cnt, V * cnt});
            K -= cnt;
            cnt *= 2;
        }
        if (K > 0) {
            items.push_back({W * K, V * K});
        }
    }
    
    // Standard 0/1 knapsack with space optimization
    vector<int> dp(S + 1, 0);
    
    for (auto& [weight, value] : items) {
        for (int w = S; w >= weight; w--) {
            dp[w] = max(dp[w], dp[w - weight] + value);
        }
    }
    
    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...