|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class Main { |
| 11 | + |
| 12 | + static class City{ |
| 13 | + int cost; |
| 14 | + int value; |
| 15 | + |
| 16 | + public City(int cost, int value) { |
| 17 | + this.cost = cost; |
| 18 | + this.value = value; |
| 19 | + } |
| 20 | + } |
| 21 | + static int[] dp; |
| 22 | + static City[] cities; |
| 23 | + static int goal,cityCnt,ans = Integer.MAX_VALUE; |
| 24 | + |
| 25 | + public static void main(String[] args) throws IOException { |
| 26 | + init(); |
| 27 | + process(); |
| 28 | + print(); |
| 29 | + } |
| 30 | + |
| 31 | + private static void init() throws IOException{ |
| 32 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in));; |
| 33 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 34 | + goal = Integer.parseInt(st.nextToken()); |
| 35 | + cityCnt = Integer.parseInt(st.nextToken()); |
| 36 | + dp = new int[1101]; |
| 37 | + cities = new City[cityCnt]; |
| 38 | + |
| 39 | + Arrays.fill(dp, Integer.MAX_VALUE); |
| 40 | + dp[0] = 0; |
| 41 | + |
| 42 | + |
| 43 | + for (int i = 0; i < cityCnt; i++) { |
| 44 | + st = new StringTokenizer(br.readLine()); |
| 45 | + int cost = Integer.parseInt(st.nextToken()); |
| 46 | + int value = Integer.parseInt(st.nextToken()); |
| 47 | + cities[i] = new City(cost,value); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + private static void process() { |
| 54 | + for (City c: cities) { |
| 55 | + int cost = c.cost; |
| 56 | + int value = c.value; |
| 57 | + |
| 58 | + for (int i = value; i <= 1100; i++) { |
| 59 | + if (dp[i] > dp[i-value]+cost) { |
| 60 | + dp[i] = dp[i-value]+cost; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + for (int i = goal; i <= 1100; i++) { |
| 67 | + ans = Math.min(ans, dp[i]); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + private static void print() { |
| 75 | + System.out.println(ans); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +``` |
0 commit comments