|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | +
|
| 6 | +public class Main { |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static Set<Integer>[] dp; |
| 10 | + private static int[] weight; |
| 11 | + private static int N, sum, max; |
| 12 | +
|
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | + DP(); |
| 16 | +
|
| 17 | + int answer = 0; |
| 18 | +
|
| 19 | + for (int val : dp[dp.length-1]) { |
| 20 | + answer = Math.max(val, answer); |
| 21 | + } |
| 22 | +
|
| 23 | + if (N%2 == 1) { |
| 24 | + for (int val : dp[dp.length-2]) { |
| 25 | + answer = Math.max(val, answer); |
| 26 | + } |
| 27 | + } |
| 28 | +
|
| 29 | + bw.write(answer + " " + (sum - answer) + "\n"); |
| 30 | + bw.flush(); |
| 31 | + bw.close(); |
| 32 | + br.close(); |
| 33 | + } |
| 34 | +
|
| 35 | + private static void init() throws IOException { |
| 36 | + N = Integer.parseInt(br.readLine()); |
| 37 | + sum = 0; |
| 38 | + max = 0; |
| 39 | +
|
| 40 | + int length = N/2; |
| 41 | + if (N%2 == 1) length++; |
| 42 | + weight = new int[N]; |
| 43 | + dp = new Set[length + 1]; |
| 44 | + for (int i = 0; i < dp.length; i++) { |
| 45 | + dp[i] = new HashSet<>(); |
| 46 | + } |
| 47 | +
|
| 48 | + dp[0].add(0); |
| 49 | +
|
| 50 | + for (int i = 0; i < N; i++) { |
| 51 | + weight[i] = Integer.parseInt(br.readLine()); |
| 52 | + sum += weight[i]; |
| 53 | + } |
| 54 | +
|
| 55 | + max = sum / 2; |
| 56 | + } |
| 57 | +
|
| 58 | + private static void DP() { |
| 59 | + for (int i = 0; i < N; i++) { |
| 60 | + for (int j = dp.length-1; j > 0; j--) { |
| 61 | + if (!dp[j-1].isEmpty()) { |
| 62 | + for (int val : dp[j-1]) { |
| 63 | + int result = val + weight[i]; |
| 64 | + if (result <= max) { |
| 65 | + dp[j].add(result); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
0 commit comments