This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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 time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |