File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-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 int n;
7+ static int [][] cost;
8+ static int [][] dp;
9+
10+ static final int INF = 1_000_000_000 ;
11+
12+ public static void main (String [] args ) throws Exception {
13+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
14+ StringTokenizer st;
15+
16+ n = Integer . parseInt(br. readLine());
17+ cost = new int [n][3 ];
18+
19+ for (int i = 0 ; i < n; i++ ) {
20+ st = new StringTokenizer (br. readLine());
21+ cost[i][0 ] = Integer . parseInt(st. nextToken());
22+ cost[i][1 ] = Integer . parseInt(st. nextToken());
23+ cost[i][2 ] = Integer . parseInt(st. nextToken());
24+ }
25+
26+ int ans = INF ;
27+
28+ for (int firstColor = 0 ; firstColor < 3 ; firstColor++ ) {
29+ dp = new int [n][3 ];
30+
31+ for (int c = 0 ; c < 3 ; c++ ) {
32+ if (c == firstColor) {
33+ dp[0 ][c] = cost[0 ][c];
34+ } else {
35+ dp[0 ][c] = INF ;
36+ }
37+ }
38+
39+ for (int i = 1 ; i < n; i++ ) {
40+ dp[i][0 ] = Math . min(dp[i - 1 ][1 ], dp[i - 1 ][2 ]) + cost[i][0 ];
41+ dp[i][1 ] = Math . min(dp[i - 1 ][0 ], dp[i - 1 ][2 ]) + cost[i][1 ];
42+ dp[i][2 ] = Math . min(dp[i - 1 ][0 ], dp[i - 1 ][1 ]) + cost[i][2 ];
43+ }
44+
45+ for (int lastColor = 0 ; lastColor < 3 ; lastColor++ ) {
46+ if (lastColor != firstColor) {
47+ ans = Math . min(ans, dp[n - 1 ][lastColor]);
48+ }
49+ }
50+ }
51+
52+ System . out. println(ans);
53+ ```
54+ }
55+ }
You can’t perform that action at this time.
0 commit comments