#C4843. Largest Possible Integer After Removals
Largest Possible Integer After Removals
Largest Possible Integer After Removals
Given a string of N digits and an integer K, remove exactly K digits from the string so that the remaining number is the largest possible. You must remove the digits while keeping the order of the remaining digits. The problem can be solved using a greedy strategy by maintaining a stack of digits.
Formally, given a digit string (D = d_1d_2...d_N) and an integer (K) (with (0 < K < N)), remove exactly K digits from (D) such that the resulting number, when read from left to right, is maximized. Note that the relative order of digits in the original string must be preserved in the output.
For example, if (N = 9, K = 3) and the digit string is "123456789", one optimal removal is to remove the first three digits to obtain "456789".
inputFormat
The input is given via standard input (stdin) and consists of two lines. The first line contains two space-separated integers (N) and (K) where (N) is the length of the digit string, and (K) is the number of digits to remove. The second line contains a string of (N) digits.
outputFormat
Output the largest possible integer (as a string) that can be obtained by removing exactly (K) digits from the given digit string. The output should be printed to standard output (stdout).## sample
9 3
123456789
456789