Submission #1309634

#TimeUsernameProblemLanguageResultExecution timeMemory
1309634husseinjuandaKnapsack (NOI18_knapsack)C++20
100 / 100
55 ms3156 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int s, n; cin >> s >> n;
    vector<vector<pair<int, int>>> weight(2001);
    for(int i = 0; i < n; i++){
        int v, w, k; cin >> v >> w >> k;
        weight[w].push_back({v, k});
    }
    vector<pair<int, int>> x; //{weight, cost}
    for(int i = 1; i <= 2000; i++){
        sort(weight[i].rbegin(), weight[i].rend());
        int cur = 0;
        for(auto g : weight[i]){
            for(int y = 0; y < g.second; y++){
                if(cur+i <= 2000){
                    x.push_back({i, g.first});
                    cur += i;
                }else{
                    break;
                }
            }
        }
    }
    vector<int> dp(2001, -1e18);
    dp[0] = 0;
    for(int y = 0; y < x.size(); y++){
        for(int i = 2000; i >= 0; i--){
            if(i + x[y].first > 2000 || dp[i] == -1e18) continue;
            dp[i+x[y].first] = max(dp[i+x[y].first], dp[i] + x[y].second);
        }
    }
    int mx = 0;
    for(int i = s; i >= 0; i--){
        mx = max(mx, dp[i]);
    }
    cout << mx << "\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...