제출 #1291856

#제출 시각아이디문제언어결과실행 시간메모리
1291856atharva0300Knapsack (NOI18_knapsack)C++20
37 / 100
1 ms660 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, long long>> items; // {weight, value}
    
    for (int i = 0; i < N; i++) {
        long long V;
        int 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<long long> 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...