제출 #1175618

#제출 시각아이디문제언어결과실행 시간메모리
1175618FZ_LaabidiKnapsack (NOI18_knapsack)C++20
73 / 100
1097 ms8824 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<int> dp(s + 1, 0);
    vector<pair<int,int>> items; // {weight, value}
    
    for (int i = 0; i < n; ++i) {
        int v, w, k; cin >> v >> w >> k;
        k = min(k, s / w); // No more than what fits
        
        for (int x = 1; k > 0; x *= 2) {
            int take = min(x, k);
            items.emplace_back(take * w, take * v);
            k -= take;
        }
    }
    
    // Standard 0-1 knapsack
    for (auto [w, v] : items) {
        for (int j = s; j >= w; --j) {
            dp[j] = max(dp[j], dp[j - w] + v);
        }
    }
    
    cout << dp[s] << '\n';
}
#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...