File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws Exception {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+
9+ int n = Integer . parseInt(br. readLine());
10+ int [] num = new int [n];
11+
12+ StringTokenizer st = new StringTokenizer (br. readLine());
13+ for (int i = 0 ; i < n; i++ ) {
14+ num[i] = Integer . parseInt(st. nextToken());
15+ }
16+
17+ long [][] dp = new long [n - 1 ][21 ];
18+
19+ dp[0 ][num[0 ]] = 1 ;
20+
21+ for (int i = 1 ; i < n - 1 ; i++ ) {
22+ for (int sum = 0 ; sum <= 20 ; sum++ ) {
23+ if (dp[i - 1 ][sum] == 0 ) continue ;
24+
25+ int plus = sum + num[i];
26+ int minus = sum - num[i];
27+
28+ if (plus <= 20 ) {
29+ dp[i][plus] += dp[i - 1 ][sum];
30+ }
31+ if (minus >= 0 ) {
32+ dp[i][minus] += dp[i - 1 ][sum];
33+ }
34+ }
35+ }
36+
37+ int target = num[n - 1 ];
38+ System . out. println(dp[n - 2 ][target]);
39+ }
40+ }
41+
42+ ```
You can’t perform that action at this time.
0 commit comments