|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +class Main { |
| 8 | + // 어차피 숫자간의 순서는 의미 x -> 중요한건 어느 숫자가 몇개있느냐 |
| 9 | + // 0만 있는게 아니라면, 숫자의 개수가 많은게 무조건 최고 |
| 10 | + |
| 11 | + static int[] cost; |
| 12 | + static int numMax,costMax; |
| 13 | + static ArrayList<ArrayList<Integer>> list = new ArrayList<>(); |
| 14 | + |
| 15 | + static StringBuilder sb = new StringBuilder(); |
| 16 | + // list.get(i) -> i원을 써서 만들 수 있는 가장 큰 녀석. |
| 17 | + // list.get(i)에 대해서 n개의 숫자를 새로 하는게 cost_0 cost_1 ....cost_n-1이라면 |
| 18 | + // list.get(i) 끝에 0을 붙인거와 list.get(i+ cost_0)과 비교... 이런식으로 진행하면 될듯. |
| 19 | + |
| 20 | + |
| 21 | + public static void main(String[] args) throws NumberFormatException, IOException { |
| 22 | + init(); |
| 23 | + process(); |
| 24 | + print(); |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public static void init() throws NumberFormatException, IOException { |
| 30 | + BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); |
| 31 | + numMax = Integer.parseInt(br.readLine()); |
| 32 | + cost = new int[numMax]; |
| 33 | + |
| 34 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 35 | + for (int i = 0; i < numMax; i++) { |
| 36 | + cost[i] = Integer.parseInt(st.nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + costMax = Integer.parseInt(br.readLine()); |
| 40 | + for (int i = 0; i<= costMax; i++) { |
| 41 | + list.add(new ArrayList<>()); |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + public static void process() throws IOException { |
| 48 | + for (int i = 0; i < costMax; i++) { |
| 49 | + |
| 50 | + for (int j = 0; j < numMax; j++) { |
| 51 | + if (i + cost[j] > costMax) continue; |
| 52 | + |
| 53 | + ArrayList<Integer> temp = (ArrayList<Integer>) list.get(i).clone(); |
| 54 | + temp.add(j); |
| 55 | + if (isLarger(temp, list.get(i+cost[j]))) { |
| 56 | + list.set(i+cost[j], temp); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + List<Integer> ansList = list.get(costMax); |
| 64 | + for (int i = 0; i < ansList.size(); i++) { |
| 65 | + sb.append(ansList.get(i)); |
| 66 | + } |
| 67 | + if (ansList.isEmpty()) sb.append(0); |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean isLarger(List<Integer> temp, List<Integer> target) { |
| 71 | + if (temp.get(0) == 0) return false; |
| 72 | + if (temp.size() > target.size()) return true; |
| 73 | + else if (temp.size() == target.size()) { |
| 74 | + for (int i = 0; i < temp.size(); i++) { |
| 75 | + int tempNum = temp.get(i); |
| 76 | + int targetNum = target.get(i); |
| 77 | + |
| 78 | + if (tempNum > targetNum) return true; |
| 79 | + else if (tempNum < targetNum) return false; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public static void print() { |
| 88 | + System.out.println(sb); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
0 commit comments