-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSES1745.java
73 lines (54 loc) · 1.75 KB
/
CSES1745.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
import java.io.*;
public class CSES1745 {
static BufferedReader br;
static PrintWriter pw;
static StringTokenizer st;
static boolean dp[];
static int ni() {
return Integer.parseInt(st.nextToken());
}
/*
DP[i] denotes whether i is possible to sum using given denominations
Base case :
DP[i] = true if i is present as a denomination
DP[0] = true
Recursive case :
A sum = x can only be made if x is present as denomination or
x - d is possible to make for any d >= 0
for all d in denominations
DP[x] |= DP[d - x]
*/
public static void main(String args[]) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
st = new StringTokenizer(br.readLine());
int n = ni();
int arr[] = new int[n];
dp = new boolean[100001];
dp[0] = true;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; ++i) arr[i] = ni();
HashSet<Integer> h;
for (int i = 0; i < n; ++i) {
h = new HashSet<>();
for (int j = 1; j < dp.length; ++j) {
if (dp[j] && j + arr[i] < dp.length) h.add(j + arr[i]);
}
dp[arr[i]] = true;
for (Integer x : h) dp[x] = true;
}
TreeSet<Integer> t = new TreeSet<>();
int cnt = 0;
for (int i = 1; i < dp.length; ++i) {
if (dp[i]) {
++cnt;
t.add(i);
}
}
pw.println(cnt);
for (Integer x : t) pw.print(x + " ");
pw.close();
br.close();
}
}