Submission #815241

#TimeUsernameProblemLanguageResultExecution timeMemory
815241AlphaMale06Knapsack (NOI18_knapsack)C++14
100 / 100
195 ms248912 KiB
#include <bits/stdc++.h>


using namespace std;

#define pb push_back
#define int long long
#define mp make_pair
#define F first
#define S second
struct item{
    int w;
    int v;
    int k;
};

bool compare(item a, item b){
    if(a.w==b.w){
        return a.v>b.v;
    }
    return a.w<b.w;
}
int dp[18000][2010];
signed main()
{
    int w, n;
    cin >> w >> n;
    item a[n];
    for(int i=0; i< n; i++){
        cin >> a[i].v >> a[i].w >> a[i].k;
    }
    sort(a, a+n, compare);
    vector<pair<int, int>> b;
    int cnt[w+1]={0};
    for(int i=0; i< n; i++){
        while(cnt[a[i].w]<w/a[i].w && a[i].k > 0){
            cnt[a[i].w]++;
            a[i].k--;
            b.pb(mp(a[i].w, a[i].v));
        }
    }
    n=b.size();
    for(int i=1; i<= n; i++){
        for(int j=0; j<=w; j++){
            if(b[i-1].F<=j){
                dp[i][j]=max(dp[i-1][j], dp[i-1][j-b[i-1].F]+b[i-1].S);
            }
            else{
                dp[i][j]=dp[i][j-1];
            }
        }
    }
    cout << dp[n][w] << '\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...