Submission #942295

#TimeUsernameProblemLanguageResultExecution timeMemory
942295TrendBattlesKnapsack (NOI18_knapsack)C++14
73 / 100
1057 ms5856 KiB
//https://oj.uz/problem/view/NOI18_knapsack

#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;

const lli inf = 0x3f3f3f3f3f3f3f3f;

void maximise(lli& x, lli y) {
    if (x < y) x = y;
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, S; cin >> S >> n;

    vector <vector <int>> value(S + 1);

    for (int i = 1; i <= n; ++i) {
        int v, w, k; cin >> v >> w >> k;

        int pow_2 = 1;
        while (k >= pow_2) {
            if ((lli) w * pow_2 <= S) {
                value[w * pow_2].push_back(v * pow_2);
            }
            k -= pow_2;
            pow_2 <<= 1;
        }

        if (k > 0 and (lli) w * k <= S) {
            value[w * k].push_back(v * k);
        }
    }

    vector <lli> dp(S + 1);

    for (int i = 1; i <= S; ++i) {
        for (int v : value[i]) {
            for (int j = S; j >= i; --j) {
                maximise(dp[j], dp[j - i] + v);
            }
        }
    }

    cout << dp[S];
    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...