fun main(arr:Array) { val(station, limit) = readLine()!!.trim().split(" ").map { it.toInt() } val log = (1..station).map { readLine()!!.trim().toInt() } val block = 1000 val count = mutableMapOf() val count1000 = mutableMapOf() for(i in log) { if(i >= 0) { val key1000 = i / block var cnt = count1000.filter { it.key > key1000 }.map { it.value }.sum() if(cnt >= limit) { continue } cnt += count.filter { it.key>=i && it.key < (key1000+1)*block }.map { it.value }.sum() if(cnt >= limit) { continue } count[i]?.also { count[i] = it + 1 }?: run { count[i] = 1 } count1000[key1000]?.also { count1000[key1000] = it + 1 } ?: run { count1000[key1000] = 1 } } else { val key = i * -1 count[key]?.let { a -> if(a <= 1) { count.remove(key) } else { count[key] = a - 1 } val key1000 = key / block count1000[key1000]?.let { if(it <= 1) { count1000.remove(key1000) } else { count1000[key1000] = it -1 } } } } } println(count.map { it.value }.sum()) }