/
BAEKJOON 2961_SUBSET
50 lines (43 loc) · 1.07 KB
/
BAEKJOON 2961_SUBSET
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
package BJ;
import java.io.*;
import java.util.*;
public class BJ2961_2 {
static int[] sin, sem;
static int T, ans = Integer.MAX_VALUE, S = 1, R, count;
static boolean[] select;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
sin = new int[T];
sem = new int[T];
select = new boolean[T];
for (int t = 0; t < T; t++) {
StringTokenizer st = new StringTokenizer(br.readLine());
sin[t] = Integer.parseInt(st.nextToken());
sem[t] = Integer.parseInt(st.nextToken());
}
subset(0);
System.out.println(ans);
}
private static void subset(int index) {
if (index == select.length) {
for (int i = 0; i < select.length; i++) {
if(select[i]) {
S*=sin[i];
R+=sem[i];
count++;
}
}
if(count == 0)
return;
System.out.println(S+","+R);
ans = Math.min(ans, Math.abs(S-R));
S = 1; R = 0;count=0;
return;
}
select[index] = true;
subset(index+1);
select[index] = false;
subset(index+1);
}
}