제출 #1255208

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

typedef long long ll;
typedef vector<ll> vl;

#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PB push_back

int main() {
    optimize();

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

    vector<vector<pair<ll, ll>>> by_weight(S + 1); 

    for (int i = 0; i < N; ++i) {
        ll v, w, k;
        cin >> v >> w >> k;
        by_weight[w].emplace_back(v, k);
    }

    vl dp(S + 1, 0);

    for (int w = 1; w <= S; ++w) {
        if (by_weight[w].empty()) continue;


        for (auto [v, k] : by_weight[w]) {

            for (ll t = 1; k > 0; t <<= 1) {
                ll use = min(t, k);
                k -= use;
                ll tv = v * use;
                ll tw = w * use;

                for (int j = S; j >= tw; --j) {
                    dp[j] = max(dp[j], dp[j - tw] + tv);
                }
            }
        }
    }

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