제출 #1331396

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

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

    long long S, n;
    cin >> S >> n;

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

    for (int i = 0; i < n; i++) {
        long long v, w, k;
        cin >> v >> w >> k;

        // Binary splitting
        for (long long j = 1; j <= k; j <<= 1) {
            long long take = min(j, k);
            long long value = v * take;
            long long weight = w * take;

            // 0/1 knapsack transition (backward)
            for (long long s = S; s >= weight; s--) {
                dp[s] = max(dp[s], dp[s - weight] + value);
            }

            k -= take;
        }
    }

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