Submission #963223

#TimeUsernameProblemLanguageResultExecution timeMemory
963223biankKnapsack (NOI18_knapsack)C++14
73 / 100
1057 ms2652 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    int n, s;
    cin >> s >> n;
    vector<ll> v(n), w(n), k(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i] >> w[i] >> k[i];
    }
    vector<ll> dp(s + 1, 0);
    auto add = [&](ll p, ll c) {
        for (int i = s; i >= c; i--) {
            dp[i] = max(dp[i], dp[i - c] + p);
        }
    };
    for (int i = 0; i < n; i++) {
        int j = 0;
        while (1 << j <= k[i] && w[i] << j <= s) {
            add(v[i] << j, w[i] << j);
            k[i] -= 1 << j;
            j++;
        }
        if (k[i] > 0) add(v[i] * k[i], w[i] * k[i]);
    }
    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...