# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1027156 | lmaobruh | Knapsack (NOI18_knapsack) | C++14 | 5 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 = 1e18+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) {
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]);
}
swap(dp1, dp2);
}
ll ans = 0;
for (int j = 0; j <= s; ++j) if (dp1[j] < inf)
chmax(ans, dp1[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... |