제출 #1341231

#제출 시각아이디문제언어결과실행 시간메모리
1341231kiengytKnapsack (NOI18_knapsack)C++20
73 / 100
1096 ms432 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int S_MAX = 100005; 
ll dp[S_MAX]; 

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

    int s, n;
    if (!(cin >> s >> n)) return 0;

    fill(dp, dp + s + 1, 0);

    for (int i = 0; i < n; i++) {
        ll v_i, w_i, k_i;
        cin >> v_i >> w_i >> k_i;
        
        for (ll j = 1; k_i > 0; j <<= 1) {
            ll num = min(j, k_i);
            ll V = num * v_i;
            ll W = num * w_i;

            for (int j_s = s; j_s >= W; j_s--) {
                if (dp[j_s - W] + V > dp[j_s]) {
                    dp[j_s] = dp[j_s - W] + V;
                }
            }
            k_i -= num;
        }
    }

    ll ans = 0;
    for (int j = 0; j <= s; j++) ans = max(ans, dp[j]);
    cout << ans;

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