Submission #1265790

#TimeUsernameProblemLanguageResultExecution timeMemory
1265790minggaKnapsack (NOI18_knapsack)C++20
73 / 100
1093 ms8340 KiB
// Author: caption_mingle
#include "bits/stdc++.h"

using namespace std;

#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
int dp[2005];
int n, s;

signed main() {
	cin.tie(0) -> sync_with_stdio(0);
	#define task ""
	if(fopen(task ".INP", "r")) {
		freopen(task ".INP", "r", stdin);
		freopen(task ".OUT", "w", stdout);
	}
	vector<int> W, V;
	cin >> s >> n;
	for(int i = 1; i <= n; i++) {
		int v, w, k; cin >> v >> w >> k;
		k = min(k, s / w);
		int cnt = 1; 
		while(1) {
			if(k < cnt) break;
			W.pb(cnt * w);
			V.pb(v * cnt);
			k -= cnt;
			cnt *= 2;
		}
		if(k > 0) {
			W.pb(k * w);
			V.pb(k * v);
		}
	} 
	for(int i = 0; i < sz(W); i++) {
		for(int j = s; j >= W[i]; j--) {
			dp[j] = max(dp[j], dp[j - W[i]] + V[i]);
		}
	}
	cout << * max_element(dp, dp + s + 1) << ln;
    cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:22:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |                 freopen(task ".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:23:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |                 freopen(task ".OUT", "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...