Submission #330021

#TimeUsernameProblemLanguageResultExecution timeMemory
330021oakKnapsack (NOI18_knapsack)C++14
29 / 100
2 ms492 KiB
#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)

knapsack.cpp: In function 'int main()':
knapsack.cpp:5:8: warning: unused variable 'k' [-Wunused-variable]
    5 |  int n,k,s,i,j;
      |        ^
knapsack.cpp:6:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    6 |  scanf("%d %d",&s,&n);
      |  ~~~~~^~~~~~~~~~~~~~~
knapsack.cpp:9:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    9 |   scanf("%d %d %d",&val[i],&wei[i],&num[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...