-
Notifications
You must be signed in to change notification settings - Fork 1
/
CD_DP.java
99 lines (83 loc) · 2.69 KB
/
CD_DP.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.io.*;
import java.util.StringTokenizer;
public class CD_DP {
final private static InputReader input = new InputReader(System.in);
final private static PrintWriter output = new PrintWriter(System.out);
public static void main(String[] args) {
Problem CD = new Problem();
CD.Solve();
output.close();
}
static class Problem {
int[] t;
int[][] dp;
int n, M;
void Solve() {
while(input.Ready()) {
M = input.nextInt();
n = input.nextInt();
t = new int[n];
dp = new int[n][M+1];
for(int i = 0; i < n; ++i) {
t[i] = input.nextInt();
}
for(int m = 0; m <= M; ++m) {
if(t[0] <= m) dp[0][m] = t[0];
else dp[0][m] = 0;
}
for(int i = 1; i < n; ++i) {
for(int m = 0; m <= M; ++m) {
if(t[i] <= m) {
dp[i][m] = Math.max(dp[i-1][m], dp[i-1][m - t[i]] + t[i]);
continue;
}
dp[i][m] = dp[i-1][m];
}
}
trace(n-1, M);
output.printf("sum:%d\n", dp[n-1][M]);
}
}
void trace (int i, int m) {
if(i == 0) {
if(t[i] <= m) output.printf("%d ", t[i]);
}
else {
if(t[i] <= m && dp[i][m] == dp[i-1][m - t[i]] + t[i]) {
trace(i-1, m - t[i]);
output.printf("%d ", t[i]);
return;
}
trace(i-1, m);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public boolean Ready() {
try {
return reader.ready();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}