# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
984310 | vjudge1 | Knapsack (NOI18_knapsack) | C++14 | 103 ms | 28108 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>
using namespace std;
using ll = long long;
const int mxn = 9 + 1e6;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
vector<int> obj[mxn];
void SaKaTa(void) {
int s, n;
cin >> s >> n;
for (int i = 1; i <= n; ++i) {
int v, w, k;
cin >> v >> w >> k;
k = min(k, s / w);
int tmp = 1;
while (k) {
int V = v * tmp, W = w * tmp;
obj[W].push_back(V);
k -= tmp;
tmp = min(tmp * 2, k);
}
}
vector<int> dp(s + 1);
for (int i = 1; i <= s; ++i) {
sort(begin(obj[i]), end(obj[i]), greater<int>());
while ((int)obj[i].size() > (int) (s / i)) {
obj[i].pop_back();
}
for (int v : obj[i]) {
for (int w = s; w >= i; --w) {
dp[w] = max(dp[w], dp[w - i] + v);
}
}
}
cout << *max_element(begin(dp), end(dp)) << "\n";
}
int32_t main(void) {
#define task "SaKaTa"
cin.tie(0)->sync_with_stdio(0);
for (string iext : {"in", "inp"}) {
if (fopen((task "." + iext).c_str(), "r")) {
freopen((task "." + iext).c_str(), "r", stdin);
freopen(task ".out", "w", stdout);
}
}
int testcase = 1;
// cin >> testcase;
while (testcase--) SaKaTa();
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... |