https://www.acmicpc.net/problem/1182
실버 2
N개의 정수로 이루어진 수열이 있을 때, 크기가 양수인 부분수열 중에서 그 수열의 원소를 다 더한 값이 S가 되는 경우의 수를 구하는 프로그램을 작성하시오.
첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.
첫째 줄에 합이 S가 되는 부분수열의 개수를 출력한다.
import java.util.*;
import java.io.*;
class Main {
private static int N, sum, ans = 0;
private static int[] number;
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());
sum = Integer.parseInt(st.nextToken());
number = new int[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) number[i] = Integer.parseInt(st.nextToken());
dfs(0, 0);
if(sum == 0) ans--;
System.out.println(ans);
}
public static void dfs(int index, int value){
if(index == N){
if(value == sum) ans++;
return;
}
dfs(index + 1, value);
dfs(index + 1, value + number[index]);
}
}

dfs(depth + 1, sum); // 현재 원소를 선택하지 않음
dfs(depth + 1, sum + arr[depth]); // 현재 원소를 선택함
if (S == 0) count--; // // 부분수열이 "공집합"일 경우 제외