<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

296. Sasha vs. Kate
Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



During the regular Mars's World Finals Subregional Programming Contest a boy Sasha lost N "Mars" bars of chocolate to a girl Kate. But for two years already Sasha does not hurry to pay his debt. And now Sasha and Kate decided that Sasha will give Kate P chocolate bars, where number P can be obtained from the number N by removing exactly K decimal digits. Sasha generously let Kate to choose digits to be removed. Your task is to find out how many bars Sasha will give Kate. Of course Kate will choose K digits from the number N in such a way that the resulting number P would be maximal.

Input
The first line of the input file contains two integer numbers N and K (1≤ N≤ 101000; 0≤ K≤ 999). Number K is strictly less than the number of digits in N. N will not have any leading zeros.

Output
Output the unknown P.

Example(s)
sample input
sample output
1992 2
99

sample input
sample output
1000 2
10

<|response|>
1. Abridged Problem Statement  
You are given a non-negative integer N as a string (with no leading zeros) and an integer K (< length of N). Remove exactly K digits from N (keeping the remaining digits in their original order) so that the resulting number P is as large as possible. Output P.

2. Key Observations  
- Whenever you have a digit sequence and you can delete some digits, to maximize the result you want larger digits as far left as possible.  
- A classical way to enforce “bigger‐first” is to scan the digits left to right, and whenever the current digit is larger than the last digit you have kept, you should remove that last digit—provided you still have deletions remaining.  
- After this “local” greedy removal, if you have deletions left, you simply remove them from the end (because the suffix is now non-increasing).  
- This entire procedure can be implemented with a stack (or dynamic array) in O(|N|) time.

3. Full Solution Approach  
Let s = N as a string of length M, and let K be the number of digits we must remove. We will build a stack (initially empty) to hold the digits of the resulting number:

1. Initialize an empty stack (e.g., a string `stk`) and set `rem = K`.  
2. Iterate over each digit `d` in s from left to right:
   a. While `rem > 0` and the stack is non-empty and the top of the stack is less than `d`, pop the stack (this simulates deleting that smaller digit) and decrement `rem`.  
   b. Push `d` onto the stack.  
3. If after processing all digits we still have `rem > 0`, pop `rem` more digits from the end of the stack.  
4. The stack now contains `M−K` digits. Convert it to a string and strip leading zeros (unless the entire string is zeros, then return "0").  
5. Print the result.

This runs in O(M) time and uses O(M) extra space, where M = |N|.

4. C++ Implementation with Detailed Comments

```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string N;
    int K;
    cin >> N >> K;

    // rem = how many digits we still need to remove
    int rem = K;
    // We'll build the answer in 'stk'
    string stk;
    stk.reserve(N.size());

    // Process each digit
    for(char d : N) {
        // While we can delete a previous digit, and doing so
        // will make a bigger leading digit, pop it.
        while (rem > 0 && !stk.empty() && stk.back() < d) {
            stk.pop_back();
            rem--;
        }
        // Keep the current digit
        stk.push_back(d);
    }

    // If we still have deletions left, remove from the end
    // (the remaining suffix is non-increasing)
    while (rem > 0 && !stk.empty()) {
        stk.pop_back();
        rem--;
    }

    // Remove leading zeros
    int pos = 0;
    while (pos < (int)stk.size() && stk[pos] == '0') {
        pos++;
    }

    // Construct the final answer
    string answer = (pos == (int)stk.size() ? "0" : stk.substr(pos));

    cout << answer << "\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments

```python
import sys

def max_after_k_removals(N: str, K: int) -> str:
    rem = K
    stack = []

    # Greedy pass: remove smaller previous digits when possible
    for d in N:
        while rem > 0 and stack and stack[-1] < d:
            stack.pop()
            rem -= 1
        stack.append(d)

    # If deletions remain, chop from the end
    if rem > 0:
        stack = stack[:-rem]

    # Build the result string and strip leading zeros
    result = ''.join(stack).lstrip('0')
    return result if result else "0"

def main():
    data = sys.stdin.read().split()
    N, K = data[0], int(data[1])
    print(max_after_k_removals(N, K))

if __name__ == "__main__":
    main()
```

Explanation of the Core Idea:
- By always removing a smaller digit that appears before a larger one (when we can), we push larger digits as far left as possible.
- Once no more “improvements” are possible in this way, any remaining removals are best spent trimming the tail, which by then is in non-increasing order.
- Stripping leading zeros ensures we don’t output something like “00123”—we output “123”, or “0” if all digits are removed/zero.