Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions LiiNi-coder/202508/02 BOJ 1학년.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

static int N;
static int[] A;
static long[][] dp; // [index][sum], sum은 0이상 20미만
static long dfs(int idx, int result) {
if (result < 0 || result > 20) return 0;
if (idx == 0) {
// idx==0이면 a = a 인것 1개의 경우의수 아니면 a = b형태라 0의 경우의수
return (result == A[0])? 1 : 0;
}
if (dp[idx][result] != -1) return dp[idx][result];

long count = 0; // 경우의수
count += dfs(idx - 1, result + A[idx]);
count += dfs(idx - 1, result - A[idx]);
dp[idx][result] = count;
return count;
}

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
A = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
A[i] = Integer.parseInt(st.nextToken());
}

dp = new long[N][21]; // 인덱스: 0~N-1, sum: 0~20
for (int i = 0; i < N; i++) {
for (int j = 0; j <= 20; j++) {
dp[i][j] = -1;
}
}

System.out.println(dfs(N - 2, A[N - 1]));
}
}
```