PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000182. Số nhỏ nhất sau tối đa K lần đổi chỗ kề (Minimum Possible Integer After at Most K Adjacent Swaps on Digits)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số nhỏ nhất sau tối đa K lần đổi chỗ kề (Minimum Possible Integer After at Most K Adjacent Swaps on Digits)

Minimum Possible Integer After at Most K Adjacent Swaps on Digits

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given the decimal digit string num of a very large nonnegative integer without leading zeroes, and an integer KK. One operation swaps two adjacent digits. Perform at most KK swaps, possibly fewer, to obtain the lexicographically smallest digit string. The result may start with zeroes and must preserve all original digits. Deletion and nonadjacent swaps are not allowed as single operations.

Input

The first line contains the digit string num; the second contains integer KK. This is the PHOJ standard-input adaptation of the LeetCode function interface.

Output

Print the smallest attainable digit string, retaining any leading zeroes.

Subtasks

  • Subtask 1 (20 points): 1≤∣num∣≤81\le |num|\le8; 1≤K≤121\le K\le12; the input is a decimal digit string with no leading zero; output may start with zeroes.
  • Subtask 2 (30 points): 1≤∣num∣≤20001\le |num|\le2000; 1≤K≤1000001\le K\le100000; the input is a decimal digit string with no leading zero; output may start with zeroes.
  • Subtask 3 (50 points): 1≤∣num∣≤300001\le |num|\le30000; 1≤K≤10000000001\le K\le1000000000; the input is a decimal digit string with no leading zero; output may start with zeroes.

Examples

Example 1

Input

4321
4

Output

1342

Explanation

  1. Choose digit 1 at current index 3 (0-based); use 3 adjacent swaps, budget 4 → 1; result prefix 1.
  2. Choose digit 3 at current index 1 (0-based); use 1 adjacent swaps, budget 1 → 0; result prefix 13.
  3. Choose digit 4 at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix 134.
  4. Choose digit 2 at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix 1342.

Example 2

Input

100
1

Output

010

Explanation

  1. Choose digit 0 at current index 1 (0-based); use 1 adjacent swaps, budget 1 → 0; result prefix 0.
  2. Choose digit 1 at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix 01.
  3. Choose digit 0 at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix 010.