# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
330021 | oak | Knapsack (NOI18_knapsack) | C++14 | 2 ms | 492 KiB |
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;
pair<long long,int> dp[2][2020];
int main(){
int n,k,s,i,j;
scanf("%d %d",&s,&n);
int val[n],wei[n],num[n];
for(i=0;i<n;i++){
scanf("%d %d %d",&val[i],&wei[i],&num[i]);
}
for(i=0;i<n;i++){
for(j=1;j<=s;j++){
dp[i%2][j] = make_pair(dp[(i+1)%2][j].first,num[i]);
if(j>=wei[i] && dp[(i+1)%2][j-wei[i]].first + val[i] > dp[i%2][j].first){
dp[i%2][j] = make_pair(dp[(i+1)%2][j-wei[i]].first + val[i],num[i]-1);
}
if(j>=wei[i] && dp[i%2][j-wei[i]].second && dp[i%2][j-wei[i]].first + val[i] > dp[i%2][j].first){
dp[i%2][j] = make_pair(dp[i%2][j-wei[i]].first + val[i],dp[i%2][j-wei[i]].second - 1);
}
if(j>=wei[i] && !dp[i%2][j-wei[i]].second && dp[i%2][j-wei[i]].first > dp[i%2][j].first){
dp[i%2][j] = dp[i%2][j-wei[i]];
}
}
}
printf("%lld",dp[(i-1)%2][s].first);
}
Compilation message (stderr)
# | 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... |