# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
674566 | omikron123 | Knapsack (NOI18_knapsack) | C++14 | 1063 ms | 1832 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.
// https://oj.uz/problem/view/NOI18_knapsack
#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
// 0-k背包问题
// n <= 1e5, s <= 2000
int n, s;
int v[100005], w[100005], K[100005]; // v[] <= 1e6
ll dp[100005][2005]; // dp[i][j], max value achieved with items 1..i and weight j
int main() {
scanf("%d %d", &s, &n);
for (int i = 1; i <= n; i++)
scanf("%d %d %d", &v[i], &w[i], &K[i]);
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= s; j++)
for (int k = 0; k <= K[i]; k++)
if (j-w[i]*k >= 0)
dp[i][j] = max(dp[i][j], dp[i-1][j-w[i]*k] + v[i]*k);
ll ans = 0;
for (int i = 0; i <= s; i++)
ans = max(ans, dp[n][i]);
printf("%lld", ans);
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... |