Skip to content

Commit 87308ec

Browse files
authored
Merge pull request #54 from SoptJune/coin2
[2294] : 동전2
2 parents 3080a52 + 323fb2c commit 87308ec

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed
+27-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
package baekjun.dp
22

3-
import java.io.BufferedReader
4-
import java.io.BufferedWriter
5-
import java.io.InputStreamReader
6-
import java.io.OutputStreamWriter
7-
3+
private val br = System.`in`.bufferedReader()
4+
private val bw = System.out.bufferedWriter()
85
fun main() {
9-
val br = BufferedReader(InputStreamReader(System.`in`))
10-
val bw = BufferedWriter(OutputStreamWriter(System.out))
11-
12-
val (n, k) = br.readLine().split("")
13-
println(n)
14-
println(k)
6+
val maxValue = 100_001
7+
val limit = 10_000
8+
// n <= 100, k <= 10,000
9+
val (n, k) = br.readLine().split(" ").map { it.toInt() }
10+
val values = IntArray(n).apply {
11+
repeat(n) {
12+
val v = br.readLine().toInt()
13+
if(v > limit) return@repeat
14+
this[it] = v
15+
}
16+
}
17+
val d = IntArray(10_001) { maxValue }.apply {
18+
values.forEach { v ->
19+
this[v] = 1
20+
}
21+
}
22+
repeat(k + 1) { i ->
23+
values.forEach { v ->
24+
val diff = i - v
25+
if (diff >= 0) {
26+
d[i] = minOf(d[i], d[i - v] + 1)
27+
}
28+
}
29+
}
30+
println(if (d[k] == maxValue) -1 else d[k])
1531
}

0 commit comments

Comments
 (0)