fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class Main
  6. {
  7.  
  8. public static void main (String[] args) throws IOException
  9. {
  10. int n = Integer.parseInt(br.readLine());
  11. int[] nums = new int[n];
  12. long[][] dp = new long[n-1][21];
  13.  
  14. StringTokenizer st = new StringTokenizer(br.readLine());
  15. for(int i = 0 ; i < n ; i++)
  16. {
  17. nums[i] = Integer.parseInt(st.nextToken());
  18. }
  19.  
  20. dp[0][nums[0]] = 1;
  21.  
  22. for(int i = 1 ; i < n-1 ; i++)
  23. {
  24. for(int j = 0 ; j < 21 ; j++)
  25. {
  26. if(dp[i-1][j] > 0)
  27. {
  28. if(j - nums[i] > -1)
  29. {
  30. dp[i][j - nums[i]] += dp[i-1][j];
  31. }
  32.  
  33. if(j + nums[i] <= 20)
  34. {
  35. dp[i][j + nums[i]] += dp[i-1][j];
  36. }
  37. }
  38. }
  39. }
  40.  
  41. System.out.println(dp[n-2][nums[n-1]]);
  42. }
  43.  
  44. }
Success #stdin #stdout 0.08s 47448KB
stdin
40
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
stdout
7069052760