# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
911652 | anastasiskolio | Knapsack (NOI18_knapsack) | C++14 | 1027 ms | 19544 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;
int main()
{
int S, N;
scanf("%d%d", &S, &N);
int V[N + 1];
int W[N + 1];
int K[N + 1];
int M = 0;
for (int i = 1; i <= N; i++)
{
scanf("%d%d%d", &V[i], &W[i], &K[i]);
M += K[i];
}
int Weight[M + 1];
int Value[M + 1];
int m = 0;
for (int i = 1; i <= N; i++)
{
int j = K[i];
while (j > 0)
{
m++;
Weight[m] = W[i];
Value[m] = V[i];
j--;
}
}
/*int dp[2][S + 1];
dp[1][0] = 0;
for (int s = 0; s <= S; s++)
dp[0][s] = 0;
for (int i = 1; i <= M; i++)
{
for (int s = 1; s <= S; s++)
{
if (s - Weight[i] < 0)
dp[i%2][s] = dp[(i - 1)%2][s];
else
dp[i%2][s] = max(dp[(i - 1)%2][s], dp[(i - 1)%2][s - Weight[i]] + Value[i]);
}
}
printf("%d\n", dp[M%2][S]);*/
int dp[2][S + 1];
dp[1][0] = 0;
for (int s = 0; s <= S; s++)
dp[0][s] = 0;
for (int i = 1; i <= M; i++)
{
for (int s = 1; s <= S; s++)
{
dp[i%2][s] = dp[(i - 1)%2][s];
if (s - Weight[i] >= 0)
dp[i%2][s] = max(dp[i%2][s], dp[(i - 1)%2][s - Weight[i]] + Value[i]);
}
}
printf("%d\n", dp[M%2][S]);
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... |