-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGB.java
30 lines (24 loc) · 858 Bytes
/
RGB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package dynamicProgramming;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class RGB {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int d[][] = new int[N + 1][3];
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
int rgb[] = {Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken())};
for (int j = 0; j < 3; j++)
d[i][j] = Math.min(d[i - 1][(j + 1) % 3], d[i - 1][(j + 2) % 3]) + rgb[j];
}
int min = d[N][0];
for (int i = 1; i < 3; i++) {
if(d[N][i] < min)
min = d[N][i];
}
System.out.println(min);
}
}