Submission #839043

#TimeUsernameProblemLanguageResultExecution timeMemory
839043snasibov05Knapsack (NOI18_knapsack)C++17
100 / 100
69 ms37632 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define double long double
#define ull unsigned long long
#define pii pair<int,int>
#define tiii tuple<int,int,int>
#define pll pair<long long, long long>
#define pdd pair<double, double>
#define s second
#define f first
#define pb push_back
#define oo 1000000000000000000ll
#define all(a) (a).begin(), a.end()
#define rall(a) (a).rbegin(), a.rend()
#define mp make_pair

void solve() {

	int s, n; cin >> s >> n;
	vector<int> v(n), w(n), k(n);
	for (int i = 0; i < n; ++i) cin >> v[i] >> w[i] >> k[i];
	
	vector< vector< pair<int, int> > > by_weight(s+1);
	for (int i = 0; i < n; ++i){
		by_weight[w[i]].pb(mp(v[i], k[i]));
	}
	
	for (int i = 1; i <= s; ++i){
		sort(rall(by_weight[i]));
	}
	
	vector< vector<int> > dp(s+1, vector<int>(s+1, -oo)); dp[0][0] = 0;
	for (int i = 1; i <= s; ++i){
		
		for (int j = 0; j <= s; ++j){
			dp[i][j] = dp[i-1][j];
			
			int cur_idx = 0;
			int cur_cnt = 0;
			int smv = 0;
			int smw = 0;
			
			while (cur_idx < by_weight[i].size() && smw + i <= j){
				smw += i;
				smv += by_weight[i][cur_idx].f;
				dp[i][j] = max(dp[i][j], dp[i-1][j - smw] + smv);
				
				cur_cnt++;
				if (cur_cnt == by_weight[i][cur_idx].s){
					cur_cnt = 0;
					cur_idx++;
				}
			}
			
			//cerr << i << " " << j << " " << dp[i][j] << "\n";
		}
		//cerr << "\n";
	}
	
	int ans = 0;
	for (int i = 1; i <= s; ++i) ans = max(ans, dp[s][i]);
	
	cout << ans << "\n";

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int tst; tst = 1;
    //cin >> tst;
    while (tst--){
        solve();
    }

    return 0;

}

Compilation message (stderr)

knapsack.cpp: In function 'void solve()':
knapsack.cpp:46:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |    while (cur_idx < by_weight[i].size() && smw + i <= j){
      |           ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...