-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
52 lines (39 loc) · 1.12 KB
/
Main.java
File metadata and controls
52 lines (39 loc) · 1.12 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N;
static int S;
static int[] num;
static int count = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
S = Integer.parseInt(st.nextToken());
num = new int[N];
st = new StringTokenizer(br.readLine());
int i = 0;
while (st.hasMoreTokens()) {
num[i] = Integer.parseInt(st.nextToken());
i++;
}
DFS(0, 0);
if(S==0){
System.out.println(count-1);
}else{
System.out.println(count);
}
}
private static void DFS(int sum, int r) {
if (N == r) {
if (sum == S) {
count++;
}
return;
}
DFS(sum + num[r], r + 1) ;
DFS(sum , r + 1) ;
}
}