Submission #920654

#TimeUsernameProblemLanguageResultExecution timeMemory
920654gustavo_dKnapsack (NOI18_knapsack)C++17
73 / 100
1054 ms29328 KiB
// https://oj.uz/problem/view/NOI18_knapsack > p83 e p199
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("03,unroll-loops")

typedef long long ll;

const ll MAXS = 2000;
const ll MAXLOG = 15;

ll dp[2][MAXS+1];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
 
    ll S, n; cin >> S >> n;
    vector<ll> val(n);
    vector<ll> w(n);
    vector<ll> K(n);
    vector<pair<ll,ll>> v; // peso, valor
    v.reserve(n);
    ll tmpn=n;
    for (ll i=0; i<n; i++) {
        cin >> val[i] >> w[i] >> K[i];
        K[i] = min(K[i], (S/w[i]));

        bool fi = true;
        ll other = K[i];
        for (ll b=MAXLOG; b>=0; b--) {
            if (fi and ((1 << b) & K[i]) != 0) {
                fi = false;
                other -= ((1 << b) - 1);
            } else if (!fi) {
                tmpn++;
                v.push_back({(1 << b) * w[i], (1 << b) * val[i]});
            }
        }
        v.push_back({other * w[i], other * val[i]});
    }

    for (ll i=1; i<=tmpn; i++) {
        for (ll P=0; P<=S; P++) {
            if (P != 0) {
                dp[i&1][P] = max(dp[(i&1)^1][P], dp[i&1][P-1]);
            } else {
                dp[i&1][P] = dp[(i&1)^1][P];
            }
            if (P>=v[i-1].first) {
                dp[i&1][P] = max(
                    dp[i&1][P],
                    dp[(i&1)^1][P-v[i-1].first] + v[i-1].second
                );
            }
        }
    }
 
    cout << dp[tmpn&1][S] << endl;
 
    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...