Submission #956266

#TimeUsernameProblemLanguageResultExecution timeMemory
956266haileyyKnapsack (NOI18_knapsack)C++14
37 / 100
1035 ms348 KiB
//https://oj.uz/problem/view/NOI18_knapsack
#include <bits/stdc++.h>

#define loop(i, l, r) for(int i = (l); i < (r); ++i)
#define pool(i, l, r) for(int i = (l); i > (r); --i)
#define debug(x) cerr << #x << ": " << x << endl;
using namespace std;
int n,s;
const int MAXS = 2005;
long long dp[MAXS];
map<int,vector<pair<int,int>>> items;
bool cmp(pair<int,int> a,pair<int,int> b){
    return a.first>b.first;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>s>>n;
    long long v,w,k;
    loop(i,0,n){
        cin>>v>>w>>k;
        items[w].push_back({v,k});
    }
    for(auto i:items){
        vector<pair<int,int>> a = i.second;
        sort(a.begin(),a.end(),cmp);
        int b = 0;
        for(auto j:a){
            b+=j.second;
        }

        pool(j,s,-1){
            int value = 0;
            int c = 0;
            int count = 0;
            loop(l,1,b+1){
                w = l*i.first;
                if(j>=w){
                    value+=a[c].first;
                    count++;
                    if(count>=a[c].second){
                        c++;
                        count = 0;
                    }
                    dp[j] = max(dp[j],dp[j-w]+value);
                }
            }
        }
    }
    cout<<dp[s]<<endl;
    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...