-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1106.java
More file actions
71 lines (51 loc) · 1.89 KB
/
BOJ_1106.java
File metadata and controls
71 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ps;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BOJ_1106 {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String[] input = in.readLine().split(" ");
int C = Integer.parseInt(input[0]);
int N = Integer.parseInt(input[1]);
int[][] arr = new int[21][2];
for(int i=0;i<N;i++) {
input = in.readLine().split(" ");
arr[i][0] = Integer.parseInt(input[0]);
arr[i][1] = Integer.parseInt(input[1]);
}
int[][] dp = new int[21][1001];
//dp [i][j] i도시까지 활용해서 j만큼의 사람을 모으는 비용
for(int i=0;i<N;i++) {
for(int j=0;j<=C;j++) {
if(j==0) continue;
int cost = arr[i][0];
int customer = arr[i][1];
if(j-customer < 0) {
if(i-1 < 0) {
dp[i][j] = cost;
}
else {
dp[i][j] = Math.min(dp[i-1][j], cost);
}
}
else {
if(i-1 < 0) {
if(j-customer < 0)dp[i][j] = 0 + cost;
else {
//System.out.println("now i and j" + i+" : "+j);
dp[i][j] = dp[i][j-customer] + cost;
}
}
else {
if(j-customer < 0)dp[i][j] = Math.min(dp[i-1][j], 0+cost);
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-customer]+cost);
}
}
}
}
int min = Integer.MAX_VALUE;
min = dp[N-1][C];
System.out.println(min);
}
}