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>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int s, n;
cin >> s >> n;
int w[n+1], v[n+1], k[n+1];
for(int i=1; i<=n; i++){
cin >> v[i] >> w[i] >> k[i];
}
pair<int, int> ans[n+1][s+1];
for(int i=0; i<=s; i++){
ans[0][i] = {0, 0};
}
for(int i=1; i<=n; i++){
for(int j=0; j<=s; j++){
if(j>=w[i]){
if(ans[i][j-w[i]].second < k[i]){
if(ans[i][j-w[i]].first+v[i] > ans[i-1][j].first) {
ans[i][j] = {ans[i][j-w[i]].first + v[i], ans[i][j-w[i]].second+1};
}
else{
ans[i][j] = {ans[i-1][j].first, 0};
}
}
else{
for(int kk=0; kk*w[i]<=j && kk<=k[i]; kk++){
if(ans[i-1][j-w[i]*kk].first+v[i]*kk > ans[i][j].first){
ans[i][j] = {ans[i-1][j-w[i]*kk].first+v[i]*kk, kk};
}
}
}
}
else{
ans[i][j] = ans[i-1][j];
}
}
}
// for(int i=-1; i<=s; i++){
// printf("%5d", i);
// }
// printf("\n");
// for(int i=0; i<=n; i++){
// printf("%3d: ", i);
// for(int j=0; j<=s; j++){
// printf("%5d", ans[i][j].first);
// }
// printf("\n");
// }
cout << ans[n][s].first << '\n';
}
# | 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... |