제출 #1266226

#제출 시각아이디문제언어결과실행 시간메모리
1266226sathegoodkidKnapsack (NOI18_knapsack)C++20
100 / 100
73 ms3656 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll MOD = 1e9 +7;
int MAX_C = 1e8+1;
int main(){
    int s,n;
    cin >> s >> n;
    vector <int> w(n);
    vector <ll> v(n);
    vector <ll> k(n);
    vector <vector<pair<int,int>>> by_weight(s+1);
    for(int i = 0 ; i < n ;i++){
        cin >> v[i] >> w[i] >> k[i];
        by_weight[w[i]].push_back({v[i],k[i]});
    }
    vector <ll> dp(s+1,0);
    for(int i = 1; i <=s ; i++){
        if(by_weight[i].size() != 0){
            sort(by_weight[i].begin(),by_weight[i].end(),
                [](pair<int,int> &a , pair <int,int> &b){
                    return a.first > b.first;
                });
        }
    }
    for(int i = 1 ; i <= s ; i++){
        if(by_weight[i].size() == 0) continue;
        int total = s/i;
        int a = 0;
        while(a < by_weight[i].size() && total > 0){
            ll check = min(total,by_weight[i][a].second);
            for(int j = s ; j >= i ; j--){
                for(int x = 1; x <= check ; x++){
                    if(j < x*i) break;
                    dp[j] = max(dp[j],dp[j-x*i] + x*by_weight[i][a].first);
                }
                
            }
            total -= check;
            a++;
        };
    }
    cout << dp[s] << endl;
}
#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...