-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-operations.java
56 lines (51 loc) · 1.6 KB
/
minimum-operations.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
class MinimumOperations {
private static final Scanner scan = new Scanner(System.in);
int n, r ,g, b;
int[][] dp = new int[110][1<<3];
Vector<Integer> red = new Vector();
Vector<Integer> green = new Vector();
Vector<Integer> blue = new Vector();
public void get() {
n = scan.nextInt();
for (int i = 0; i < n; i++) {
r = scan.nextInt();
g = scan.nextInt();
b = scan.nextInt();
red.add(r);
green.add(g);
blue.add(b);
}
}
public void minOperations() {
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j < 8; j++) {
dp[i][j] = (1<<30);
}
}
dp[0][0] = 0;
for (i = 0; i < n; i++){
for (j = 0; j < 8; j++){
dp[i + 1][j | 1] = Math.min(dp[i + 1][j | 1], dp[i][j] + green.get(i) + blue.get(i));
dp[i + 1][j | 2] = Math.min(dp[i + 1][j | 2], dp[i][j] + red.get(i) + blue.get(i));
dp[i + 1][j | 4] = Math.min(dp[i + 1][j | 4], dp[i][j] + red.get(i) + green.get(i));
}
}
j = 0;
for (i = 0; i < n; i++){
if (green.get(i) != 0) j |= 2;
if (red.get(i) != 0) j |= 1;
if (blue.get(i) != 0) j |= 4;
}
if (dp[n][j] >= (1<<30)) dp[n][j] = -1;
System.out.println(dp[n][j]);
}
}
class Solution {
public static void main(String[] args) {
MinimumOperations obj = new MinimumOperations();
obj.get();
obj.minOperations();
}
}