제출 #1290106

#제출 시각아이디문제언어결과실행 시간메모리
1290106hahaKnapsack (NOI18_knapsack)C++20
73 / 100
1096 ms8688 KiB
#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
using namespace std;

const int MAXS = 2005;
int n, S;
ll dp[MAXS];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> S >> n;
    vector<pair<int,int>> items;

    for (int i = 1; i <= n; i++) {
        int v, w, k;
        cin >> v >> w >> k;
        k = min(k, S / w); 
        for (int p = 1; k > 0; p <<= 1) {
            int take = min(p, k);
            k -= take;
            items.push_back({v * take, w * take});
        }
    }

    // classic 0/1 knapsack
    for (auto [val, wei] : items) {
        for (int s = S; s >= wei; s--) {
            dp[s] = max(dp[s], dp[s - wei] + val);
        }
    }

    cout << *max_element(dp, dp + S + 1);
}
#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...