File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.InputStreamReader ;
4+ import java.util.StringTokenizer ;
5+
6+ public class Main {
7+
8+ static int N ;
9+ static int [] A ;
10+ static long [][] dp; // [index][sum], sum은 0이상 20미만
11+ static long dfs (int idx , int result ) {
12+ if (result < 0 || result > 20 ) return 0 ;
13+ if (idx == 0 ) {
14+ // idx==0이면 a = a 인것 1개의 경우의수 아니면 a = b형태라 0의 경우의수
15+ return (result == A [0 ])? 1 : 0 ;
16+ }
17+ if (dp[idx][result] != - 1 ) return dp[idx][result];
18+
19+ long count = 0 ; // 경우의수
20+ count += dfs(idx - 1 , result + A [idx]);
21+ count += dfs(idx - 1 , result - A [idx]);
22+ dp[idx][result] = count;
23+ return count;
24+ }
25+
26+ public static void main (String [] args ) throws Exception {
27+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
28+ N = Integer . parseInt(br. readLine());
29+ A = new int [N ];
30+ StringTokenizer st = new StringTokenizer (br. readLine());
31+ for (int i = 0 ; i < N ; i++ ) {
32+ A [i] = Integer . parseInt(st. nextToken());
33+ }
34+
35+ dp = new long [N ][21 ]; // 인덱스: 0~N-1, sum: 0~20
36+ for (int i = 0 ; i < N ; i++ ) {
37+ for (int j = 0 ; j <= 20 ; j++ ) {
38+ dp[i][j] = - 1 ;
39+ }
40+ }
41+
42+ System . out. println(dfs(N - 2 , A [N - 1 ]));
43+ }
44+ }
45+ ```
You can’t perform that action at this time.
0 commit comments