From 7b38665087efd94ed9a3637ff3068ca04f6edaec Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Tue, 9 Sep 2025 21:27:46 +0900 Subject: [PATCH] 20250909 baekjoon --- code_study/Baekjoon/c/12865.c | 44 +++++++++++++++++++++++++++ code_study/Baekjoon/swift/12865.swift | 22 ++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 code_study/Baekjoon/c/12865.c create mode 100644 code_study/Baekjoon/swift/12865.swift diff --git a/code_study/Baekjoon/c/12865.c b/code_study/Baekjoon/c/12865.c new file mode 100644 index 0000000..7191d39 --- /dev/null +++ b/code_study/Baekjoon/c/12865.c @@ -0,0 +1,44 @@ +#include +#include + +int max(int a, int b) { + return a>b ? a : b; +} + +int main() { + int N, K; + scanf("%d %d",&N, &K); + int** items = (int**)malloc(sizeof(int*)*(N+1)); + int** dp = (int**)malloc(sizeof(int*)*(N+1)); + for(int i=0; i<=N; i++) { + items[i] = (int*)malloc(sizeof(int)*2); + if(i==0) { + items[i][0] = 0; + items[i][1] = 0; + } + else { + scanf("%d %d",&items[i][0], &items[i][1]); + } + + dp[i] = (int*)malloc(sizeof(int)*(K+1)); + for(int j=0; j<=K; j++) dp[i][j] = 0; + } + + for(int n=1; n<=N; n++) { + for(int k=1; k<=K; k++) { + if (items[n][0] <= k) dp[n][k] = max(dp[n-1][k], dp[n-1][k-items[n][0]] + items[n][1]); + else dp[n][k] = dp[n-1][k]; + } + } + + printf("%d\n",dp[N][K]); + + for(int i=0; i<=N; i++) { + free(items[i]); + free(dp[i]); + } + free(items); + free(dp); + + return 0; +} \ No newline at end of file diff --git a/code_study/Baekjoon/swift/12865.swift b/code_study/Baekjoon/swift/12865.swift new file mode 100644 index 0000000..6f1276c --- /dev/null +++ b/code_study/Baekjoon/swift/12865.swift @@ -0,0 +1,22 @@ +if let input1 = readLine(), let NK = input1.split(separator: " ").compactMap({Int($0)}) as? [Int], + let N = NK.first, let K = NK.last +{ + var items: [[Int]] = [] + for _ in 0..