# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
585797 | imtiyazrasool92 | Knapsack (NOI18_knapsack) | C++17 | 1089 ms | 10572 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<stdio.h>
int min(int A, int B) {
return A > B ? B : A;
}
int max(int A, int B) {
return A < B ? B : A;
}
int S, N;
int dp[2001][100000];
int A[100000][3];
int dfs(int index, int weight) {
if (weight == 0)
return 0;
if (index >= N)
return 0;
if (dp[weight][index] == -1) {
dp[weight][index] = 0;
for (int i = 0; i <= min(weight / A[index][1], A[index][2]); i++) {
if (weight - (i * A[index][1]) >= 0)
dp[weight][index] = max(dp[weight][index], dfs(index + 1, weight - (i * A[index][1])) + A[index][0] * i);
else
break;
}
}
return dp[weight][index];
}
int main() {
scanf("%d%d", &S, &N);
for (int i = 0; i < N; i++) {
scanf("%d%d%d", &A[i][0], &A[i][1], &A[i][2]);
if (A[i][1] > S) {
N--;
i--;
} else if (A[i][1] == 0) {
N--;
i--;
}
}
for (int i = 0; i < N; i++)
for (int s = 0; s <= S; s++)
dp[s][i] = -1;
printf("%d", dfs(0, S));
}
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... |