Skip to content

Commit 7a34fca

Browse files
authored
[20251002] BOJ / G4 / 사탕 가게 / 김수연
1 parent 0db4710 commit 7a34fca

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class boj4781 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception { st = new StringTokenizer(br.readLine()); }
9+
static int nextInt() { return Integer.parseInt(st.nextToken()); }
10+
static double nextDouble() { return Double.parseDouble(st.nextToken()); }
11+
12+
public static void main(String[] args) throws Exception {
13+
while(true) {
14+
nextLine();
15+
int n = nextInt(); // 사탕 종류의 수
16+
int m = (int) (nextDouble() * 100); // 상근이 돈 양
17+
if (n == 0 && m == 0) break;
18+
Candy[] candy = new Candy[n+1];
19+
for (int i = 1; i <= n; i++) {
20+
nextLine();
21+
candy[i] = new Candy(nextInt(), (int) (nextDouble() * 100 + 0.5));
22+
}
23+
System.out.println(solve(n, m, candy));
24+
}
25+
}
26+
27+
static private int solve(int n, int m, Candy[] candy) {
28+
int[] dp = new int[m+1];
29+
for (int i = 1; i <= n; i++) { // 사탕 종류
30+
int cal = candy[i].c;
31+
int val = candy[i].p;
32+
for (int j = 0; j <= m; j++) {
33+
if (j - val >= 0) dp[j] = Math.max(dp[j], dp[j-val] + cal);
34+
}
35+
}
36+
return dp[m];
37+
}
38+
39+
static class Candy {
40+
int c;
41+
int p;
42+
public Candy(int c, int p) {
43+
this.c = c;
44+
this.p = p;
45+
}
46+
}
47+
}
48+
```

0 commit comments

Comments
 (0)