제출 #908536

#제출 시각아이디문제언어결과실행 시간메모리
908536KactusJackKnapsack (NOI18_knapsack)C++14
100 / 100
35 ms4912 KiB
#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
using namespace std;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int s, n;
    cin >> s >> n;
    vector<vector<pair<ll, int>>> a(s+1);
    for(int i = 0; i < n; i++){
        ll v, w, k;
        cin >> v >> w >> k;
        a[w].push_back({v, k});
    }
    for(int w = 1; w <= s; w++){
        sort(a[w].begin(), a[w].end(), greater<pair<ll, int>>());
    }
    vector<vector<ll>> dp(2, vector<ll> (s+1));
    for(int w = 1; w <= s; w++){
        ll tot_weight = 0, tot_val = 0;
        for(auto pr : a[w]){
            while(pr.S > 0 && tot_weight <= s){
                tot_weight += w;
                tot_val += pr.F;
                pr.S--;
                for(int i = s; i >= tot_weight; i--){
                    dp[1][i] = max(dp[1][i], tot_val + dp[0][i - tot_weight]);
                }
            }
        }
        dp[0] = dp[1];
    }
    cout << dp[0][s] << "\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...