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 C (judge verdict: Accepted)

// AOJ 0365 Swapping Characters
// 2018.4.19 bal4u

#include <stdio.h>
#include <stdlib.h>

// BIT library
int bit[200005]; int n;

int sum(int i)
{
	int s = 0;
	while (i) {
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void add(int i)
{
	while (i <= n) {
		bit[i]++;
		i += i & -i;
	}
}

void sub(int i)
{
	while (i <= n) {
		bit[i]--;
		i += i & -i;
	}
}

char s[200005];
int  c['z'+1];

void calc(int ch)
{
	while (c[ch] < n && s[c[ch]] != ch) c[ch]++;
}

int main()
{
	int k, i, j, f;
	char b[15], *p;

	fgets(p=s, sizeof(s), stdin);
	while (*p > ' ') p++; n = p-s;
	fgets(b, 15, stdin), k = atoi(b);

	for (i = 1; i <= n; i++) add(i);
	for (i = 'a'; i <= 'z'; i++) calc(i);
	for (i = 0; i < n; i++) for(j = 'a'; j <= 'z'; j++) {
		if (c[j] == n) continue;
		if ((f = sum(c[j])) <= k) {
			k -= f;
			sub(c[j]+1);
			putchar(j);
			c[j]++;
			calc(j);
			break;
		}
	}
	putchar('\n');
	return 0;
}