Submission #1291082

#TimeUsernameProblemLanguageResultExecution timeMemory
1291082lechaaKnapsack (NOI18_knapsack)C++20
0 / 100
1 ms572 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 = 1;
        int now = 0;
        int hm = 0;
        for(auto g : weight[i]){
            int k = g.second;
            while(k > 0){
                int need = cur-now;
                if(k >= need){
                    hm += need*g.first;
                    now = cur;
                    x.push_back({now * i, hm});
                    cur*=2;
                    k -= need;
                    now = 0;
                    hm = 0;
                }else{
                    now += k;
                    hm += g.first * i;
                    k = 0;
                }
            }
            x.push_back({now * i, hm});
        }
    }
    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...