Submission #330027

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

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:10:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   10 |   scanf("%lld %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...