#P1106. Minimum Number by Removing Digits

    ID: 13114 Type: Default 1000ms 256MiB

Minimum Number by Removing Digits

Minimum Number by Removing Digits

You are given a high-precision positive integer \( n \) (with at most 250 digits) and an integer \( k \). Your task is to remove exactly \( k \) digits from \( n \) (without changing the order of the remaining digits) so that the new number formed by the remaining digits is as small as possible.

Note: If all digits are removed, the result is considered to be 0.

Example:

Input: n = "1432219", k = 3
Output: "1219"
Explanation: Remove the digits '4', '3', and '2' to form the minimum number "1219".

Solve this problem using an appropriate algorithm that guarantees the optimal solution while handling the high-precision input.

inputFormat

The input consists of two lines:

  • The first line contains the high-precision positive integer \( n \) as a string (its length does not exceed 250 digits).
  • The second line contains the integer \( k \) indicating how many digits to remove.

outputFormat

Output the smallest possible number (as a string) that can be obtained after removing exactly \( k \) digits from \( n \). If the resulting number has leading zeros, they should be removed. If all digits are removed, output "0".

sample

1432219
3
1219