제출 #1286521

#제출 시각아이디문제언어결과실행 시간메모리
1286521ducmanh2612Knapsack (NOI18_knapsack)C++20
0 / 100
1 ms568 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, S;
    cin >> N >> S;

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

    for (int i = 0; i < N; ++i) {
        int V, W, K;
        cin >> V >> W >> K;

        // Giới hạn số lượng theo sức chứa
        long long max_take = min<long long>(K, S / W);

        // Tách nhị phân
        for (int t = 1; max_take > 0; t <<= 1) {
            int num = min<long long>(t, max_take);
            int weight = num * W;
            int value  = num * V;
            for (int s = S; s >= weight; --s) {
                dp[s] = max(dp[s], dp[s - weight] + value);
            }
            max_take -= num;
        }
    }

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