Submission #1112262

#TimeUsernameProblemLanguageResultExecution timeMemory
1112262shfvbhwbrvjKnapsack (NOI18_knapsack)C++17
73 / 100
1052 ms3016 KiB
#include <bits/stdc++.h>
using namespace std;

int s, n, maxv;
struct item_type {
    int v, w, k;
};
vector<item_type> item;
vector<int> dp(2001);
vector<bool> vis(2001);

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

    cin >> s >> n;
    for (int i = 1; i <= n; i++) {
        int v, w, k;
        cin >> v >> w >> k;
        item_type temp;
        temp.v = v, temp.w = w, temp.k = k;
        item.push_back(temp);  
    }

    vis[0] = true;
    for (auto i : item) {
        for (int k = 0; k * i.w < s && k < i.k; k++) {
            for (int j = s; j >= i.w; j--) {
                if (vis[j - i.w]) {
                    dp[j] = max(dp[j], dp[j - i.w] + i.v);
                    vis[j] = true;
                    maxv = max(maxv, dp[j]);
                }  
            }
        }
    }

    cout << maxv << 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...