# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
820176 | uped | Knapsack (NOI18_knapsack) | C++14 | 1079 ms | 2768 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);
}
//ll MOD = 1e9 + 7;
//ll TWO_MOD_INV = 500000004;
struct Item {
int value;
int weight;
int amount;
};
int main() {
fastio();
vector<Item> items;
ll s, n;
cin >> s >> n;
items.resize(n);
for (ll i = 0; i < n; ++i) {
int v, w, k;
cin >> v >> w >> k;
items[i] = {v, w, k};
}
// vector<vector<ll>> dp(s + 1, vector<ll>(n + 1));
//
// for (ll i = n - 1; i >= 0; --i) {
// for (ll j = 0; j <= s; ++j) {
// for (ll k = 0; k <= items[i].amount; ++k) {
// if (j - items[i].weight * k >= 0) {
// dp[j][i] = max(dp[j][i], dp[j - items[i].weight * k][i + 1] + items[i].value * k);
// } else {
// break;
// }
// }
// }
// }
vector<ll> dp2(s + 1);
for (ll i = n - 1; i >= 0; --i) {
for (ll j = s; j >= 0; --j) {
for (ll k = 0; k <= items[i].amount; ++k) {
if (j - (items[i].weight * k) >= 0) {
dp2[j] = max(dp2[j], dp2[j - items[i].weight * k] + items[i].value * k);
} else {
break;
}
// cout << "item: " << i << " capacity: " << j << " amount: " << k << '\n';
// for (int l = 0; l < s + 1; ++l) {
// cout << dp2[l] << ' ';
// }
// 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... |