From c5ad16536000f0d5595ace3647269df0d5dc98f1 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Tue, 11 Feb 2025 15:51:04 +0900 Subject: [PATCH] =?UTF-8?q?[20250211]=20BOJ=20/=20=EA=B3=A8=EB=93=9C5=20/?= =?UTF-8?q?=20=EC=B0=BD=EC=98=81=EC=9D=B4=EC=99=80=20=EC=BB=A4=ED=94=BC=20?= =?UTF-8?q?/=20=EA=B9=80=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\231\200 \354\273\244\355\224\274.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "suyeun84/202502/11 BOJ G5 \354\260\275\354\230\201\354\235\264\354\231\200 \354\273\244\355\224\274.md" diff --git "a/suyeun84/202502/11 BOJ G5 \354\260\275\354\230\201\354\235\264\354\231\200 \354\273\244\355\224\274.md" "b/suyeun84/202502/11 BOJ G5 \354\260\275\354\230\201\354\235\264\354\231\200 \354\273\244\355\224\274.md" new file mode 100644 index 00000000..d1a1a262 --- /dev/null +++ "b/suyeun84/202502/11 BOJ G5 \354\260\275\354\230\201\354\235\264\354\231\200 \354\273\244\355\224\274.md" @@ -0,0 +1,41 @@ +```java +import java.util.*; +import java.io.*; + +class Solution +{ + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + Integer[] coffee = new Integer[N]; + int[] dp = new int[K+1]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + coffee[i] = Integer.parseInt(st.nextToken()); + } + + Arrays.sort(coffee, Collections.reverseOrder()); + + for (int i = 0; i < N; i++) { + if (coffee[i] > K) continue; + dp[coffee[i]] = 1; + for (int j = K; j > coffee[i]; j--) { + if (dp[j - coffee[i]] != Integer.MAX_VALUE) { + dp[j] = Math.min(dp[j], dp[j - coffee[i]] + 1); + } + } + } + + if (dp[K] != Integer.MAX_VALUE) { + System.out.println(dp[K]); + } else { + System.out.println(-1); + } + } +} + +```