From d84000a15f5452114727d7d63c80540aad9b7c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=8B=A0=EC=A7=80?= <101992179+ksinji@users.noreply.github.com> Date: Tue, 27 Jan 2026 23:40:38 +0900 Subject: [PATCH] =?UTF-8?q?[20260127]=20BOJ=20/=20G4=20/=20RGB=20=EA=B1=B0?= =?UTF-8?q?=EB=A6=AC=202=20/=20=EA=B0=95=EC=8B=A0=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../27 BOJ RGB\352\261\260\353\246\254 2.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "ksinji/202601/27 BOJ RGB\352\261\260\353\246\254 2.md" diff --git "a/ksinji/202601/27 BOJ RGB\352\261\260\353\246\254 2.md" "b/ksinji/202601/27 BOJ RGB\352\261\260\353\246\254 2.md" new file mode 100644 index 00000000..0dc9f744 --- /dev/null +++ "b/ksinji/202601/27 BOJ RGB\352\261\260\353\246\254 2.md" @@ -0,0 +1,55 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int n; + static int[][] cost; + static int[][] dp; + + static final int INF = 1_000_000_000; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + n = Integer.parseInt(br.readLine()); + cost = new int[n][3]; + + for (int i = 0; i < n; i++) { + st = new StringTokenizer(br.readLine()); + cost[i][0] = Integer.parseInt(st.nextToken()); + cost[i][1] = Integer.parseInt(st.nextToken()); + cost[i][2] = Integer.parseInt(st.nextToken()); + } + + int ans = INF; + + for (int firstColor = 0; firstColor < 3; firstColor++) { + dp = new int[n][3]; + + for (int c = 0; c < 3; c++) { + if (c == firstColor) { + dp[0][c] = cost[0][c]; + } else { + dp[0][c] = INF; + } + } + + for (int i = 1; i < n; i++) { + dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + cost[i][0]; + dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + cost[i][1]; + dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + cost[i][2]; + } + + for (int lastColor = 0; lastColor < 3; lastColor++) { + if (lastColor != firstColor) { + ans = Math.min(ans, dp[n - 1][lastColor]); + } + } + } + + System.out.println(ans); +``` + } +}