# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
823517 | uped | Knapsack (NOI18_knapsack) | C++14 | 58 ms | 3876 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// Created by MiłoszK on 11.08.2023.
//
// Two Sets II
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> sieve(ll n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (ll i = 2; i <= n; i++) {
if (is_prime[i] && (long long) i * i <= n) {
for (ll j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
vector<ll> primes;
for (ll i = 2; i <= n; ++i) {
if (is_prime[i]) {
primes.push_back(i);
}
}
return primes;
}
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
void fileio(const string &name) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
int main() {
fastio();
ll s, n;
cin >> s >> n;
map<int, vector<pair<int, int>>> items;
vector<pair<int, int>> vp;
for (ll i = 0; i < n; ++i) {
int v, w, k;
cin >> v >> w >> k;
items[w].emplace_back(v, k);
}
// for (int i = 0; i <= 2000; ++i) {
// sort(items[i].begin(), items[i].end(), greater<>());
// }
vector<int> dp2(s + 1);
for (auto& [w, v] : items) {
sort(v.begin(), v.end(), greater<>());
for (int j = s; j >= w; --j) {
int cnt_w = 0;
int cnt_v = 0;
int used = 0;
int k = 0;
while (k < v.size()) {
cnt_w += w;
cnt_v += v[k].first;
if (j - cnt_w < 0) break;
++used;
if (used == v[k].second) {
++k;
used = 0;
}
dp2[j] = max(dp2[j], dp2[j - cnt_w] + cnt_v);
}
}
// for (int i = 0; i <= s; ++i) {
// cout << dp2[i] << ' ';
// }
// cout << '\n';
}
cout << dp2[s];
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |