From 08cb4ebf61c571f5009329dea43ced8afaef9ad9 Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Fri, 29 Aug 2025 14:26:47 +0900 Subject: [PATCH] =?UTF-8?q?[20250829]=20BOJ=20/=20G1=20/=20=EA=B3=B5?= =?UTF-8?q?=ED=8F=89=ED=95=98=EA=B2=8C=20=ED=8C=80=20=EB=82=98=EB=88=84?= =?UTF-8?q?=EA=B8=B0=20/=20=ED=95=9C=EC=A2=85=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0 \353\202\230\353\210\204\352\270\260.md" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "Ukj0ng/202508/29 BOJ G1 \352\263\265\355\217\211\355\225\230\352\262\214 \355\214\200 \353\202\230\353\210\204\352\270\260.md" diff --git "a/Ukj0ng/202508/29 BOJ G1 \352\263\265\355\217\211\355\225\230\352\262\214 \355\214\200 \353\202\230\353\210\204\352\270\260.md" "b/Ukj0ng/202508/29 BOJ G1 \352\263\265\355\217\211\355\225\230\352\262\214 \355\214\200 \353\202\230\353\210\204\352\270\260.md" new file mode 100644 index 00000000..f4088fed --- /dev/null +++ "b/Ukj0ng/202508/29 BOJ G1 \352\263\265\355\217\211\355\225\230\352\262\214 \355\214\200 \353\202\230\353\210\204\352\270\260.md" @@ -0,0 +1,73 @@ +``` +import java.io.*; +import java.util.HashSet; +import java.util.Set; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static Set[] dp; + private static int[] weight; + private static int N, sum, max; + + public static void main(String[] args) throws IOException { + init(); + DP(); + + int answer = 0; + + for (int val : dp[dp.length-1]) { + answer = Math.max(val, answer); + } + + if (N%2 == 1) { + for (int val : dp[dp.length-2]) { + answer = Math.max(val, answer); + } + } + + bw.write(answer + " " + (sum - answer) + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + sum = 0; + max = 0; + + int length = N/2; + if (N%2 == 1) length++; + weight = new int[N]; + dp = new Set[length + 1]; + for (int i = 0; i < dp.length; i++) { + dp[i] = new HashSet<>(); + } + + dp[0].add(0); + + for (int i = 0; i < N; i++) { + weight[i] = Integer.parseInt(br.readLine()); + sum += weight[i]; + } + + max = sum / 2; + } + + private static void DP() { + for (int i = 0; i < N; i++) { + for (int j = dp.length-1; j > 0; j--) { + if (!dp[j-1].isEmpty()) { + for (int val : dp[j-1]) { + int result = val + weight[i]; + if (result <= max) { + dp[j].add(result); + } + } + } + } + } + } +} +```