Submission #950428

#TimeUsernameProblemLanguageResultExecution timeMemory
950428KakarotKnapsack (NOI18_knapsack)C++98
73 / 100
1035 ms4048 KiB
#include <bits/stdc++.h>
#define int int64_t
using namespace std;
 
void setIO() {
	cin.tie(0)->sync_with_stdio(0);
}
 
void setIJ() {
	#ifdef ONLINEJUDGE
    	clock_t tStart = clock();
    	freopen("input.txt","r",stdin); //can need to change file . this one for taking input
    	freopen("output.txt","w",stdout); // this one for output
    #endif
}
 
void setOJ() {
    #ifdef ONLINEJUDGE
        fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); // this line gives your code runtime
    #endif
}
 
struct item { int value; int weight; int copies; };
 
void solve() {
	//cout << "zco";
	int s, n;
	cin >> s >> n;
	vector<item> items(n);
	for(auto &x : items) cin >> x.value >> x.weight >> x.copies;
	vector<vector<int>> dp(2, vector<int>(s+1));
	//dp[i][j] = max value that can be obtained from first i items upto weight j
	//dp[i][j] = dp[i-1][j-0*items[i].weight] + dp[i-1][j-1*items[i].weight] + dp[i-1][j-2*items[i].weight] + dp[i-1][j-items[i].copies*items[i].weight]
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= s; j++) {
			for(int k = 0; k <= items[i-1].copies && k*items[i-1].weight <= j; k++) {
				dp[i%2][j] = max(dp[i%2][j], k*items[i-1].value + dp[1-i%2][j-k*items[i-1].weight]);
			}
		}
	}
	cout << dp[n%2][s] << '\n';
}
 
int32_t main() {
	setIO();
	setIJ();
	solve();
	setOJ();
	return 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...