#P8082. Maximum Number after Deletions
Maximum Number after Deletions
Maximum Number after Deletions
Given a positive integer N, a positive integer K and an N-digit number, remove exactly K digits from the number such that the remaining number is as large as possible.
The problem can be stated mathematically as follows:
Given an N-digit number represented as a string \(S = s_1 s_2 \dots s_N\) and an integer \(K\) (with \(1 \leq K < N\)), remove exactly \(K\) digits from \(S\) to obtain a new number \(S'\) (keeping the original order of the remaining digits) that is maximum among all possibilities.
For example, if N = 4, K = 2 and the number is "1924", one optimal solution is to remove the digits '1' and '2' to get "94", which is the maximum possible result.
inputFormat
The first line contains two space-separated integers N and K (1 ≤ K < N ≤ 10^5
).
The second line contains an N-digit number (may contain leading zeros, but N is the exact length of the string).
Note: The input number is given as a string to avoid issues with large numbers.
outputFormat
Output the maximum number that can be obtained after removing exactly K digits from the given N-digit number.
sample
3 1
123
23