Submission #863432

#TimeUsernameProblemLanguageResultExecution timeMemory
863432KoyoteKnapsack (NOI18_knapsack)C++11
100 / 100
61 ms1916 KiB
#include <bits/stdc++.h>
using namespace std;

const int maxS = 2e3 + 2;
vector<pair<int, int>> items_with_weight[maxS];
int S, n;
long long dp[2][maxS];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> S >> n;
    for (int i = 0; i < n; i++) {
        int v, w, k; cin >> v >> w >> k;
        items_with_weight[w].emplace_back(v, k);
    }

    vector<int> v, w;
    for (int i = 1; i <= S; i++) {
        sort(items_with_weight[i].rbegin(), items_with_weight[i].rend());
        int lim = S / i;
        for (auto j : items_with_weight[i]) {
            while (lim && j.second) 
                lim--, j.second--, v.push_back(j.first), w.push_back(i);
        }
    }

    n = (int)v.size();
    for (int i = 1; i <= n; i++)
        for (int j = S; j >= 0; j--) {
            dp[i & 1][j] = dp[~i & 1][j];
            if (j >= w[i - 1]) dp[i & 1][j] = max(dp[i & 1][j], dp[~i & 1][j - w[i - 1]] + v[i - 1]);
        }
    
    long long ans = 0;
    for (int i = 1; i <= S; i++) ans = max(ans, dp[n & 1][i]);
    cout << ans << '\n';
}
#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...