Submission #696312

#TimeUsernameProblemLanguageResultExecution timeMemory
696312Alihan_8Knapsack (NOI18_knapsack)C++17
100 / 100
90 ms36300 KiB
#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define int long long
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int S, n; cin >> S >> n;
	vector <pair<int,int>> g[S+1];
	for ( int i = 1; i <= n; i++ ){
        int v, w, k; cin >> v >> w >> k;
        assert(w > 0 and w <= S);
        g[w].pb({v, k});
	}
	vector <vector<int>> dp(S+1, vector <int> (S+1));
	int _it = 0;
    for ( auto &p: g ){
        if ( !_it ){
            _it = 1; continue;
        }
        sort(all(p), greater <pair<int,int>> ());
        for ( int i = 1; i <= S; i++ ){
            dp[_it][i] = dp[_it-1][i];
            int it = 0, cnt = 0, value = 0, num = 0;
            while ( (cnt+1)*_it <= i and it < (int)p.size() ){
                value += p[it].first;
                cnt++;
                dp[_it][i] = max(dp[_it][i], dp[_it-1][i-cnt*_it]+value);
                if ( ++num == p[it].second ){
                    num = 0; it++;
                }
            }
        } _it++;
    }
    cout << *max_element(all(dp.back()));

	cout << '\n';
}

Compilation message (stderr)

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