File tree Expand file tree Collapse file tree 1 file changed +75
-0
lines changed
Expand file tree Collapse file tree 1 file changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+
7+ class Main {
8+
9+ // IO field
10+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
12+ static StringTokenizer st;
13+
14+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
15+ static int nextInt() {return Integer . parseInt(st. nextToken());}
16+ static long nextLong() {return Long . parseLong(st. nextToken());}
17+ static void bwEnd() throws Exception {bw. flush();bw. close();}
18+
19+ // Additional field
20+
21+ static int [] A ;
22+ static long [][] dp;
23+ static long [][] sum;
24+ static int N , K ;
25+
26+ public static void main(String [] args) throws Exception {
27+
28+ ready();
29+ solve();
30+
31+ bwEnd();
32+ }
33+
34+ static void ready() throws Exception {
35+
36+ N = Integer . parseInt(br. readLine());
37+ A = new int [N ];
38+ nextLine();
39+ for (int i= 0 ;i< N ;i++ ) A [i] = nextInt();
40+ K = Integer . parseInt(br. readLine());
41+ dp = new long [500001 ][K + 1 ];
42+ sum = new long [500001 ][K + 1 ];
43+
44+ }
45+
46+ static void solve() throws Exception {
47+
48+ Arrays . sort(A );
49+ dp[A [0 ]][1 ] = 1 ;
50+ sum[A [0 ]][1 ] = 1 ;
51+ for (int i= 1 ;i< N ;i++ ) {
52+ for (int s= 500000 ;s> 2 * A [i];s-- ) {
53+ for (int k= K ;k> 2 ;k-- ) {
54+ dp[s][k] += sum[s- A [i]][k- 1 ];
55+ }
56+ }
57+ for (int s= 500000 ;s> A [i];s-- ) {
58+ for (int k= K ;k> 1 ;k-- ) {
59+ sum[s][k] += sum[s- A [i]][k- 1 ];
60+ }
61+ }
62+ for (int s= 500000 ;s> A [i];s-- ) if (dp[s- A [i]][1 ] != 0 ) dp[s][2 ] += dp[s- A [i]][1 ];
63+ dp[A [i]][1 ]++ ;
64+ sum[A [i]][1 ]++ ;
65+ }
66+
67+ long ans = 0 ;
68+ for (int i= 1 ;i<= 500000 ;i++ ) ans += dp[i][K ];
69+ bw. write(ans+ " \n " );
70+
71+ }
72+
73+ }
74+
75+ ```
You can’t perform that action at this time.
0 commit comments