# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1027130 | lmaobruh | Knapsack (NOI18_knapsack) | C++14 | 2 ms | 4696 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;
#define ll long long
#define pb push_back
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
const int N = 1e6+5, M = 2005;
const ll inf = 2e16+5;
ll s, n, v[N], w[N], k[N], dp1[M], dp2[M];
void sol() {
cin >> s >> n;
for (int i = 1; i <= n; ++i)
cin >> v[i] >> w[i] >> k[i];
for (int i = 1; i <= s; ++i) dp1[i] = -inf;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= s; ++j) dp2[j] = -inf;
for (int j = 0; j <= s; ++j) {
if (j + w[i+1] <= s) {
ll cnt = min(k[i+1], (s-j)/w[i+1]);
chmax(dp2[j+w[i+1]*cnt], dp1[j]+cnt*v[i+1]);
}
chmax(dp2[j], dp1[j]);
}
if (i + 1 < n) swap(dp1, dp2);
}
ll ans = 0;
for (int j = 0; j <= s; ++j) if (dp2[j] < inf)
chmax(ans, dp2[j]);
cout << ans;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
int tc = 1, test = 0; // cin >> tc;
while (++test <= tc) sol();
}
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... |