Submission #1192588

#TimeUsernameProblemLanguageResultExecution timeMemory
1192588dprtoKnapsack (NOI18_knapsack)C++20
100 / 100
37 ms3912 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
#define MASK(i) (1 << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define f(i, l, r) for(int i = l; i <= r; ++i)
const int mod = 1e9 + 7;
const int ars = 1e5 + 5;
const ll infll = 1e18;


int s, n, dp[ars];
vector<pair<int, int>> wei[ars];
signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> s >> n;

    for(int i = 1; i <= n; ++i){
        int v, w, k;
        cin >> v >> w >> k;
        if(w > s) continue;
        wei[w].push_back({v, k});
    }

    for(int i = 1; i <= s; ++i) dp[i] = -1e9;
    dp[0] = 0;

    for(int w = 1; w <= s; ++w){
        sort(wei[w].begin(), wei[w].end(), greater<>());
    }

    for(int w = 1; w <= s; ++w){
        if(wei[w].empty()) continue;

        vector<int> items;
        int limit = s / w;
        for(auto [val, cnt] : wei[w]){
            int take = min(cnt, limit);
            limit -= take;
            while(take--) items.push_back(val);
            if(limit == 0) break;
        }

        for(int v : items){
            for(int i = s; i >= w; --i){
                if(dp[i - w] != -1e9){
                    dp[i] = max(dp[i], dp[i - w] + v);
                }
            }
        }
    }

    int res = 0;
    for(int i = 1; i <= s; ++i){
        res = max(res, dp[i]);
    }
    cout << res << "\n";

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