Ania has a large integer S. Its decimal representation has length n and doesn't contain any leading zeroes. Ania is allowed to change at most k digits of S. She wants to do it in such a way that S still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?
Input
The first line contains two integers n and k (1 ≤ n ≤ 200 000, 0 ≤ k ≤ n) — the number of digits in the decimal representation of S and the maximum allowed number of changed digits.
The second line contains the integer S. It's guaranteed that S has exactly n digits and doesn't contain any leading zeroes.
Output
Output the minimal possible value of S which Ania can end with. Note that the resulting integer should also have n digits.
Examples
Input
5 3
51528
Output
10028
Input
3 2
102
Output
100
Input
1 1
1
Output
0
Note
A number has leading zeroes if it consists of at least two digits and its first digit is 0. For example, numbers 00, 00069 and 0101 have leading zeroes, while 0, 3000 and 1010 don't have leading zeroes.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given a problem: Ania has a large integer S as a string of length n, no leading zeros (except the number could be 0? Actually leading zero means the first digit cannot be zero if n>1; but if n=1, digit can be 0? The problem says: "S has exactly n digits and doesn't contain any leading zeroes." So if n=1, S can be '0'? Possibly but it says doesn't contain leading zeroes, so if n=1, '0' is allowed because it's just the number 0 (not considered leading). But the examples: Input 1 1; 1 -> output 0. So indeed n=1, S=1, change at most 1 digit, minimal is 0. So it's valid.
Goal: Change at most k digits (we can change fewer) to get minimal integer (interpreted as integer with no leading zeros) while respecting: final integer should have exactly n digits, and cannot have leading zeros. That means if n>1, first digit cannot be 0. So we can change digits, but we cannot make the first digit zero if n>1. We want minimal possible integer.
Interpretation: We have string of digits; we can replace up to k of them with any digit (0-9) we choose. But we must maintain the length and no leading zeros. Find the lexicographically smallest string (since minimal integer corresponds to smallest numeric value, which for same length is lexicographically smallest). However, careful: For integers of same length, indeed the smaller numeric value corresponds to lexicographically smaller string because no leading zeros. So we want to transform S into the lexicographically smallest string possible by at most k changes, with constraint first digit not zero if n>1. Also we can change fewer than k digits.
We need to output the resulting integer.
Constraints: n up to 200k. So O(n) algorithm.
We need to think about strategy: To minimize the integer, we want as many leading digits as possible to be 0? But can't if n>1 and first digit must not be 0. So first digit must be >=1. Ideally, we want first digit as small as possible, but at least 1 if n>1. So we can consider changing first digit to 1 if it's not already 1, provided we have changes left. That would be beneficial if it reduces value. Similarly, for other positions, we want to change to 0 if possible to minimize.
But we have limited k. So we need to allocate changes to positions to get the smallest lexicographic order. Lexicographically, earlier positions matter more. So we should prioritize making earlier digits as low as possible, subject to constraints.
Thus, the typical approach: For each position i (0-indexed), consider the current digit d = S[i]. We want to set it to target digit t. For i=0 (most significant digit), if n>1, t must be >=1. The best possible is 1 if d != 1 (or even if d=1 we keep). But note: If we can change digit 0? Actually d can be any from 1-9 (since no leading zero originally). So for i=0, the smallest digit allowed is 1. So we set it to 1 if d != 1, cost 1. If d=1, cost 0.
For i>0, smallest digit allowed is 0. So we set to 0 if possible, cost 1 if d != 0; else cost 0.
But is it always optimal to set earlier positions to their minimal values? Yes, because lexicographic ordering: having a smaller digit at an earlier position outweighs any changes later, even if later digits become larger. However, we have a constraint that we can only change up to k digits. So we need to decide how many changes we can allocate to achieve the minimal lexicographic string.
But careful: If we change an earlier digit to something less, that uses a change. But we also might consider not changing it if we need to use changes to fix something else? Since we want minimal, it's always beneficial to change an earlier digit to a smaller target as long as we have change budget. However, there might be a subtlety: Changing a digit to a smaller one uses a change, but we might need to also change later digits? But later digits we would also want to minimize, but if we run out of changes, we cannot minimize them. However, because earlier positions are more significant, using a change to improve an earlier position is always worth it even if it prevents improving later positions. As long as we have the change budget, we should do it. But we must also consider that sometimes we could make the same digit smaller without cost if it's already minimal? Obviously.
But the decision is: For each position from left to right, decide what digit to set. Since we want lexicographically smallest, we should, for each position, try to set it to the smallest allowed digit (given the remaining budget), but we need to consider that if we set it to the smallest, it might cost 1 if it's not already that digit. If we have budget, we do it. However, there is a nuance: maybe we shouldn't set it to the smallest if that uses a change that could be used later? But because earlier positions have higher priority, it's better to spend budget earlier. If we have budget B, and we have two options: spend 1 now to reduce the current digit from d to 0 (or 1) versus saving that 1 to reduce a later digit from 9 to 0, the earlier reduction yields a strictly smaller number (if d>target). For instance: S = "99", k=1. Options: change first digit to 1 => "19", change second to 0 => "90". 19 < 90, so first change is better. So yes, prioritize earlier positions.
Thus greedy: iterate from leftmost to rightmost; for each position i, compute the smallest digit allowed (1 for i=0 if n>1, else 0 for i>0; also if n==1, i=0 allowed digit 0 because leading zero not an issue). Let target = (i == 0 and n > 1) ? 1 : 0. If current digit > target and we have remaining k>0, then change it to target (cost 1). Else, keep as is. However, is this always optimal? Let's test with examples:
Example1: n=5, k=3, S=51528. According to greedy:
i=0: target 1, current=5>1, k=3>0 -> change to 1, k=2. string becomes 1? originally? We'll build.
i=1: target 0, current=1? Actually original S[1]=1, but after change? Actually we consider original S? We're modifying S; but for subsequent positions, we consider the original digits? We are allowed to change at most k digits total; we need to decide which positions to change. Greedy on the fly: i=1: current digit is original S[1]=1? But we might have changed it? We haven't changed i=1 yet. So original is 1. target 0, and 1>0, k=2>0 -> change to 0, k=1.
i=2: original S[2]=5? Actually index: S[0]=5, S[1]=1, S[2]=5, S[3]=2, S[4]=8. i=2: target 0, current=5>0, k=1>0 -> change to 0, k=0.
i=3: target 0, current=2>0, but k=0, so keep 2.
i=4: target 0, current=8>0, keep 8.
Result: 10028? Wait: i=2 becomes 0, so digits: 1,0,0,2,8 => "10028". That's example output "10028". So works.
Example2: n=3,k=2,S=102. Greedy:
i=0: target=1, current=1? Actually S[0]=1, equals target, no change (k=2).
i=1: target=0, current=0? S[1]=0, equals target, no change.
i=2: target=0, current=2>0, k=2>0 -> change to 0, k=1. Result: 100. Output is 100. Works.
Example3: n=1,k=1,S=1. i=0: n=1 => allowed digit 0? Since single-digit numbers can be 0. So target=0. current=1>0, k=1>0 -> change to 0 => 0. Output 0. Works.
So greedy seems correct? But wait, consider scenario where first digit is already 1; we don't change it. But maybe we could change first digit to something even smaller? Not possible because smallest allowed is 1 (if n>1). So fine.
But what about scenario where first digit is 1, but we could change first digit to 0 to get smaller number? That's not allowed because leading zero not allowed (unless n=1). So not allowed.
Edge case: n>1, S[0] is 1 originally, but we have k>0. Could we change S[0] to 0 to get a number with leading zero? Not allowed. So cannot.
Thus the greedy is straightforward: change earliest digits to 0, except first digit to 1 if needed.
But is it always optimal to change a digit to 0 even if it's already 0? No cost.
Wait, what about the possibility that we might want to not change a digit to 0 at some earlier position if we have limited changes? Because maybe by not changing that early digit (say it's 1 at position i>0), we could use the change to make a later digit 0, but that would result in a larger number? Example: S = "10", k=1. Greedy: i=0: target=1, current=1, no change. i=1: target=0, current=0? S[1]=0, so no change. Result "10". But maybe we could change i=0 to something? Not allowed to 0, but could change to smaller? No, smallest is 1. So no better. So result 10.
But what about S = "19", k=1. Greedy: i=0: target=1, current=1, no change. i=1: target=0, current=9, change to 0 => "10". That's minimal? Could we instead change i=0 to something? Changing i=0 to 1 is same, or to 0 not allowed. So "10" is minimal. Good.
Now consider S = "91", k=1. Greedy: i=0: target=1, current=9>1, change to 1, k=0. i=1: target=0, current=1>0, but no changes left => keep 1 => "11". But is "11" minimal? Could we instead not change i=0 and change i=1 to 0, resulting "90"? "11" vs "90": 11 < 90, so "11" is better. So greedy correct.
But what about S = "90", k=1. Greedy: i=0: target=1, current=9>1, change to 1 => "10". That uses k, i=1 unchanged 0 -> "10". Could we instead keep i=0=9 and change i=1 to 0? Already 0? Actually i=1 is 0, so no change needed, but that would be "90". "10" < "90". So greedy fine.
Now consider S = "2000", k=2. Greedy: i=0: target=1, current=2>1, change to 1, k=1. i=1: target=0, current=0, no change. i=2: target=0, current=0, no change. i=3: target=0, current=0, no change. Result "1000". Is there better? Could we instead change i=0 to 1 and also change i=1 to 0? But i=1 already 0. So result "1000". Could we change i=0 to 1 and something else? Not needed. "1000" is minimal.
But consider S = "2100", k=2. Greedy: i=0: change 2->1 (k=1). i=1: target 0, current 1>0, change to 0 (k=0). i=2: keep 0, i=3: keep 0 => "1000". That's minimal? Could we get "1100"? No, larger. Could we get "2000"? That's larger. So "1000" is indeed minimal.
Now, what about a scenario where we have more changes than needed? For instance, S = "555", k=3. Greedy: i=0: change 5->1 (k=2). i=1: change 5->0 (k=1). i=2: change 5->0 (k=0) => "100". That's minimal.
Now, is there any scenario where it's not optimal to change a digit to 0 because we might want to change it to something else to achieve even smaller number? Obviously 0 is the smallest digit. So no.
Thus greedy seems correct.
But wait, there's a nuance: The problem statement says "change at most k digits". That means we are allowed to change fewer than k. So we can stop when we have used k changes or when further changes wouldn't reduce the number. So greedy works.
But is there any scenario where we might want to change a digit to something other than 0 or 1, maybe to minimize later? For example, if we have S = "109", k=1. Greedy: i=0: target=1, current=1 no change. i=1: target=0, current=0 no change. i=2: target=0, current=9>0, change to 0 => "100". That's minimal. Could we do better by changing i=0 to something? Not allowed to 0. So no.
Consider S = "129", k=1. Greedy: i=0: target=1, current=1, no change. i=1: target=0, current=2>0, change to 0 => "109". That's "109". Could we instead change i=2 to 0? That would give "120". "109" < "120". So greedy is better.
Now, consider S = "199", k=1. Greedy: i=0: 1 unchanged. i=1: 9 -> 0 => "109". But what if we change i=2 to 0? That yields "190". "109" < "190". Good.
Now, consider S = "990", k=2. Greedy: i=0: 9->1 (k=1). i=1: 9->0 (k=0). i=2: 0 unchanged => "100". Could we do something else: change i=0->1 and i=2? i=2 is 0 already. So same. But what about not changing i=1 but changing i=0 and something else? If we change i=0->1 and i=1 keep 9 => "190". Worse. Or change i=0->1 and i=2->0 (but i=2 already 0) => same. So optimal.
Now, consider S = "900", k=1. Greedy: i=0: 9->1 (k=0) => "100". Could we instead change i=1 or i=2 to 0? Then we get "900" unchanged? Actually if we change i=1 from 0 to 0 no change; i=2 from 0 to 0 no change; so only possible change is i=0 to something smaller? If we change i=0 to 1, we get "100". If we change i=0 to 0 not allowed. So "100". Good.
But what about S = "1000", k=1. Greedy: i=0: 1 is target, no change. i=1: target 0, current 0 no change. i=2: 0, no. i=3: 0, no. So no change, result "1000". Could we change i=0 to 0? Not allowed. Could we change i=1 to something? Changing i=1 to 0 no change, to something else would increase number, not beneficial. So optimal.
Now, consider S = "200", k=1. Greedy: i=0: 2->1 => "100". That's minimal. Could we instead change i=1 or i=2 to 0? That gives "200" unchanged? Actually i=1 is 0, i=2 is 0, so no changes. So "200" > "100". So good.
Thus greedy seems correct.
But wait, there is one more nuance: The integer must have exactly n digits, and no leading zeroes. This means the first digit cannot be zero if n>1. But what about changing the first digit to 0 and then reducing n? That's not allowed; we must keep length n. So first digit cannot be zero. So our target for first digit is min(1, original) if we can change? Actually we want it as small as possible, which is 1 (unless n=1). So we set it to 1 if not already 1, given k>0.
But is there any case where it's better to leave first digit >1 even if we have k>0? No, because 1 is smaller. So we should always change it to 1 if possible. However, consider if we have k=0, obviously can't.
Now, one more nuance: The problem says "at most k digits". So we can change fewer. So we will change only those that reduce the digit to a smaller value. Changing to a larger digit would never help minimize.
Thus the greedy algorithm: For i from 0 to n-1:
if i == 0 and n > 1: target = 1 else target = 0.
if S[i] != target and remaining k > 0: set S[i] = target, decrement k.
Otherwise, keep S[i].
But is it always safe to set target = 0 for all i>0? Yes, because for positions after the first, we can have zero. That yields the smallest lexicographic string because 0 is smallest digit.
But consider the scenario where we might want to change the first digit to 1, but we only have exactly 1 change, and the first digit is not 1. That seems optimal.
Now, there is a potential pitfall: If the first digit is already 1, we don't change it. But what if we have k changes and we could change the first digit to 0? Not allowed. So fine.
But is there any scenario where we might consider not changing a later digit to 0, even though we have changes, because we might want to use that change on an earlier digit? We already process earlier first, so we use changes earlier. So if we still have changes after processing all positions, we could just not use them. That's okay.
Thus algorithm appears trivial. But is it correct for all? Let's try a trick: S = "10", k=1. Greedy: i=0: target 1, current 1, no change; i=1: target 0, current 0, no change. Result "10". Could there be a better result? Could we change i=0 to something else? If we change i=0 to 0? Not allowed. Change i=0 to 2? That would increase number, not better. Change i=1 to something smaller? 0 is already smallest. So "10" is minimal. Good.
S = "99", k=1. Greedy: i=0: change to 1 (k=0) => "19". Could we change i=1 to 0? Then "90". 19 < 90, so yes.
Now, what about S = "100000", k=1. Greedy: i=0: 1 ok; i=1: 0 ok; i=2: 0 ok; ... no changes. Could we make it smaller? The number is 100000. Could we change first digit to something? Only 1 is minimal. Could we change second digit to 0? It's already 0. So no.
Now, S = "200001", k=2. Greedy: i=0: 2->1 (k=1). i=1: target 0, current 0 -> no change. i=2: 0 no change. i=3: 0 no change. i=4: 0 no change. i=5: 1 -> target 0, current 1>0, k=1>0 -> change to 0 => "100000". That's "100000". Could there be a better? If we instead change i=0 to 1 and i=5 to 0? That's what we did. Could we change i=0 to 1 and i=1 to 0? Already 0. So "100000". Could we not change i=0 and instead change i=5 to 0 and i= something else? That would be "200000", which is larger than "100000". So yes.
Now, S = "500", k=2. Greedy: i=0: 5->1 (k=1). i=1: 0 unchanged. i=2: 0 unchanged. result "100". Could we instead do i=0->1 and i=1->0? Already 0. So fine.
Thus far works.
But wait, consider S = "10999", k=2. Greedy: i=0: 1 ok; i=1: 0 ok; i=2: 9 -> 0 (k=1); i=3: 9 -> 0 (k=0); i=4: 9 unchanged => "10009"? Actually digits: 1,0,0,0,9 => "10009". But is there a better configuration? Could we change i=2 to 0 and i=4 to 0? That would be "10090"? Wait: if we change i=2 to 0 and i=4 to 0: digits: 1,0,0,9,0 => "10090". Compare "10009" vs "10090": "10009" is smaller because at position 3 (0-indexed 3) we have 0 vs 9. So our greedy gave i=3 changed, i=4 unchanged, resulting "10009". But could we get "10000"? That would need 3 changes: i=2,i=3,i=4, but only k=2. So we have to decide which positions to set to 0 to minimize lexicographically. Greedy chooses earliest possible (leftmost) positions among those >0. That yields "10009". That is indeed minimal? Let's test all combinations with exactly 2 changes among positions 2,3,4 (positions with non-zero digits) - also maybe consider changing i=0 or i=1 but they are already minimal. So we need to pick two positions out of {2,3,4} to set to 0, keeping the rest as original (9). The strings possible:
set {2,3}: "10009"
set {2,4}: "10090"
set {3,4}: "10900"
Which is smallest lexicographically? Compare: after "10", at position 2: for {2,3}: 0; for {2,4}: 0; for {3,4}: 9. So {3,4} gives "10900" > others. Among {2,3} and {2,4}, at position 2 both 0, then at position 3: {2,3} has 0, {2,4} has 9. So "10009" < "10090". So indeed the smallest is to set the leftmost possible non-zero digits to 0. So greedy works.
Thus the problem reduces to: For each digit from left to right, if we can change it to the smallest allowed digit (1 for first if n>1, else 0) and we have remaining changes, do it.
But is there any scenario where we would want to change a digit to something other than 0 or 1 to achieve a better overall number? For example, suppose we have S = "90", k=1. Changing first to 1 yields "10". That's good. But what about S = "19", k=1, we changed second to 0 => "10". So far so good.
Now, consider S = "21", k=1. Greedy: i=0: 2->1 => "11". Could we instead change i=1 to 0 => "20"? 11 < 20, so good.
Now consider S = "200", k=1. Greedy: i=0: 2->1 => "100". Good.
Now consider S = "101", k=1. Greedy: i=0: 1 ok; i=1: 0 ok; i=2: 1 -> change to 0 => "100". That's minimal. Could we do something else? If we change i=0 to 1 is same, i=1 to 0 is same. So "100".
Now, what about the constraint "at most k digits"? So we might not use all changes if the string is already minimal (all digits minimal allowed). For example, S = "100", k=5 -> output "100".
Now, is there any scenario where we might want to change a digit to something higher to enable more changes? No, that would not help minimization.
Thus the solution seems extremely simple: iterate, set target, change if needed and budget.
But wait, the problem is from Codeforces? It might be "Changing a String" or similar? Actually this seems like Codeforces problem "Little Pony and Sort by Shift"? No.
Maybe it's more complex: Are we allowed to change digits to any digit, not just minimal? Could there be a scenario where we need to change a digit to a non-minimal value to avoid violating the "no leading zeroes"? For instance, if we change first digit to 0, that's invalid. So we must not do that. But our greedy ensures first digit is set to 1, which is valid.
But suppose first digit is 1 originally, we have k>0, and we consider changing it to 0? Not allowed, so we don't.
Thus greedy stands.
But is there any nuance about the fact that we can change at most k digits, but we are not forced to change exactly k? So we can stop early.
Hence the solution is O(n) and trivial.
But the problem statement says "large integer S" and n up to 200k, so a simple solution is expected.
I recall a Codeforces problem: "Ania and Minimizing" maybe? I think it's from Codeforces Round #1230 (Div. 2) problem A? Actually I recall a problem: "Ania and Minimizing" maybe similar. In that problem, you are allowed to change at most k digits, want minimal number without leading zeros. The solution is exactly: if k > 0 and n > 1, set first digit to '1' if not already; then for the rest, set to '0' as long as k remains. That's exactly what we described.
But I should double-check if there is a subtle edge case: if k=0, we just output S unchanged. Good.
If n=1, then we can change to 0 if k>=1 and original >0, else output original. That's covered by target=0 for i=0 when n=1.
Now, consider S = "10", k=1. Already minimal? Actually "10" is not minimal? Could we get "01"? Not allowed. Could we get "00"? Not allowed because leading zero. So "10" is minimal. Good.
Now, consider S = "100", k=1. Already minimal? Yes.
Now, consider S = "1010", k=2. Greedy: i=0: 1 ok; i=1: 0 ok; i=2: 1->0 (k=2->1); i=3: 0 ok? Actually S[3]=0, so no change; we still have 1 left? Wait after i=2 we have k=1. i=3: target 0, current 0, no change. So result "1000". Is that minimal? Could we get "1000"? That's indeed minimal? Could we get "1000" by changing i=2 to 0. But we have k=2, maybe we could also change i= something else? i=1 is already 0, i=0 is 1. So "1000" is the smallest? What about "1000" vs "1000" same. Could we get "1000"? That's the same. So fine.
But wait, could we get "1000" using only 1 change? That would be "1000" already. So with 2 changes we could also get "1000", which is same.
Now, what about S = "5000", k=1. Greedy: i=0: 5->1 => "1000". Good.
Now, consider S = "109", k=1. Greedy gives "100"? Wait: i=0:1; i=1:0; i=2:9->0 => "100". That's "100". But is "100" < "109"? Yes. Good.
Now, consider S = "101", k=1: "100". Good.
All good.
Thus solution: read n,k; read string S. If k==0, output S. Else, iterate i from 0 to n-1:
if i==0:
if n>1: target='1'
else: target='0' // because n==1, we can set to 0
else: target='0'
if S[i] != target and k>0:
S[i] = target
k -= 1
After loop, output S.
But wait: Is it always optimal to set first digit to '1'? What if n>1 and k>=1, but first digit is already '1'? Then no change. If first digit is '0'? But S doesn't have leading zeroes initially, so first digit cannot be '0' for n>1. So no.
Now, consider if k is large, we may change many digits to 0, but if we change first digit to 1 and then change many others to 0, the resulting number will be something like 1 followed by zeros, except the positions that we didn't change because k ran out. That seems right.
But is there a scenario where we might not want to change first digit to 1 because we need that change to set a later digit to 0, and that yields a smaller number than changing first to 1 and leaving later digit as is? For example, suppose S = "20", k=1. Changing first digit to 1 yields "10". Changing second digit to 0 yields "20". "10" < "20". So better to change first.
Suppose S = "29", k=1. First to 1 yields "19". Second to 0 yields "20". 19 < 20. So still first.
Suppose S = "90", k=1: "10" vs "90": "10" wins.
Suppose S = "99", k=1: "19" vs "90": "19" wins.
Thus always better to change first if it's >1.
But what about when first digit is 2 and k=1, and the rest digits are all 0? For instance S = "2000", k=1. Changing first to 1 yields "1000". Changing any later digit to 0 does nothing because they're already 0. So "1000". Could we get "2000" by doing nothing? "1000" is smaller. So first change is better.
What if S = "100...09", k=1, where first digit is already 1. Then we cannot improve first, so we change the first non-zero later digit to 0, which is best.
Thus the greedy is optimal.
But wait, there might be a scenario where the first digit is 1 but we have k>=2, and we could change the first digit to 0? Not allowed. So no.
Now, what about the scenario where we have n>1 and k=0, output original.
Now, what about scenario where we have n>1 and k>=1, and the first digit is '1', but the second digit is non-zero. Then we set second digit to '0' if possible. But could there be a scenario where we might want to change the first digit to something else to enable more zeros? For instance, if we change first digit to 0, we would have a leading zero, invalid. So not allowed.
Thus, I'm confident.
But wait, I've seen a similar problem: "Minimize the string" where you can change up to k digits, but you can also delete digits? No, not that.
Another nuance: The integer is large, but we can output as string. So all good.
Thus answer: implement as described.
But let's double-check the note: "A number has leading zeroes if it consists of at least two digits and its first digit is 0. For example, numbers 00, 00069 and 0101 have leading zeroes, while 0, 3000 and 1010 don't have leading zeroes." So single-digit 0 is allowed. So our logic handles n=1.
But wait: There is a case where n>1 and first digit is already '1', but we might be able to change it to '0' if we also add a leading non-zero? No, we cannot add digits. So illegal.
Thus solution is complete.
But I'd like to think about potential pitfalls: if we have n>1 and k>0 but first digit is '0'? Not possible by input constraints.
Case: n=5, k=4, S=98765 -> i=0:9->1 (3); i=1:8->0(2); i=2:7->0(1); i=3:6->0(0); i=4:5 unchanged => "10005"? Actually after changes: positions:0=1,1=0,2=0,3=0,4=5 => "10005". That's minimal? Could we get "10000" by not changing i=1 but changing i=4? That would be: 1,8,7,6,0 => "18760" which is larger. Or change i=0 and i=4: 1,8,7,6,0 => "18760". Or change i=0,i=1,i=2,i=4: 1,0,0,6,0 => "10060"? Compare "10005" vs "10060": "10005" is smaller because at index 3, we have 0 vs 6? Actually "10005": digits: 1,0,0,0,5; "10060": 1,0,0,6,0. At index 3, 0 < 6, so "10005" is smaller. So greedy is indeed better. Could there be a combination "10000" if we changed i=0,i=1,i=3,i=4? That would be 1,0,7,0,0 => "10070"? Actually 1,0,7,0,0 = "10700"? Wait compute: index0=1,1=0,2=7,3=0,4=0 => "10700". That's larger. So greedy chooses leftmost positions for zeros, which yields the lexicographically smallest.
But is it always best to zero the leftmost non-zero digits? Yes, because we want to minimize the string lexicographically: we want as many leading digits as possible to be small. So the optimal set of positions to change to 0 is exactly the leftmost m positions where the digit is not already 0 (or the smallest possible), where m is number of changes we can allocate. However, we also have the first digit constraint: if the first digit is >1 and we have at least one change, we should change it to 1 rather than 0. That's consistent: for first digit, the smallest allowed is 1, so we set to 1 if >1. That's essentially making it as small as possible (1) rather than 0.
Thus algorithm can be seen as: For i=0..n-1, define desired digit: 1 for i=0 (if n>1) else 0; for i>0 desired=0. Then we change the digit if it's larger than desired and we have changes left. Since any change that sets digit to desired reduces it, and setting to a larger digit would increase it, we only consider reducing. And because we process left to right, we use changes to reduce the earliest digits as much as possible.
Thus solution is correct.
Edge case: n>1, k>0, S[0] = '0'? Not allowed in input, but after changes we might accidentally set it to 0? Our code sets target=1 for i=0 if n>1, so we won't set to 0. Good.
Now, what about if k>0 and n>1 and S[0]='1'? No change. So we keep 1.
Now, if n=1, target='0', we may change to 0 if it's not already 0. So that yields minimal integer 0.
But what about n=1, S='0', k arbitrary? Then S[0]==target, no change, output 0. That's minimal.
Now, what about n=1, S='5', k=0 -> output 5. Minimal? Could we get 5? Yes.
Thus all fine.
Thus final solution is trivial.
But wait, I recall a Codeforces problem "A. Minimum Integer" or something similar? There might be a twist: the integer must not contain leading zeroes, but after changing digits, we cannot create leading zeroes. However, consider a scenario where the first digit is 1, and we have k=1. If we change the first digit to 0, we would have leading zero, which is not allowed. So we cannot. So our code doesn't do that.
Now, some might think that we could potentially change first digit to 0 if we also change another digit to non-zero so that the first non-zero digit appears later? But that still results in leading zeros; the number is considered to have leading zeros because its first digit is 0 and length >1. For example, "01" is considered to have leading zero because it consists of at least two digits and first digit is 0. So it's invalid. So we cannot have any zero at position 0 when n>1.
Thus our approach is safe.
Now, check the note: "numbers 00, 00069 and 0101 have leading zeroes, while 0, 3000 and 1010 don't have leading zeroes." So "0" is okay; "0101" is not okay. So indeed we cannot have first digit zero if length>1.
Thus solved.
One more test: n=2, k=1, S="10". Greedy output "10". Could we get "01"? Not allowed. So minimal is "10". But is "10" minimal? Could we get "00"? Not allowed. So yes.
n=2, k=1, S="20". Greedy: change first to 1 => "10". That's minimal.
n=2, k=1, S="02"? Not allowed input because leading zero.
n=2, k=2, S="99": i=0: 9->1 (k=1); i=1: 9->0 (k=0) => "10". Could we get "01"? Not allowed. So "10" is minimal.
Now, check n=2, k=2, S="19": i=0: 1 ok; i=1: 9->0 => "10". Could we get "09"? Not allowed. So "10".
Now, n=2, k=2, S="10": no changes needed => "10". Could we get "00"? Not allowed. So "10".
Thus all fine.
Thus solution is correct.
Implementation details: reading input with spaces, using string.
Potential pitfalls: if k is large, we may try to change digits that are already target, but we only change if different and k>0. So no waste.
Thus final answer.
But wait: There is a nuance: If k>0, we are allowed to change at most k digits. In our greedy, we might change a digit to target even if it's already target? No, we check s[i] != target. So we won't waste changes.
Edge case: What if n>1, first digit is 1, and we have k>0. Could we change first digit to 0 and then change another digit to something else to compensate? But that would still be invalid because first digit is 0. Even if we change second digit to non-zero, the number still starts with 0, so invalid. So no.
But what if we change first digit to 0 and also change the number of digits? Not allowed.
Thus no.
Now, maybe there is an alternative: If n>1 and we have many changes, we could set the entire number to 0 except first digit? But first digit cannot be 0. So smallest possible number is 100...0 (with n-1 zeros). That's the absolute minimal integer with n digits and no leading zeros. So if k is large enough, we can achieve that. Our algorithm would produce that if k >= (number of digits not already minimal). Specifically, we need changes: if first digit >1, one change to set to 1; for each of the remaining positions, if digit !=0, need change to 0. If k is sufficient to cover all those, we get "100...0". If not, we get a number that is as close to that as possible from the left.
Thus solution stands.
But wait, is there any scenario where we wouldn't want to change a digit that is >0 to 0 because we might want to change it to something else to achieve a smaller overall number? No, 0 is minimal.
Thus solution is simple.
But hold on: I recall a Codeforces problem "Minimize the Integer" where you could swap adjacent digits? Not that.
Now, I think this problem is from Codeforces Round #466 (Div. 2) problem B? Actually problem "A. Minimum number of steps"? Not sure.
Anyway, I'm confident.
But we should consider if there is any scenario where we might want to change a digit to something larger than 0 to get a smaller number later? That doesn't make sense.
Thus final answer: implement as described.
Now, write solution in C++.
Potential tricky part: Input reading; n and k are int; S as string.
Pseudo:
int main(){
int n, k;
string s;
cin >> n >> k >> s;
if (k > 0) {
for (int i = 0; i < n; ++i) {
char target;
if (i == 0) {
if (n == 1) target = '0';
else target = '1';
} else {
target = '0';
}
if (s[i] != target && k > 0) {
s[i] = target;
--k;
}
}
}
cout << s << endl;
return 0;
}
But is there any case where we might want to change first digit to 0 when n=1? Already handled.
Now, think about performance: O(n) fine.
But is there a need to consider that we might change first digit to 1 only if it's >1? Yes, we check s[i] != target, so if it's 1 we don't change.
Now, test with n=3, k=2, S=102 -> as earlier yields 100.
Now, test with n=3, k=1, S=102 -> i=0: 1 ok; i=1:0 ok; i=2:2->0 if k=1 => 100? Actually we have k=1, so change i=2 to 0 => 100. But is that allowed? Yes, we have 1 change. So output 100. That's smaller than 102. So that is correct.
Now, what about n=3, k=1, S=100 -> i=0:1 ok; i=1:0 ok; i=2:0 ok; no change => 100. That's fine.
Now, n=3, k=1, S=200 -> i=0:2->1 => 100. So 100.
Now, n=3, k=1, S=110 -> i=0:1 ok; i=1:1>0? target for i=1 is 0, so we can change to 0 if k>0 => 100? Actually i=1 change to 0 => s becomes "100", and then k=0, i=2 original 0 unchanged => "100". That's minimal? Could we get "110"? No. So yes.
But note: after changing i=1 to 0, the number becomes "100". That's indeed smaller than "110". So correct.
Now, what about n=3, k=1, S=101 -> i=0:1 ok; i=1:0 ok; i=2:1->0 => 100. Good.
Now, n=3, k=2, S=101 -> i=0:1 ok; i=1:0 ok; i=2:1->0 (k=1) => 100. Then still k=1 left, but no beneficial changes (all digits already minimal). So output 100. That's fine.
Thus everything works.
But wait, there is a subtlety: The problem says "change at most k digits". We are allowed to change fewer. So using fewer than k is fine.
Now, is there any scenario where we would want to change a digit to something other than the target to minimize the number further? For example, maybe if we cannot change a later digit to 0 because we don't have enough changes, we might consider changing an earlier digit to a value that is not the minimal allowed but still less than original, to free up a change? But changes are independent; we cannot "free up" a change; we have a budget of changes, each change can be applied to any digit. So if we have a limited budget, we allocate it to digits to reduce as much as possible. Since reducing an earlier digit to a lower value is always beneficial, we should allocate budget to that digit to reduce it to the minimal possible (target). But what if the minimal target for that digit is 1 (if first) or 0 (if later). Could there be a scenario where reducing it to a value that is not the absolute minimal (like reducing from 9 to 2) might be better because it uses less changes? But each change costs 1 regardless of magnitude. Changing from 9 to 0 costs 1 change; changing from 9 to 2 also costs 1 change. So if we have a budget of 1, we can change a digit to any digit. So we should always choose the smallest allowed digit (target) because that gives the greatest reduction. There's no cost difference. So it's optimal.
But what about the case where we have multiple changes? Could there be an interaction? For instance, suppose we have two changes, and we could reduce first digit from 5 to 1 (cost 1) and then later reduce a digit from 9 to 0 (cost 1) -> total 2 changes. That's our greedy. If we instead reduced first digit from 5 to 0 (not allowed) or to 2? That would be worse.
Thus greedy stands.
Now, but let's double-check the scenario where the first digit is 2, and we have k=1. Greedy changes it to 1, yielding e.g. "1xxxx". Could we get a smaller number by not changing the first digit but instead changing the second digit to 0? That would give "20xxx" vs "1xxxx". Since the first digit is 1 vs 2, the 1-digit number is always smaller regardless of the rest. So yes, change first.
Now, what if the first digit is 1, we have k=1, and second digit is 9. Greedy changes second to 0 -> "10xxx". Could we instead change first digit to 0? Not allowed. So fine.
Now, is there any scenario where we might want to change first digit to something else to allow more zeros? For example, if we change first digit to 0, we would invalidate. So no.
Thus solution is trivial.
Now, but I've seen a problem called "Ania and Minimizing" which has a twist: you are allowed to change at most k digits, but you also need to output the minimal possible value after changes, and the integer might be very large, but you output it as a string. That's exactly this.
Wait, I recall that in some version, the integer might be represented with leading zeros if you change first digit to zero? But it's disallowed.
Thus the solution is accepted.
Now, I'll provide final answer with explanation.
But let's think about a more formal proof: The lexicographic order of strings of same length with no leading zeroes corresponds to numeric order. To minimize, we want the leftmost digit as small as possible, subject to constraints. The smallest possible digit at position 0 is 1 if n>1 else 0. If we can change it to that (cost 1 if not already), we do. After fixing the first digit (or deciding not to change because it's already minimal or insufficient budget), we proceed to position 1: The smallest allowed digit is 0. If we have remaining changes and the current digit is not 0, we change it to 0. This is optimal because any other use of a change (e.g., on a later position) would result in a larger string at this position, making the whole number larger. Since we can only use at most k changes, using them on earlier positions yields the lexicographically smallest result. This greedy is optimal.
Thus code as above.
But wait, what about the scenario where n>1 and k=0? Then we output original S. If S has leading zero? Not possible.
Now, what about the scenario where n=200000, k=200000, and S all '9's? Our loop will change each digit: first to '1', others to '0'. That's fine.
Now, implement.
One nuance: In C++, we can modify characters in string directly.
Now, let's test with some random cases programmatically mentally.
Case: n=4, k=2, S="1234". Greedy: i=0: 1 already; i=1: target 0, current 2 -> change to 0 (k=1); i=2: target 0, current 3 -> change to 0 (k=0); i=3: keep 4 => "1004". Is that minimal? Could we instead change i=0 to 1 (no change needed) and change i=3 to 0? That yields "1230". Compare "1004" vs "1230": at i=1, 0 vs 2 => "1004" smaller. So good. Could we change i=1 to 0 and i=0? i=0 is already 1. So "1004". Could we change i=2 to 0 and i=3 to 0? That would require 2 changes on i=2 and i=3, but i=1 remains 2 => "1200". That's larger. So greedy best.
Case: n=4, k=2, S="1034". Greedy: i=0:1; i=1:0; i=2:3->0 (k=1); i=3:4->0 (k=0) => "1000"? Wait i=3 after i=2 we have k=1, then at i=3 we change 4->0 (k=0). So result "1000". That's minimal. Could there be "1000"? Yes.
Case: n=4, k=1, S="1034": i=0:1; i=1:0; i=2:3->0 (k=0); i=3:4 unchanged => "1004". Could we instead change i=3 to 0? That yields "1030". Compare "1004" vs "1030": at i=2, 0 vs 3 => "1004" smaller. So good.
Now, just to be thorough, consider scenario where first digit is 2, k=1, and all other digits are 0 except one later digit is 9. For example, S="2009", k=1. Greedy: i=0:2->1 => "1009". Could we instead change the 9 to 0 to get "2000"? 1009 < 2000, so greedy better.
Thus fine.
Now, what about if the first digit is 1, k=1, and the second digit is 0, third digit is 5, fourth is 0, ...? Greedy changes the leftmost non-zero after first to 0. That's best.
Now, I think we have solved.
But before finalizing, I should search for any hidden nuance: The integer S is large, and we output it as a string. The problem statement: "Ania has a large integer S. Its decimal representation has length n and doesn't contain any leading zeroes." So S is a positive integer? Not necessarily, but length n and no leading zeros means it's a positive integer if n>=1 except possibly 0 when n=1? Because "0" is allowed, but if n=1, S could be "0"? But it says "doesn't contain any leading zeroes", and for n=1, "0" does not have leading zeroes. So possible. But we can treat it as string.
Now, is there any situation where we might need to consider that after changes, the integer might become zero? That's only if n=1 and we change to 0. That's allowed.
Now, what about n>1 and we have changes that could make all digits zero except first maybe 0? Not allowed. So we never output a string starting with 0 when n>1. So our algorithm ensures that.
Now, a possible bug: In the loop, if we change first digit to '1' when n>1, that's fine. But what if the first digit is already '1' and we have k>0, we don't change it. Good.
But consider the scenario where n>1 and first digit is '0' originally? Not allowed by input.
Now, think about scenario: n=2, k=2, S="10". Greedy: i=0:1 ok; i=1:0 ok; no changes. Output "10". But could we change i=0 to something else? Changing i=0 to 0 would make leading zero, invalid. Changing i=0 to something >1 would increase number. Changing i=1 to something >0 would increase number. So "10" is minimal.
Now, scenario: n=2, k=2, S="20". Greedy: i=0:2->1 (k=1); i=1:0 ok; output "10". Could we change i=0 to 1 and i=1 to 0? i=1 already 0. So fine.
Now, scenario: n=2, k=2, S="11". Greedy: i=0:1; i=1:1->0 (k=1) => "10". Could we change i=0 to 1? already. So "10". Could we change i=0 to 0? not allowed. So "10". Good.
Now, scenario: n=2, k=2, S="01"? Not allowed input.
Thus all good.
Now, but what about a scenario where we have k=0, and S contains leading zeros? Not allowed input.
Now, I think solution is done.
But I recall a Codeforces problem "A. Minimizing the String" where you delete one character. Not that.
One more nuance: In the problem statement, it says: "change at most k digits of S. She wants to do it in such a way that S still won't contain any leading zeroes and it'll be minimal possible." So we cannot introduce leading zeroes. But note that if n=1, the number 0 is allowed.
Now, check if there's any possibility that we could reduce the number of digits? No, we cannot change length.
Thus final answer.
Now, let's write final solution with comments.
Potential edge: Input reading: first line contains n and k, second line contains S. So we use cin.
But ensure we handle large input: use ios_base::sync_with_stdio(false); cin.tie(0); maybe.
Now, code.
One more test: n=200000, k=200000, S all '9's. Output: first char '1', rest '0's. That's correct.
Now, what about S = "1" repeated? Already minimal.
Thus solution ready.
Now, I'll produce final answer.
But wait, I recall that there is a known Codeforces problem "A. Ania and Minimizing" with a twist: if k==0, just output S. But there is also a scenario where k>0 and n>1 and the first digit is already 1, but we might consider not changing the first digit, but we might want to change it to 0 if we can also change another digit to non-zero? But that's not allowed because leading zero. So no.
However, there is an official solution for that problem: For i=0: if n>1 and k>0 and s[0]!='1', set s[0]='1', k--. Then for i=1 to n-1: if s[i]!='0' and k>0, set s[i]='0', k--. Output s. That's exactly our algorithm.
I think it's correct.
Thus final answer. ```cpp
#include
#include
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
code
int n, k;
string s;
cin >> n >> k >> s;
if (k > 0) {
for (int i = 0; i < n; ++i) {
char target;
if (i == 0) {
// first digit: at least 1 if more than one digit, else 0
target = (n == 1) ? '0' : '1';
} else {
target = '0';
}
if (s[i] != target && k > 0) {
s[i] = target;
--k;
}
}
}
cout << s << '\n';
return 0;