Submission #1153084

#TimeUsernameProblemLanguageResultExecution timeMemory
1153084murpylKnapsack (NOI18_knapsack)C++20
100 / 100
67 ms34376 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define endl "\n"
#define mp make_pair
void setIO(string name = "") {
	ios_base::sync_with_stdio(0); cin.tie(0);
	if(sz(name)){
		freopen((name+".in").c_str(), "r", stdin); 
		freopen((name+".out").c_str(), "w", stdout);
	}
}


signed main(){
    setIO();
    int s, n;
    cin>>s>>n;
    map<int, vector<pi>> items;
    for (int i = 0; i < n; i++){
        int val, weight, quant;
        cin>>val>>weight>>quant;
        if (weight <= s && quant >= 0) items[weight].pb({val, quant});
        //group shit by weight
        //and u only care about s/w_i items
    }
    
    vector<vector<int>> dp(items.size()+1, vi(s+1, INT_MIN));
    dp[0][0] = 0;
    int i = 1;
    for (auto &[weight, item]: items){
        sort(all(item), greater<pi>());
        for (int j = 0; j <= s; j++){
            dp[i][j] = dp[i-1][j];
            int profit = 0;
            int cur = 0; 
            int used = 0;
            int temp_used = 0;
            // go through as many items until we run out of items or usable
			// weight
            while ((used+1)*weight<=j&&cur<item.size()){
                used++;
                temp_used++;
                profit+=item[cur].first;
                if (dp[i-1][j-used*weight] != INT_MIN){
                    dp[i][j] = max(dp[i][j], dp[i-1][j-used*weight]+profit);
                }
                if (temp_used == item[cur].second){
                    temp_used = 0;
                    cur++;
                }
            }
        }
        i++;
    }
    int ans = 0;
    for (int i = 0; i <= s; i++){
        ans = max(ans, dp[sz(items)][i]);
    }
    cout<<ans<<endl;
}

Compilation message (stderr)

knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |                 freopen((name+".in").c_str(), "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |                 freopen((name+".out").c_str(), "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...