|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class BJ_1106_호텔 { |
| 7 | + |
| 8 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 10 | + private static StringTokenizer st; |
| 11 | + |
| 12 | + private static int C; |
| 13 | + private static int N; |
| 14 | + private static int[] dp; |
| 15 | + private static int[][] cost; |
| 16 | + |
| 17 | + public static void main(String[] args) throws IOException { |
| 18 | + init(); |
| 19 | + sol(); |
| 20 | + } |
| 21 | + |
| 22 | + private static void init() throws IOException { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + C = Integer.parseInt(st.nextToken()); |
| 25 | + N = Integer.parseInt(st.nextToken()); |
| 26 | + |
| 27 | + cost = new int[N][2]; |
| 28 | + for (int i = 0; i < N; i++) { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + cost[i][0] = Integer.parseInt(st.nextToken()); |
| 31 | + cost[i][1] = Integer.parseInt(st.nextToken()); |
| 32 | + } |
| 33 | + |
| 34 | + dp = new int[C + 100]; |
| 35 | + Arrays.fill(dp, 987654321); |
| 36 | + dp[0] = 0; |
| 37 | + } |
| 38 | + |
| 39 | + private static void sol() throws IOException { |
| 40 | + for (int[] c : cost) { |
| 41 | + for (int i = c[1]; i < C + 100; i++) { |
| 42 | + dp[i] = Math.min(dp[i], dp[i - c[1]] + c[0]); |
| 43 | + } |
| 44 | + } |
| 45 | + bw.write(findMin() + ""); |
| 46 | + bw.flush(); |
| 47 | + bw.close(); |
| 48 | + br.close(); |
| 49 | + } |
| 50 | + |
| 51 | + private static int findMin() { |
| 52 | + int ans = dp[C]; |
| 53 | + for (int i = C + 1; i < C + 100; i++) { |
| 54 | + ans = Math.min(ans, dp[i]); |
| 55 | + } |
| 56 | + return ans; |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | +``` |
0 commit comments