# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
486417 | TruaShamu | Knapsack (NOI18_knapsack) | C++17 | 20 ms | 44108 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.
#include <bits/stdc++.h> // see /general/running-code-locally
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define all(x) begin(x), end(x)
#define allr(x, r) begin(x), begin(x) + (r)
#define sz(x) (int) (x).size()
using pi = pair<int, int>;
#define f first
#define s second
#define mp make_pair
void setIO(string name = "") {
cin.tie(0)->sync_with_stdio(0); // see /general/fast-io
if (sz(name)) {
freopen((name + ".in").c_str(), "r", stdin); // see /general/input-output
freopen((name + ".out").c_str(), "w", stdout);
}
}
int main() {
int S, N;
cin >> S >> N;
vector<vector<pi>> items(S + 1, vector<pi>());
for (int i = 0; i < N; i++) {
int V, W, K;
cin >> V >> W >> K;
items[W].pb({ V, K });
}
for (int i = 0; i <= S; i++) {
sort(all(items[i]), greater<pi>());
}
for (int i = 0; i <= S; i++) {
for (pi j : items[i]) {
cout << j.first << " " << j.second << "\n";
}
cout << "-------------------------\n";
}
int maxItems = 0;
for (int i = 1; i <= S; i++) {
maxItems += S / i;
}
cout << "total: " << maxItems << "\n";
// DP[i][j] is the max profit for i items and j weight.
vector<vi> dp(maxItems + 1, vi(S + 1, 0));
int numItems = 1;
for (int w = 1; w <= S; w++) {
if (items[w].size() == 0) {
continue;
}
int w_i = 0;
int copies = items[w][0].second;
for (int amt = 0; amt < S / w; amt++) {
for (int cur = 1; cur <= S; cur++) {
dp[numItems][cur] = dp[numItems - 1][cur];
if (cur - w >= 0) {
dp[numItems][cur] = max(dp[numItems][cur], dp[numItems - 1][cur - w] + items[w][w_i].f);
}
}
numItems++;
copies--;
if (copies == 0) {
w_i++;
if (w_i >= items[w].size()) {
break;
}
copies = items[w][w_i].s;
}
}
}
cout << dp[numItems - 1][S] << "\n";
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... |