Submission #1156998

#TimeUsernameProblemLanguageResultExecution timeMemory
1156998escobrandKnapsack (NOI18_knapsack)C++20
100 / 100
59 ms33352 KiB
#include <bits/stdc++.h>

using namespace std;
#define all(v) v.begin(),v.end()
#define eb push_back
#define ll long long
#define fi first
#define se second
int i,n,t,m,v,w,cap,j;
const int maxn = 2e3 + 10;
ll dp[maxn][maxn],ans;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);


    cin>>cap>>n;
    map<int,vector<pair<int,int>>> mp;
    for(i = 1;i<=n;i++)
    {
        cin>>v>>w>>m;
        mp[w].eb({v,m});
    }

    for(i = 0;i<=mp.size();i++)
    {
        for(j = 0;j<=cap;j++)
        {
            dp[i][j] = LLONG_MIN;
        }
    }
    dp[0][0] = 0;
    i = 0;
    for(auto &[w,a] : mp)
    {
        i++;
        sort(all(a),greater<>());
        for(j = 0;j<=cap;j++)
        {
            dp[i][j] = dp[i-1][j];
            int c = 0,cc = 0,id = 0;
            ll val = 0;
            while(j >= (c + 1) * w)
            {
                c++;
                cc++;
                val += a[id].fi;
                if(dp[i-1][j- c * w] != LLONG_MIN)
                {
                    dp[i][j] = max(dp[i][j],dp[i-1][j- c * w] + val);
                }
                if(cc == a[id].se)
                {
                    id++;
                    cc = 0;
                    if(id == a.size())break;
                }
            }
            ans = max(ans,dp[i][j]);
        }
    }

    cout<<ans;
    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...