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)

using System;
using System.Collections.Generic;
using static Assistant.Input;
using System.Linq;
using Assistant;
using System.Text;

namespace AOJ_0365
{
    class Program
    {
        static void Main(string[] args)
        {
            var S = Console.ReadLine();
            int N = S.Length;
            var k = ReadInt;

            List<int>[] index = new List<int>[26].Select(x => new List<int>()).ToArray();
            for (int i = 0; i < N; i++)
            {
                int a = S[i] - 'a';
                index[a].Add(i);
            }

            StringBuilder ans = new StringBuilder();

            BIT bit = new BIT(N + 2);
            int[] count = new int[26];
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < 26; j++)
                {
                    if (index[j].Count == count[j]) continue;
                    int pos = index[j][count[j]];
                    long v = pos - bit.Sum(1, pos + 1);

                    if (k >= v)
                    {
                        k -= (int)v;
                        count[j]++;
                        ans.Append((char)('a' + j));
                        bit.Add(pos + 1, pos + 1, 1);
                        break;
                    }
                }
            }

            Console.WriteLine(ans);
        }


    }

    class BIT
    {
        long[] bit0, bit1;  //1-indexed
        int n;
        public BIT(int n)
        {
            bit0 = new long[n + 1];
            bit1 = new long[n + 1];
            this.n = n;
        }

        public long Sum(int l, int r)
        {
            long ret = 0;
            ret += Sum(bit0, r) + Sum(bit1, r) * r;
            ret -= Sum(bit0, l - 1) + Sum(bit1, l - 1) * (l - 1);
            return ret;
        }

        long Sum(long[] bit, int i)
        {
            long s = 0;
            while (i > 0)
            {
                s += bit[i];
                i -= i & -i;
            }
            return s;
        }

        public void Add(int l, int r, int a)
        {
            Add(bit0, l, -a * (l - 1));
            Add(bit1, l, a);
            Add(bit0, r + 1, a * r);
            Add(bit1, r + 1, -a);
        }

        void Add(long[] bit, int i, int a)
        {
            while (i <= n)
            {
                bit[i] += a;
                i += i & -i;
            }
        }
    }
}

namespace Assistant
{
    static class Input
    {
        static List<string> line = new List<string>();
        static int index = 0;
        static String ReadNext()
        {
            if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
            return line[index++];
        }
        public static int ReadInt => int.Parse(ReadNext());
        public static long ReadLong => long.Parse(ReadNext());
        public static int[] ReadInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
        public static long[] NextLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
    }

    static class Debug
    {
        //drawmap
        //drawarray
    }
}