Problem:
Swapping Characters

You are given a string and a number k. You are suggested to generate new strings by swapping any adjacent pair of characters in the string up to k times. Write a program to report the lexicographically smallest string among them.

Input

The input is given in the following format.

s
k

The first line provides a string s. The second line provides the maximum number of swapping operations k (0 ≤ k ≤ 109). The string consists solely of lower-case alphabetical letters and has a length between 1 and 2 × 105.

Output

Output the lexicographically smallest string.

Sample Input 1

pckoshien
3

Sample Output 1

ckopshien

Sample Input 2

pckoshien
10

Sample Output 2

cekophsin

Solution in Kotlin (judge verdict: Accepted)

import java.util.*

class SumBit(private val size: Int){
    private val mArray = IntArray(size){0}
    fun sumAt(index:Int): Int {
        var sum = 0
        var idx = index
        while(idx >= 0){
            sum += mArray[idx]
            idx -= (idx + 1) and idx.inv()
        }
        return sum
    }
    fun incrementAt(index:Int) {
        var idx = index
        while (idx < size){
            mArray[idx]++
            idx += (idx + 1) and idx.inv()
        }
    }
}
fun main(args:Array<String>):Unit {
    val str = readLine()!!.trim()
    var k = readLine()!!.trim().toInt()
    val queues = Array(26){PriorityQueue<Int>()}
    for ((queue, c) in queues.zip('a' .. 'z')) {
        for (i in 0 until str.length){
            if (str[i] == c) queue.add(i)
        }
    }
    val res = StringBuilder()
    val bit = SumBit(str.length)
    loop@while(res.length < str.length) {
        for (i in 0 until 26){
            if (queues[i].isNotEmpty() && queues[i].peek() - bit.sumAt(queues[i].peek()) <= k) {
                k -= queues[i].peek() - bit.sumAt(queues[i].peek())
                res.append('a'.plus(i))
                bit.incrementAt(queues[i].poll())
                continue@loop
            }
        }
    }
    println(res.toString())
}