Submission #1308267

#TimeUsernameProblemLanguageResultExecution timeMemory
1308267tolgaKnapsack (NOI18_knapsack)C++20
37 / 100
2 ms660 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int s, n;
  cin >> s >> n;
  vector<int> p(n), w(n), cnt(n);
  for (ll i = 0; i < n; i++)
    cin >> p[i] >> w[i] >> cnt[i];

  vector<ll> dp(s + 1);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int K = cnt[i], get = 1;
    while (K) {
      int br = min(get, K);
      K -= br;
      get <<= 1;

      ll weight = br * w[i], price = br * p[i];
      for (int j = s; j >= weight; j--)
        if (dp[j - weight] + price > dp[j]) {
          dp[j] = dp[j - weight] + price;
          ans = max(ans, dp[j]);
        }
    }
  }

  cout << *max_element(dp.begin(), dp.end()) << endl;
}
#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...