이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <string.h>
#define M 2000
int max(int a, int b) { return a > b ? a : b; }
int main() {
static int dp[M + 1], dq[M + 1], qu[M + 1];
int n, m, r, s, ans, head, cnt;
scanf("%d%d", &m, &n);
memset(dp, -1, (m + 1) * sizeof *dp), dp[0] = 0;
while (n--) {
int v, w, k;
scanf("%d%d%d", &v, &w, &k);
if (w > m)
continue;
for (r = 0; r < w; r++) {
head = cnt = 0;
for (s = r; s <= m; s += w) {
if (dp[s] != -1) {
dq[s] = dp[s] - s / w * v;
while (cnt && dq[qu[head + cnt - 1]] <= dq[s])
cnt--;
qu[head + cnt++] = s;
}
dp[s] = cnt == 0 ? -1 : dq[qu[head]] + s / w * v;
if (cnt && (s - qu[head]) / w == k)
head++, cnt--;
}
}
}
ans = 0;
for (s = 0; s <= m; s++)
ans = max(ans, dp[s]);
printf("%d\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.c: In function 'main':
knapsack.c:12:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%d%d", &m, &n);
| ^~~~~~~~~~~~~~~~~~~~~
knapsack.c:17:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%d%d%d", &v, &w, &k);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |