[SWEA] 3282 0/1 Knapsack

Time Lapse :3hour 10min 24sec

3282.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <memory.h>

int main(int argc, char** argv)
{
register int test_case, j;
int T, N, K, V, C;
int DP[1001];
setbuf(stdout, NULL);
scanf("%d",&T);
for (test_case = 1; test_case <= T; ++test_case){
scanf("%d %d",&N,&K);
memset(DP,0,sizeof(DP));
while(N--){
scanf("%d %d",&V,&C);
for(j = K; j>=V; j--)
DP[j] = DP[j-V]+C > DP[j] ? DP[j-V] + C : DP[j];
}
printf("#%d %d\n",test_case,DP[K]);
}
return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/3282/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.