-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.kt
35 lines (31 loc) · 904 Bytes
/
solution.kt
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
fun main(args: Array<out String>) {
var (n, k) = readLine()!!.split(" ").map { it.toInt() }
val numbers = List(10) {
mutableListOf<Int>()
}
val sequence = java.util.ArrayDeque<Int>(n)
var removed = 0
for ((i, ch) in readLine()!!.withIndex()) {
val chnum = ch.toString().toInt()
while (!sequence.isEmpty() && k > 0) {
if (sequence.peekLast()!! >= chnum) {
break
}
sequence.pollLast()
k -= 1
removed += 1
}
sequence.offerLast(chnum)
numbers[chnum] += i - removed
}
numbers.forEach { it.sort() }
val banned = numbers.flatten().take(k).toSet()
val result = StringBuilder(n)
for ((i, ch) in sequence.withIndex()) {
if (i in banned) {
continue
}
result.append(ch)
}
println(result.toString())
}