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

#include<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 100;
const long long LLINF = 2e18 + 129;
const long long MOD = 1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

const char INF = 'z' + 1;

struct segmentTree{
    int N;
    vector< pair<char, int> > mini;
    
    void init(int siz){
        N = 1;
        while(N < siz){
            N <<= 1;
        }
        mini.resize(N * 2 - 1, {INF, -1});
        return;
    }

    void build(string &dat){
        init(dat.size());
        for(int i = 0, len = dat.size() ; i < len ; ++i){
            mini[i + N - 1] = {dat[i], i};
        }
        for(int i = N - 2 ; i >= 0 ; --i){
            mini[i] = min(mini[i * 2 + 1], mini[i * 2 + 2]);
        }
    }

    void update(int k, char x){
        k += N - 1;
        mini[k].first = x;
        while(k > 0){
            k = (k - 1) / 2;
            mini[k] = min(mini[k * 2 + 1], mini[k * 2 + 2]);
        }
    }

    pair<char, int> getMin(int a, int b, int k, int l, int r){
        if(r <= a || b <= l){
            return {INF, -1};
        }
        if(a <= l && r <= b){
            return mini[k];
        }
        pair<char, int> vl = getMin(a, b, k * 2 + 1, l, (l + r) / 2);
        pair<char, int> vr = getMin(a, b, k * 2 + 2, (l + r) / 2, r);
        return min(vl, vr);
    }

    pair<char, int> getMin(int a, int b){
        return getMin(a, b, 0, 0, N);
    }
};

struct binaryIndexedTree{
    vector<int> node;
    int n;

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

    void add(int i, int x){
        ++i;
        while(i <= n){
            node[i] += x;
            i += i & -i;
        }
        return;
    }

    void build(vector<int> &dat){
        n = dat.size();
        node.resize(n + 1);
        for(int i = 0 ; i < n ; ++i){
            add(i, dat[i]);
        }
        return;
    }

    int lower_bound(int k){
        int left = 0, right = n;
        while(right - left > 1){
            int mid = (left + right) / 2;
            if(sum(mid) >= k){
                right = mid;
            }else{
                left = mid;
            }
        }
        return right;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s;
    cin >> s;

    int k;
    cin >> k;

    segmentTree seg;
    seg.build(s);

    vector<int> v(s.size(), 1);
    binaryIndexedTree bit;
    bit.build(v);

    int n = s.size();
    string ans;
    ans.resize(n);
    rep(i, n){
        int r = bit.lower_bound(k + 1);
        pair<char, int> t = seg.getMin(0, r);
        ans[i] = t.first;
        k -= bit.sum(t.second);
        seg.update(t.second, INF);
        bit.add(t.second, -1);
    }

    cout << ans << endl;

    return 0;
}