Submission #1020969

#TimeUsernameProblemLanguageResultExecution timeMemory
1020969urejgiKnapsack (NOI18_knapsack)C++17
73 / 100
1058 ms16300 KiB
#include <iostream>
#include <vector>
#define int long long
using namespace std;

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int s, n;
    cin >> s >> n;
    vector<int> value, weight;
    int v, w, k;
    for (int i = 0; i < n; ++i) {
        cin >> v >> w >> k;
        int count = 1;
        while (k > 0) {
            int currentCount = min(count, k);
            value.push_back(currentCount * v);
            weight.push_back(currentCount * w);
            k -= currentCount;
            count <<= 1;
        }
    }
    int totalItems = value.size();
    vector<int> dp(s + 1, 0);
    dp.reserve(s + 1); // preallocate memory
    for (int i = 0; i < totalItems; ++i) {
        for (int j = s; j >= weight[i]; --j) {
            if (j - weight[i] < 0) break; // avoid unnecessary computations
            dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
        }
    }
    cout << dp[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...