Skip to content

Commit f472027

Browse files
authored
Merge pull request #1492 from AlgorithmWithGod/zinnnn37
[20251123] BOJ / G4 / 서울에서 경산까지 / 김민진
2 parents d9db658 + c337f8f commit f472027

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Arrays;
4+
import java.util.StringTokenizer;
5+
6+
public class BJ_14863_서울에서_경산까지 {
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 N, K, ans;
13+
private static int[][] dp, cost;
14+
15+
public static void main(String[] args) throws IOException {
16+
init();
17+
sol();
18+
}
19+
20+
private static void init() throws IOException {
21+
st = new StringTokenizer(br.readLine());
22+
23+
N = Integer.parseInt(st.nextToken());
24+
K = Integer.parseInt(st.nextToken());
25+
26+
cost = new int[N + 1][4];
27+
for (int i = 1; i <= N; i++) {
28+
st = new StringTokenizer(br.readLine());
29+
for (int j = 0; j < 4; j++) {
30+
cost[i][j] = Integer.parseInt(st.nextToken());
31+
}
32+
}
33+
34+
dp = new int[N + 1][K + 1];
35+
for (int i = 0; i <= N; i++) {
36+
Arrays.fill(dp[i], -1);
37+
}
38+
dp[0][0] = 0;
39+
}
40+
41+
private static void sol() throws IOException {
42+
for (int i = 1; i <= N; i++) {
43+
for (int j = 0; j <= K; j++) {
44+
// 도보 선택
45+
if (j >= cost[i][0] && dp[i - 1][j - cost[i][0]] != -1) {
46+
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - cost[i][0]] + cost[i][1]);
47+
}
48+
49+
// 자전거 선택
50+
if (j >= cost[i][2] && dp[i - 1][j - cost[i][2]] != -1) {
51+
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - cost[i][2]] + cost[i][3]);
52+
}
53+
}
54+
}
55+
56+
for (int i = 0; i <= K; i++) {
57+
if (dp[N][i] != -1) {
58+
ans = Math.max(ans, dp[N][i]);
59+
}
60+
}
61+
62+
bw.write(ans + "");
63+
bw.flush();
64+
bw.close();
65+
br.close();
66+
}
67+
68+
}
69+
```

0 commit comments

Comments
 (0)