# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
898722 | Amaarsaa | Knapsack (NOI18_knapsack) | C++14 | 1 ms | 348 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 = int;
const int N = 1e5 + 2;
ll weight[N], CanAdd[N], price[N], Left[N];
ll dp[3][2005];
int main() {
// freopen("moocast.in", "r", stdin);
// freopen("moocast.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
ll i, ans, j, r, last, now, n, k;
cin >> k >> n;
for (i = 1; i <= n; i ++) {
cin >> price[i] >> weight[i] >> Left[i];
}
last = 1;
now = 0;
memset(dp, -0x3f, sizeof dp);
ll R =dp[0][0];
dp[0][0] = 0;
for (i = 1; i <= n; i ++) {
last ^= 1;
now ^= 1;
// cout << now << " " << last << endl;
for (j = 0; j <= k; j ++) {
if ( dp[last][j] >= 0) {
CanAdd[j] = max(CanAdd[j], Left[i]);
dp[now][j] = max(dp[last][j], dp[now][j]);
}
if ( CanAdd[j] != 0 && j + weight[i] <= k) {
// cout << i << "R" << j << endl;
CanAdd[j + weight[i]] = max(CanAdd[j] - 1, CanAdd[j + weight[i]]);
if(dp[now][j] >= 0) dp[now][j + weight[i]] = max(dp[now][j + weight[i]], dp[now][j] + price[i]);
}
CanAdd[j] = 0;
// cout << i << " " << j << " " << dp[now][j] << endl;
}
}
ans = 0;
for (i = 0; i <= k; i ++) {
ans = max(ans, dp[now][i]);
}
cout << ans << endl;
}
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... |