-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMoneySums.java
More file actions
39 lines (38 loc) · 1.1 KB
/
MoneySums.java
File metadata and controls
39 lines (38 loc) · 1.1 KB
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
import java.io.*;
import java.util.*;
class MoneySums {
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String s[] = br.readLine().split(" ");
int a[] = new int[n];
int sum = 0;
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(s[i]);
sum += a[i];
}
boolean dp[][] = new boolean[sum+1][n+1];
Arrays.sort(a);
dp[0][0] = true;
ArrayList<Integer> ans = new ArrayList<>();
for(int i = 1; i < dp.length; i++) {
for(int j = 1; j <= n; j++) {
int val = a[j-1];
if(dp[i][j-1])
dp[i][j] = true;
else if(i == val) {
dp[i][j] = true;
} else if(i > val) {
dp[i][j] = dp[i - val][j-1];
}
}
if(dp[i][n])
ans.add(i);
}
System.out.println(ans.size());
for(int val : ans) {
System.out.print(val +" ");
}
}
}