# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
330027 | oak | Knapsack (NOI18_knapsack) | C++14 | 2 ms | 364 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 wei[n],num[n];
long long val[n];
for(i=0;i<n;i++){
scanf("%lld %d %d",&val[i],&wei[i],&num[i]);
}
/*for(i=1;i<=s;i++){
printf("%d\t",i);
}printf("\n");*/
for(i=0;i<n;i++){
for(j=1;j<=s;j++){
dp[i%2][j] = make_pair(dp[(i+1)%2][j].first*1ll,num[i]);
if(j>=wei[i] && dp[(i+1)%2][j-wei[i]].first*1ll + val[i]*1ll > dp[i%2][j].first*1ll){
dp[i%2][j] = make_pair(dp[(i+1)%2][j-wei[i]].first*1ll + val[i]*1ll,num[i]-1);
}
if(j>=wei[i] && dp[i%2][j-wei[i]].second && dp[i%2][j-wei[i]].first*1ll + val[i]*1ll > dp[i%2][j].first*1ll){
dp[i%2][j] = make_pair(dp[i%2][j-wei[i]].first*1ll + val[i]*1ll,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*1ll > dp[i%2][j].first*1ll){
dp[i%2][j] = dp[i%2][j-wei[i]];
}
if(dp[i%2][j-1] > dp[i%2][j]){
dp[i%2][j] = dp[i%2][j-1];
}
//printf("%d,%d\t",dp[i%2][j].first,dp[i%2][j].second);
}//printf("\n");
}
printf("%lld",dp[(i-1)%2][s].first);
}
/*
15 4
5 3 1
6 4 2
3 2 3
6 1 4
*/
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... |