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>
#include <string.h>
#define N 100000
#define M 2000
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int vv[N], ww[N], kk[N];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = ww[ii[j]] != ww[i_] ? ww[ii[j]] - ww[i_] : vv[i_] - vv[ii[j]];
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
int main() {
static int ii[N], dp[M + 1];
int n, m, h, i, s, k, ans;
scanf("%d%d", &m, &n);
for (i = 0; i < n; i++) {
scanf("%d%d%d", &vv[i], &ww[i], &kk[i]);
ii[i] = i;
}
sort(ii, 0, n);
memset(dp, -1, (m + 1) * sizeof *dp), dp[0] = 0;
for (h = 0, k = 0; h < n; h++) {
i = ii[h];
if (h == 0 || ww[i] != ww[ii[h - 1]])
k = m / ww[i];
while (kk[i] > 0 && k > 0) {
for (s = m; s >= ww[i]; s--)
if (dp[s - ww[i]] != -1)
dp[s] = max(dp[s], dp[s - ww[i]] + vv[i]);
kk[i]--, k--;
}
}
ans = 0;
for (s = 0; s <= m; s++)
ans = max(ans, dp[s]);
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
knapsack.c: In function 'main':
knapsack.c:43:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d%d", &m, &n);
| ^~~~~~~~~~~~~~~~~~~~~
knapsack.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d%d%d", &vv[i], &ww[i], &kk[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |