Submission #1129222

#TimeUsernameProblemLanguageResultExecution timeMemory
1129222jackofall718Knapsack (NOI18_knapsack)C++20
73 / 100
479 ms327680 KiB
#include <bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define vn vector <ll>
using namespace std;
const int MAX_N = 1e9 + 7;
#define pii pair <ll,ll>
const ll INF = 0x3f3f3f3f3f3f3f3f;

#define pb push_back
#define srt(vp) sort(vp.begin(), vp.end())

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll s, n;
  cin >> s >> n;
  map<ll, vn> items;
  for (int i = 0; i < n; i++) {
    ll v, w, k;
    cin >> v >> w >> k;
    ll limit = min(k, s / w);
    for (int j = 0; j < limit; j++) {
      items[w].pb(v);
    }
  }

  vn weights, prices;
  for (auto& item : items) {
    ll weight = item.first;
    auto& vals = item.second;
    srt(vals);
    reverse(vals.begin(), vals.end());
    ll count = min((ll)vals.size(), s / weight);
    for (int i = 0; i < count; i++) {
      prices.pb(vals[i]);
      weights.pb(weight);
    }
  }

  vn dp(s + 1, 0);
  for (int i = 0; i < weights.size(); i++) {
    for (int j = s; j >= weights[i]; j--) {
      dp[j] = max(dp[j], dp[j - weights[i]] + prices[i]);
    }
  }

  cout << dp[s] << 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...