Submission #653875

#TimeUsernameProblemLanguageResultExecution timeMemory
653875fahimcp495Knapsack (NOI18_knapsack)C++17
37 / 100
2 ms440 KiB
#include<bits/stdc++.h>
using namespace std;

#define dbg(a)  cerr << #a << ": " << a << "\n"

using ll = long long;

void solve() {
  ll s, n;  cin >> s >> n;
  vector<array<ll, 2>> a;
  for (int i = 0; i < n; ++i) {
    ll v, w, k;  cin >> v >> w >> k;
    ll kk = 1;
    while (k > 0) {
      kk = min(kk, k);
      // if (kk * w > s)  break;
      a.push_back({kk * v, kk * w});
      k -= kk;
      kk *= 2;
    }
  }

  vector<ll> dp(s + 1);
  for (auto [v, w]: a) {
    for (int i = s - w; i >= 0; --i) {
      dp[i + w] = max(dp[i + w], dp[i] + v);
    }
  }

  cout << *max_element(dp.begin(), dp.end()) << "\n";
}

int main(){
  ios::sync_with_stdio(0), cin.tie(0);

  int tc = 1;
  for (int t = 1; t <= tc; ++t) {
    solve();
  }
}
#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...