#K3246. Remove K Digits to Form the Minimum Number

    ID: 24916 Type: Default 1000ms 256MiB

Remove K Digits to Form the Minimum Number

Remove K Digits to Form the Minimum Number

You are given a string s consisting of digits (or letters) and an integer k. Your task is to remove exactly k characters from the string such that the resulting string is the smallest possible in lexicographical order while maintaining the relative order of the remaining characters.

Note: After removal, remove any leading zeros. If the result is an empty string, output "0".

The problem can be formalized as follows:

Given a string \( s \) and an integer \( k \), remove exactly \( k \) characters from \( s \) such that the new string \( s' \) is minimized. That is, find \( s' \) such that:

[ s' = \min_{\substack{T \subseteq {0, 1, \dots, n-1} \ |T| = k}} ; \text{concatenate}(s \setminus T) ]

where the order of the remaining characters is preserved.

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains the string s (which may consist of digits or letters).
  • The second line contains an integer k, representing the number of characters to remove.

outputFormat

Output the smallest possible string (printed to stdout) after removing exactly k characters from s. Leading zeros should be removed; if the resulting string is empty, output "0".

## sample
1432219
3
1219

</p>