Submission #484425

#TimeUsernameProblemLanguageResultExecution timeMemory
484425MOHITSINGHALKnapsack (NOI18_knapsack)C++17
100 / 100
66 ms7364 KiB
/* Author:- Mohit Singhal */

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define PI 3.14159265
#define ll long long
#define vii vector<int, int>
#define vll vector<long long, long long>
#define vi vector<int>
#define vl vector<long long>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// define ordered_multiset tree< pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>
// define ordered_set tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>


int main(){
    fastio;
    ll s, n;
    cin >> s >> n;
    ll v[n], w[n], k[n];
    for(int i = 0;i<n;i++){
        cin >> v[i] >> w[i] >> k[i];
    }

    priority_queue<pair<ll, ll>> pq[s+1];
    
    for(int i = 0;i<n;i++){
        pq[w[i]].push({v[i], k[i]});
    }


    vector<ll> values[s+1];
    for(int i = 1;i<=s;i++){
        values[i].push_back(0);
        ll currValue = 0;
        ll currWeight = i;
        while(pq[i].size() > 0 && currWeight <= s){
            pair<ll, ll> p = pq[i].top();
            currValue+= p.first;
            values[i].push_back(currValue);
            currWeight+=i;
            p.second--;
            pq[i].pop();
            if(p.second > 0)
                pq[i].push(p);
        }
    }


    ll dp[s+1][2];
    for(int i = 0;i<=s;i++){
        dp[i][0] = 0;
    }

    dp[0][1] = 0;

    // for(int i = 1;i<=s;i++){
    //     for(auto ele: values[i]){
    //         cout << ele << " ";
    //     }
    //     cout << endl;
    // }


    for(int i = 1;i<=s;i++){
        for(int j = 1;j<=s;j++){
            dp[j][i%2] = dp[j][(i+1)%2];
            int k = 1;
            while(k < values[i].size() && j - i*k >= 0){
                dp[j][i%2] = max(dp[j][i%2], dp[j - i*k][(i+1)%2] + values[i][k])  ;
                k++;
            }
        }
        // cout << dp[s][i%2] << endl;
    }

    cout << dp[s][s%2] << endl;

}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:73:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |             while(k < values[i].size() && j - i*k >= 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...