Submission #704285

#TimeUsernameProblemLanguageResultExecution timeMemory
704285StormersyleKnapsack (NOI18_knapsack)C++14
100 / 100
294 ms247240 KiB
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>  
#include <math.h>
#include <numeric>
using namespace std;
using ll=long long;

int S, N, V[100001], W[100001], K[10001];
vector<pair<int, int>> weightClass[2001]; //(a, b)=value a, quantity b

int main() {
  // ifstream cin("file.in");
  cin>>S>>N;
  for (int i=0; i<N; i++){
    cin>>V[i]>>W[i]>>K[i];
    weightClass[W[i]].push_back({V[i], K[i]});
  }
  for (int w=1; w<=S; w++){
    if (weightClass[w].size()>0) sort(weightClass[w].begin(), weightClass[w].end());
  }
  vector<int> value, weight; //list of items we consider (up to top S/w from each weight w); pair=(value, weight)
  for (int w=1; w<=S; w++){
    vector<pair<int, int>> wc=weightClass[w];
    for (int i=0; i<S/w; i++){
      if (wc.size()==0) break;
      value.push_back({wc.back().first}), weight.push_back(w);
      wc.back().second--;
      if (wc.back().second==0) wc.pop_back();
    }
  }
  //Now: 0-1 DP on rest of objects
  int T=value.size();
  // cout<<T<<"\n";
  // for (int i=0; i<T; i++) cout<<value[i]<<" "<<weight[i]<<"\n";
  ll f[S+1][T];
  for (int i=0; i<T; i++) f[0][i]=0;
  for (int x=1; x<=S; x++){
    if (x>=weight[0]) f[x][0]=(ll)(value[0]);
    else f[x][0]=0;
    for (int i=1; i<T; i++){
      if (x>=weight[i]) f[x][i]=max(f[x][i-1], f[x-weight[i]][i-1]+(ll)(value[i]));
      else f[x][i]=f[x][i-1];
    }
  }
  cout<<f[S][T-1];
}
#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...