Skip to content

Commit 5d632c9

Browse files
authored
부분수열의합_2 (#63)
1 parent 03414b9 commit 5d632c9

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

pr/부분수열의 합_2/Main.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int n;
6+
static int[] arr;
7+
// N=20, max val=100,000 => Max sum = 2,000,000
8+
static boolean[] check = new boolean[2000001];
9+
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
n = sc.nextInt();
13+
arr = new int[n];
14+
for (int i = 0; i < n; i++) {
15+
arr[i] = sc.nextInt();
16+
}
17+
18+
solve();
19+
}
20+
21+
public static void solve() {
22+
dfs(0, 0);
23+
24+
// Find the smallest natural number (starting from 1)
25+
for (int i = 1; i < check.length; i++) {
26+
if (!check[i]) {
27+
System.out.println(i);
28+
break;
29+
}
30+
}
31+
}
32+
33+
public static void dfs(int depth, int sum) {
34+
if (depth == n) {
35+
check[sum] = true;
36+
return;
37+
}
38+
39+
dfs(depth + 1, arr[depth] + sum);
40+
dfs(depth + 1, sum);
41+
42+
}
43+
}

0 commit comments

Comments
 (0)