Submission #772278

#TimeUsernameProblemLanguageResultExecution timeMemory
772278RandomChickenKnapsack (NOI18_knapsack)C++11
29 / 100
1 ms340 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define f0r(i,n) for(ll i = 0; i<(ll)(n); i++)

int main(){
  ll capacity,n;
  cin>>capacity>>n;
  ll value[n], weight[n], quantity[n];
  f0r(i,n) cin>>value[i]>>weight[i]>>quantity[i];

  ll dp[capacity+1]; // in theory: dp[n+1][capacity+1]; but made 1D for memory saving
  ll nextDp[capacity+1]; // AUXILIARY FOR TEMPORARILY HOLDING EDITS TO DP
  f0r(j,capacity+1) dp[j] = 0; // initial row with nothing considered
  f0r(i,n){
    // considering item i
    ll boughtCount[capacity+2];
    boughtCount[capacity+1] = 0;
    for(ll j=capacity; j>=0; j--){
      nextDp[j] = dp[j];
      boughtCount[j] = 0;
      if(j<capacity){
        if(nextDp[j]<nextDp[j+1]){
          nextDp[j] = nextDp[j+1];
          boughtCount[j] = boughtCount[j+1];
        }
      }
      // ll buyCount = min(1LL,(capacity-j)/weight[i]);//min(quantity[i],(capacity-j)/weight[i]);
      // while(j+buyCount*weight[i]<=capacity&&buyCount<=quantity[i]){

      if(j+weight[i]<=capacity){
        ll candidate1 = dp[j+weight[i]]+value[i];
        if(candidate1>nextDp[j]){
          nextDp[j] = candidate1;
          boughtCount[j] = 1;
        }
        if(boughtCount[j+weight[i]]<quantity[i]){
          ll candidate2 = nextDp[j+weight[i]]+value[i];
          if(candidate2>nextDp[j]){
            nextDp[j] = candidate2;
            boughtCount[j] = boughtCount[j+weight[i]]+1;
          }
        }
      }
      // }
    }
    f0r(j,capacity+1){
      dp[j] = nextDp[j];
      // cout<<setw(5)<<dp[j];
    }
    // cout<<"\n";
  }
  cout<<dp[0];
}
#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...