제출 #976668

#제출 시각아이디문제언어결과실행 시간메모리
976668vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
61 ms4820 KiB
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size
#define fr front 
#define bc backn
#define pii pair<int,int>
#define all(x) x.begin() , x.end()
#define cmin(a, b) a = min(a , b)
#define cmax(a, b) a = max(a, b)
#define mems(arr , a) memset(arr , a , sizeof arr)
#define each(i , arr) for(auto &i : arr)
const int MOD = 1e9 + 7;
const int INF = 1e18;
int n , c;

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;  
    t = 1;
    // cin >> t;
    while(t--){
        cin >> c >> n;
        vector<vector<pii>> arr(c+1);
        for(int i = 0; i < n; i++){
            int v , w , k;  cin >> v >> w >> k;
            arr[w].pb({v , k});
        }
        vector<pii> item;
        for(int i = 1; i <= c; i++){
            if(arr[i].empty()) continue;
            sort(all(arr[i]));
            int tmp = (c+i-1)/i;
            while(tmp > 0 && arr[i].sz() > 0){
                auto [v , k] = arr[i].back();
                while(tmp > 0 && k > 0){
                    item.pb({v , i});
                    tmp--;
                    k--;
                }
                if(k == 0) arr[i].pob();
            }
        }
        int dp[c+1] = {};
        for(auto i : item){
            for(int w = c; w >= 0; w--){
                if(w >= i.se){
                    dp[w] = max(dp[w] , dp[w-i.se] + i.fi);
                }
            }
        }
        cout << dp[c];
    }


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