|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.OutputStreamWriter; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +public class BJ_19942_다이어트 { |
| 10 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + private static StringTokenizer st; |
| 13 | + |
| 14 | + private static int N, mp, mf, ms, mv; |
| 15 | + private static int maxMask; |
| 16 | + private static int minCost; |
| 17 | + private static String bestResult; |
| 18 | + |
| 19 | + private static int[][] ingre; |
| 20 | + |
| 21 | + public static void main(String[] args) throws IOException { |
| 22 | + init(); |
| 23 | + sol(); |
| 24 | + printAns(); |
| 25 | + } |
| 26 | + |
| 27 | + private static void init() throws IOException { |
| 28 | + N = Integer.parseInt(br.readLine()); |
| 29 | + |
| 30 | + st = new StringTokenizer(br.readLine()); |
| 31 | + mp = Integer.parseInt(st.nextToken()); |
| 32 | + mf = Integer.parseInt(st.nextToken()); |
| 33 | + ms = Integer.parseInt(st.nextToken()); |
| 34 | + mv = Integer.parseInt(st.nextToken()); |
| 35 | + |
| 36 | + minCost = Integer.MAX_VALUE; |
| 37 | + bestResult = null; |
| 38 | + |
| 39 | + ingre = new int[N][5]; |
| 40 | + for (int i = 0; i < N; i++) { |
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + |
| 43 | + ingre[i][0] = Integer.parseInt(st.nextToken()); // p |
| 44 | + ingre[i][1] = Integer.parseInt(st.nextToken()); // f |
| 45 | + ingre[i][2] = Integer.parseInt(st.nextToken()); // s |
| 46 | + ingre[i][3] = Integer.parseInt(st.nextToken()); // v |
| 47 | + ingre[i][4] = Integer.parseInt(st.nextToken()); // cost |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static void sol() { |
| 52 | + maxMask = (1 << N); |
| 53 | + |
| 54 | + for (int mask = 1; mask < maxMask; mask++) { |
| 55 | + int sp = 0, sf = 0, ss = 0, sv = 0, sc = 0; |
| 56 | + StringBuilder sb = new StringBuilder(); |
| 57 | + |
| 58 | + for (int i = 0, bit = 1; i < N; i++, bit <<= 1) { |
| 59 | + // 포함되는 재료 |
| 60 | + if ((mask & bit) != 0) { |
| 61 | + sp += ingre[i][0]; |
| 62 | + sf += ingre[i][1]; |
| 63 | + ss += ingre[i][2]; |
| 64 | + sv += ingre[i][3]; |
| 65 | + sc += ingre[i][4]; |
| 66 | + sb.append(i + 1).append(" "); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (sp >= mp && sf >= mf && ss >= ms && sv >= mv) { |
| 71 | + String currentResult = sb.toString(); |
| 72 | + |
| 73 | + if (sc < minCost) { |
| 74 | + minCost = sc; |
| 75 | + bestResult = currentResult; |
| 76 | + } else if (sc == minCost) { |
| 77 | + if (bestResult == null || currentResult.compareTo(bestResult) < 0) { |
| 78 | + bestResult = currentResult; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void printAns() throws IOException { |
| 86 | + if (bestResult == null) { |
| 87 | + bw.write("-1"); |
| 88 | + } else { |
| 89 | + bw.write(minCost + "\n"); |
| 90 | + bw.write(bestResult); |
| 91 | + } |
| 92 | + |
| 93 | + bw.flush(); |
| 94 | + bw.close(); |
| 95 | + br.close(); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
0 commit comments