File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static final int RED = 0 ;
7+ static final int GREEN = 1 ;
8+ static final int BLUE = 2 ;
9+
10+ public static void main (String [] args ) throws IOException {
11+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
12+ int n = Integer . parseInt(br. readLine());
13+
14+ int [][] cost = new int [n][3 ];
15+ for (int i = 0 ; i < n; i++ ) {
16+ StringTokenizer st = new StringTokenizer (br. readLine());
17+ cost[i][RED ] = Integer . parseInt(st. nextToken());
18+ cost[i][GREEN ] = Integer . parseInt(st. nextToken());
19+ cost[i][BLUE ] = Integer . parseInt(st. nextToken());
20+ }
21+
22+ int answer = 987654321 ;
23+
24+ for (int firstColor = 0 ; firstColor < 3 ; firstColor++ ) {
25+ int [][] dp = new int [n][3 ];
26+
27+ for (int j = 0 ; j < 3 ; j++ ) {
28+ if (j == firstColor) {
29+ dp[0 ][j] = cost[0 ][j];
30+ } else {
31+ dp[0 ][j] = 987654321 ;
32+ }
33+ }
34+
35+ for (int i = 1 ; i < n; i++ ) {
36+ dp[i][RED ] = Math . min(dp[i- 1 ][GREEN ], dp[i- 1 ][BLUE ]) + cost[i][RED ];
37+ dp[i][GREEN ] = Math . min(dp[i- 1 ][RED ], dp[i- 1 ][BLUE ]) + cost[i][GREEN ];
38+ dp[i][BLUE ] = Math . min(dp[i- 1 ][RED ], dp[i- 1 ][GREEN ]) + cost[i][BLUE ];
39+ }
40+
41+ for (int j = 0 ; j < 3 ; j++ ) {
42+ if (j != firstColor) {
43+ answer = Math . min(answer, dp[n- 1 ][j]);
44+ }
45+ }
46+ }
47+
48+ System . out. println(answer);
49+ }
50+ }
51+ ```
You can’t perform that action at this time.
0 commit comments