This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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(n+1, 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][j] = max(dp[i][j], k*items[i-1].value + dp[i-1][j-k*items[i-1].weight]);
}
}
}
cout << dp[n][s] << '\n';
}
int32_t main() {
setIO();
setIJ();
solve();
setOJ();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |