We need to understand the problem: given a string a of digits 1-9, a mapping f from digits to digits (1-9). We can perform at most one operation: choose a non-empty contiguous subsegment and replace each digit x in that segment with f(x). We want to maximize the resulting number (as a string) after at most one such operation. Since the number is long (string), lexicographically larger is larger.
We can apply the operation zero times, i.e., leave original, or exactly once.
We need to choose the subsegment to maximize the final string.
Observation: Since we can apply operation to any contiguous segment, the effect is that we replace digits in that segment with their f-mapping. The mapping can be identity or not.
Goal: maximize lexicographically. So we want the earliest possible digit to become larger, and once we have increased a digit, we might want to also apply to later digits if it can increase them further? But careful: We are applying the same operation to the entire chosen segment: we must decide the segment before seeing results? Actually the operation is: choose a segment, then replace each digit x with f(x). So we apply f to all digits in that segment. For digits outside, they remain original.
Thus the final string is: a[0..L-1] unchanged, then for i in [l..r], f(a[i]), then a[r+1..n-1] unchanged.
We can choose any l <= r.
We want the final string to be as large as possible.
Since we can only apply the operation once, and we cannot do it partially within segment; we must apply to all digits in segment.
We need to find the optimal l and r.
n up to 2e5.
We can think of it as: we can try to find the earliest position where applying f would increase the digit, and then maybe extend the segment as long as f(d) > d? But careful: f(d) could be less than d, or equal. So we want to avoid decreasing digits. But we might accept a decrease later if earlier digits become larger? No, because the segment is contiguous; if we include later positions that would decrease, it could make the number smaller after that point. However, since we are maximizing lexicographically, if we make an earlier digit larger, the rest of the string after it doesn't matter as much? Actually lexicographic order: Compare strings from leftmost digit. The first index where they differ determines ordering. So if we can increase a digit at some position, that dominates any changes to later positions (even if later digits become smaller). But careful: The operation applies to a segment. If we include positions before the first increased digit, those would be changed too; if they become smaller, that could hurt. So the optimal is likely: we start the segment at the first position where applying f gives a strictly larger digit? Or maybe we want to start at a position where we can get a larger digit, but we need to ensure that all positions before that remain unchanged (i.e., we start at that position). And for positions after that, we may want to apply as long as f(d) >= d? Actually, after the first increased digit, the lexicographic comparison at that index already makes the number larger than original (if we increased). Then the rest of the string can be anything; even if some later digits become smaller, the number is already larger because at the first differing position, our digit is larger. However, we also have to consider the scenario where we cannot increase any digit at any position; but maybe we can increase a later digit while keeping earlier digits unchanged? But that would make the number lexicographically larger only if all earlier digits are equal (they are unchanged) and at the first changed digit, our digit is larger than original. So starting at any position where f(d) > d, and ensuring that for all positions before that we do not change them (so l = that position). Then for the rest of the string, we could apply the operation to all following digits if we want? But if we apply to digits after that, it's okay because after the first differing digit, it doesn't matter if they become smaller or larger, as long as we don't cause the first differing digit to be smaller. However, we might also want to extend the segment backwards? No, because if we include earlier positions, they may become smaller or stay same. If they become smaller, that could cause the first differing digit to be at an earlier index (if we change an earlier digit and it becomes smaller, then the number would be lexicographically smaller than original at that earlier index). But if we include earlier positions and they become equal to original (if f(d)=d), that's fine. If they become larger, that's even better (then the first differing digit might be even earlier and larger). So we need to consider all possibilities.
Thus the problem reduces to selecting a contiguous segment such that the resulting string is maximized.
Since n is large, we need O(n) or O(n log n). We can treat this as a kind of "apply transformation to a subarray to maximize lexicographic order".
Let's think in terms of string comparison.
Let original string s (0-indexed). Let transformed string t(l,r) where for i in [l..r], t[i] = f(s[i]), else s[i].
We need to maximize t among all choices (including empty segment meaning no operation, which yields s).
Define g(d) = f(d) - maybe we compare digits.
We can think of this as: we have a potential to apply f to each digit individually, but only contiguously.
Observation: For lexicographic max, we want the earliest possible index i where t[i] > s[i], and for all j < i, t[j] = s[j] (i.e., we didn't change them or changed them to same). If we can achieve that, then the string is larger than s. Among all such, we want to maximize the whole string, i.e., after that index, we want the rest as large as possible, but the earliest index where it becomes larger is the primary key.
If there is no way to get t[i] > s[i] for any i without causing some earlier j to have t[j] < s[j], then the best we can do is to not change at all, or maybe we can get a string equal to s? But if we can't make any digit larger without making an earlier digit smaller, then the maximum is s (since any change would make it lexicographically smaller or equal). However, there might be cases where we can change a digit to same (f(d)=d) and that doesn't affect; but that's same as no change. So answer would be s.
But is it possible that we can make an earlier digit larger? That would be even better. So we want to maximize the earliest index where t differs from s, and at that index have larger digit.
Thus we need to find the leftmost index that we can "improve" without harming earlier indices.
If we set l = some index, and apply transformation from l to some r, then for any i < l, t[i] = s[i] (unchanged). So at index l, we have t[l] = f(s[l]). If f(s[l]) > s[l], then we have an improvement at position l, and earlier positions are unchanged, so the resulting string is lexicographically larger than s (since at first differing index l, we have larger). Good.
If f(s[l]) < s[l], then we have a decrease at l, making the string smaller than s (provided no earlier differences). So we would not choose such l as start unless we also change some earlier digit to increase? But earlier digits are unchanged, so it would be smaller.
If f(s[l]) = s[l], then the first difference may be later. So we might need to look beyond.
So for a segment starting at l, the first index where t differs from s is the first i >= l such that f(s[i]) != s[i]. If that i has f(s[i]) > s[i], then t > s. If that i has f(s[i]) < s[i], then t < s (provided no earlier differences). So we want to start l at a position such that the first change is an increase, not a decrease.
But we could also consider that we might include some preceding indices where f(s[i]) = s[i]; that's fine, they don't cause difference. So we could start earlier even if f(s[i]) = s[i] for those i, because they don't change. However, if we start earlier at a position where f(s[i]) < s[i], that would cause a decrease at that earlier index, which would be bad. So we must avoid that.
Thus an optimal segment could start at some l where either f(s[l]) > s[l] (immediate improvement) or f(s[l]) = s[l] (neutral) and later we get an improvement before any decrease.
But if we start at a neutral position and then later we encounter a decrease before any increase, that would be bad because the first difference would be a decrease. So we need the first index i in [l, r] where f(s[i]) != s[i] to have f(s[i]) > s[i].
Therefore, to get a string larger than s, we need to choose a segment such that the first non-identity change within the segment is an increase.
Moreover, once we have that increase, all subsequent positions (within and after the segment) can be whatever; the string is already larger than s. However, we also want to maximize the string among all such choices; i.e., after we have an improvement at some earliest position, we then want the rest of the string to be as large as possible.
But there is nuance: Suppose we have two choices both causing the first difference at index i, but one yields a larger digit at i (if multiple ways? Actually, at index i, f(s[i]) is fixed; we cannot change it except by not applying transformation at i (if we don't include i in segment). So if we want the first difference at i to be an increase, we must include i in the segment, and f(s[i]) > s[i]. There's no alternative for that digit. So the digit at the first differing index is fixed as f(s[i]) if we include i, otherwise it's s[i]. So for a given i where f(s[i]) > s[i], we can achieve a string where the first difference is at i with a larger digit by starting the segment at any l ≤ i such that for all j from l to i-1, f(s[j]) = s[j] (so they don't cause earlier difference). And also we must ensure that we don't include any j < l? Actually, l is start, so for any j in [l, i-1], if f(s[j]) != s[j], they would cause a difference earlier. So for the first difference to be at i, we need that for all j in [l, i-1], f(s[j]) = s[j]. If that holds, then the first difference is at i. If some of those have f(s[j]) > s[j], that would be even better because then the first difference would be at an earlier index with an increase. So it's beneficial to include earlier increases if possible. So the best is to start the segment at the earliest possible index where we can get an increase and not cause a decrease earlier.
Thus the problem reduces to: We can choose a contiguous segment to apply f. The effect is that each digit becomes f(d). We want the resulting string to be lexicographically maximum.
We can approach this by scanning left to right, trying to decide where to start and stop.
Another perspective: The operation is applying a mapping to a subarray. This is similar to the "apply function to subarray to maximize lexicographic" type. A known solution: Since we can apply at most once, we can try to find the longest prefix where f(d) == d, then perhaps start when f(d) > d, and continue as long as f(d) >= d? Actually typical approach for such problem (I recall a Codeforces problem "Replace Digits" or "Maximum number after one operation") goes like: Find the first position where f(s[i]) > s[i], then start applying from that position, and continue applying while f(s[i]) >= s[i] (i.e., either > or =) because if you stop at a point where f(s[i]) < s[i], that would cause a decrease at that position, but since the first difference already occurred earlier, a later decrease might not harm lexicographically? Actually, once you have an earlier increase, the rest of the string can be anything; but if you stop the segment earlier, then after the segment you go back to original digits. That could be smaller than if you had continued applying f? But since lexicographic order is already determined by the first increase, any difference after that only matters when comparing two strings that are identical up to that point. So among all strings that have the same prefix up to (i-1) and then at i have the same increased digit, we want the rest as large as possible. So we need to compare possible choices for segment that yield the same prefix until the first increase? Not exactly; they might yield different first increase positions.
Thus, we need to consider all possible segments and pick the maximum resulting string.
We can think in terms of comparing two candidate strings t1 and t2. Since n is up to 2e5, we could try to find the optimal via greedy.
Let me attempt a greedy algorithm:
- Start from the leftmost index. We'll decide to apply transformation from some index l onwards. While scanning, if f(s[i]) > s[i], we might want to start applying at i because it improves the digit. But maybe we could start earlier if f(s[i-1]) == s[i-1] (neutral) to also include that digit without harm, but that doesn't change anything because it's neutral, so starting at i-1 or i yields same string if f(s[i-1]) = s[i-1]; but it might allow us to include later digits that we might want to transform if they become larger? Actually, if we start later, we may not transform some digits that we could transform to make them larger. Since after the first increase, we want the rest as large as possible. So we might want to transform as many digits as possible after the first increase, as long as transforming them doesn't make them smaller. But careful: If we transform a digit after the first increase and f(d) < d, then that digit becomes smaller than original. However, if we leave it unchanged, it remains original. Which is larger? Since we already have a larger digit at an earlier position, the rest doesn't affect lexicographic comparison with the original s, but it does affect comparison with other candidate strings that also have the same first increase index but different later digits. For maximizing the final string globally, we want the entire string to be as large as possible in lexicographic order. That means after the point of first difference, we want the string to be maximized. So if we have two different segments that both cause the first increase at the same position i, we compare the suffixes. So we need to pick the segment that yields the largest suffix.
Thus, for a given first increase index i (the earliest index where we get a digit larger than original), we want to maximize the suffix from i onward. The suffix from i is composed of: For positions j < i, they are same as original (since unchanged). At i, we have f(s[i]) which is fixed > s[i]. For positions j > i, we can decide whether to apply f to j or not, but the application must be contiguous from l to r. So if we start at l ≤ i, we apply from l to some r. That means for j > i, if j <= r, we apply f; if j > r, we don't. So the suffix after i is: for j from i+1 to r, f(s[j]); for j > r, s[j].
So we have a decision on r (the end of the segment). r must be >= i. So we can choose to apply f to some suffix after i, but we must stop at some point r, after which we revert to original. So the suffix after i is: (f(s[i+1]),..., f(s[r]), s[r+1], s[r+2],...). We want this to be as large as possible lexicographically.
Given that f(s[i]) is fixed, we want the remainder string from i+1 to end to be as large as possible. Among all possible choices of r (r >= i), we compare the strings: let T(r) be the string from i+1 onward as described. We want the maximum T(r) (and also consider possibility of no operation, which yields original suffix). But also l could be earlier than i, but as argued, if we include earlier neutral digits, they don't affect the suffix from i. If we include earlier digits where f(s[j]) > s[j], then the first increase would be earlier, which is even better. So the best overall string likely has the first increase as early as possible. So we should prioritize the leftmost index where we can get a digit increase.
However, consider scenario: we have an early index i where f(s[i]) > s[i], but if we start at i, then we could extend r to include many digits where f(d) >= d (so they become larger or equal) but maybe there is a later index j where f(s[j]) > s[j] but f(s[i]) <= s[i]? That is not relevant. But maybe by starting later at some index j, we could get a larger overall string because the suffix after j might be much larger than after i. But since i is earlier, any string that first differs at i (with a larger digit) is lexicographically larger than any string that first differs at a later index, regardless of suffix. Because at index i, the first string has digit > original, while the second string has same digit as original at i (since unchanged), so the first string is larger. So indeed, if we can get an increase at index i, then that string beats any string that doesn't have an increase until later. Therefore, the optimal must have the earliest possible first increase index (i.e., minimal index where we can achieve an increase) among all feasible segments. But is it always possible to achieve an increase at the earliest index where it's possible? Let's check: The earliest index i such that there exists a segment that yields a larger digit at i (with no earlier decrease) is simply the earliest index i where f(s[i]) > s[i] and we can start at some l ≤ i such that for all j in [l, i-1], f(s[j]) = s[j] (so they are neutral). But if the earliest index where f(s[i]) > s[i] is i0, can we always construct a segment that yields an increase at i0 without causing earlier decrease? We can set l = i0. That ensures earlier indices unchanged, and at i0 we apply f, getting increase. So yes, we can always get increase at i0. So the optimal string must have first increase at the earliest possible i where f(s[i]) > s[i] (if such exists). If there is no such index, then we cannot get any increase, and the best is original s (or maybe we can get a string equal to s but with some changes that are neutral? That's same as s). But what about the case where at some early index, f(s[i]) < s[i] but we could skip it by starting later, but maybe we could also start earlier if we include some index before that where f(s[j]) > s[j]? That would give earlier increase, which is better. So indeed the earliest index with f(s[i]) > s[i] is key.
But is it always optimal to start exactly at i0? What if we start earlier at l < i0 but f(s[l]) = s[l] (neutral), then first increase still at i0, same effect. Starting earlier doesn't change anything, unless there is some index between l and i0-1 where f(s[k]) > s[k]? That would be even earlier increase. So if there is any index < i0 with f(s[k]) > s[k], then i0 wouldn't be the earliest. So by definition, i0 is earliest index with f(s[i]) > s[i]. So no earlier index with increase. So any earlier start would only include indices where f(s) = s (neutral) or f(s) < s (decrease). If we include a decrease before i0, that would cause first difference to be a decrease, making string smaller. So we cannot include a decrease. So the only safe earlier start is if all indices from l to i0-1 are neutral (f(s)=s). That doesn't harm, but doesn't help either; the resulting string remains same as starting at i0. So we can just start at i0.
Thus, the optimal segment will start at the leftmost index i where f(s[i]) > s[i] (if any), and then we need to decide how far to extend it (r). But wait, what if there is no index with f(s[i]) > s[i]? Then we cannot get any increase. The maximum is s (or maybe we can get a string equal to s? Or maybe we could apply operation and still get same as s if all f(d)=d? That's same). So answer = s.
But is there scenario where we can get an increase at an index i even if f(s[i]) <= s[i]? No, because to get a larger digit at i, we must apply f to i and f(s[i]) > s[i]; otherwise it's not larger.
Thus, if there exists any digit d such that f(d) > d, then we can get a lexicographically larger number. So the problem reduces to: find the leftmost i where f(s[i]) > s[i]; start the segment at i; then decide r (>= i) to maximize the resulting string. Since after i, the string is already larger than original, we can extend the segment as long as doing so yields a string that is lexicographically larger than stopping earlier. But we want the maximum overall string, so we need to choose the r that maximizes the string. Since the first part up to i is fixed (original unchanged before i, then f(s[i]) at i). For positions j > i, we have a choice: if we include j in the segment, we get f(s[j]); if we exclude (i.e., stop before j), we get s[j]. We want the maximum possible suffix after i.
Now, we must decide r such that the resulting string is maximal. Since we can only have one contiguous segment, we either apply f to a range [i, r] and then stop. So the string from i+1 onward is: for indices i+1..r: f(s[k]), for indices > r: s[k].
We want to choose r (i <= r <= n-1) to maximize the string t (the whole string). Note that we could also consider r = i (only one digit transformed). So we need to compare the strings produced by different r.
But is it always optimal to extend as long as f(s[j]) >= s[j]? Let's analyze.
Suppose after i, we have a position j > i. If we include j (i.e., r >= j), then digit at j becomes f(s[j]); else it remains s[j]. We want the final string to be as large as possible. Since the string up to j-1 (including i and possibly earlier) is identical for all choices as long as we have not yet decided to stop before j. However, the decision at j influences the first position where the strings for different r differ.
Consider two choices: r = j-1 (stop before j) and r = j (include j). Both strings are identical up to index j-1. At index j, the first string has s[j], the second has f(s[j]). So which is larger? If f(s[j]) > s[j], then including j yields larger string; if f(s[j]) < s[j], then excluding j yields larger; if equal, then they are same at j, and we need to look further. But note that if we include j, then we also affect later indices (j+1 etc.) depending on whether we stop later or not. But we are comparing specific r values: r=j-1 vs r=j. However, if we include j, we might also include more later digits. So we need to consider the optimal r globally, not just pairwise.
We can treat the decision as: we transform a prefix of the suffix? Actually, the segment is contiguous starting at i, so once we stop transforming at r, we do not transform beyond. So we can think: we want to find the earliest position after i where it is beneficial to stop? Or the latest? Because we want the maximum string. This is reminiscent of choosing a cutoff: we transform from i to some r, and beyond r we keep original. The resulting string can be compared lexicographically. As we increase r, the string changes at positions where we switch from transformed to original.
Define for each candidate r, the resulting string T_r. To find the maximum among T_r, we can compare T_r and T_{r'} for r < r'. They will be identical up to index r (since both have transformation applied up to r? Wait: if r < r', then for indices <= r, both have transformation applied (since both include those indices). For indices > r but <= r', T_r has original digits, while T_{r'} has transformed digits. So they differ at the first index > r where r' continues transforming. That index is r+1. At index r+1, T_r has s[r+1], T_{r'} has f(s[r+1]). So if f(s[r+1]) > s[r+1], then T_{r'} > T_r; if f(s[r+1]) < s[r+1], then T_r > T_{r'}; if equal, then we need to look further. However, the comparison continues: after that, T_r from r+2 onward is original s, while T_{r'} from r+2 onward continues transformed until r', then original after. So the comparison can be more complex.
Thus, the optimal r is not simply "extend while f(s[j]) >= s[j]". For example, consider s = "9", f(9)=8 (so no increase). But there is increase at earlier? That's trivial.
Better to construct examples:
Suppose s = "2 1", i.e., digits 2,1. n=2. f(2)=2 (neutral), f(1)=9 (increase). The leftmost increase is at index 1 (0-indexed) because f('1')=9 >1, and at index 0 f('2')=2 =2. So i=1. Starting at i=1. Options: r=1 => string "2 9". r= ? only r=1 possible (since n-1=1). So answer "29". That's fine.
Now consider more digits: s = "1 3 2". f(1)=1 (neutral), f(3)=4 (increase), f(2)=1 (decrease). Leftmost increase is at index 1 (digit 3->4). Start at 1. Options: r=1: resulting string: index0:1, index1:4, index2:2 => "142". r=2: apply to indices 1-2: index0:1, index1:4, index2:1 => "141". Compare "142" vs "141": At index2, "2" > "1", so r=1 is better. So even though at index2, f(2)=1 < 2, we stop before. So extend only while beneficial? Here f(2)<2, so we don't extend. Good.
Now consider s = "1 2 3". f(1)=1, f(2)=3 (increase), f(3)=2 (decrease). Leftmost increase at index1 (2->3). Options: r=1 => "1 3 3"? Wait original: 1,2,3. r=1: index1 becomes 3, index2 remains 3 => "133". r=2: index1 becomes 3, index2 becomes 2 => "132". Compare "133" vs "132": at index2, 3 > 2, so r=1 better. So again stop before decrease.
Now consider scenario where extending beyond a point where f(d) = d (neutral) might be beneficial because later we have an increase? But if we stop before a neutral, we have original digit which is same as transformed (since neutral). So it doesn't matter. But if we continue through neutral, it remains same, but then we might later reach an increase which would make the string larger if we include it. However, if we stop before the increase, we would have original digit at that position which might be smaller than the transformed digit (if it's an increase). So it's beneficial to include increase positions. So the decision is: we want to include all positions j > i where f(s[j]) > s[j]? Possibly yes, because including them will increase the digit at that position, which makes the string larger (compared to not including them) provided that we haven't already stopped earlier. But what if including an increase forces us to also include a decrease later because we have to keep the segment contiguous? Actually, the segment is contiguous; if we want to include a later increase, we must also include all intermediate positions between the start and that increase. That means we may have to include positions where f(s[k]) < s[k] (decrease) or neutral. If those intermediate decreases would cause the string to be smaller than if we had stopped before them? But since the first difference occurs at i (the first increase), and we are comparing two candidate r's: one that stops before a decrease, and one that continues through the decrease to include a later increase. At the point of decrease, the string that continues would have a smaller digit at that position than the one that stopped (which has original digit). Even though later there is an increase, the comparison at the decrease position (the first differing index after the common prefix) would make the stopped version larger because its digit is larger (original > transformed). So continuing through a decrease seems detrimental unless the later increase can compensate? But lexicographic comparison is determined at the first differing position. So if we have a decrease at some index j, and we compare two strings: one that stopped before j (so at j it's original s[j]), and one that continued (so at j it's f(s[j]) which is smaller), then at index j the stopped version is larger, regardless of later digits. Therefore, to get a larger string, we cannot pass through a decrease if we have the option to stop before it. However, what if we cannot avoid the decrease because we need to reach a later increase that is after it? But we have the option to not include that later increase; we can stop earlier. So the optimal r would be such that we include all positions from i up to some point, but we should stop before the first position j > i where f(s[j]) < s[j] (i.e., a decrease). Because if we include that decrease, we get a digit smaller than original at j, which makes the string smaller than if we stopped at j-1 (since up to j-1 they are same, at j we have original vs smaller). And if we continue beyond that decrease, the digit at j remains smaller; so the string would be lexicographically smaller than the one that stopped at j-1, regardless of later changes. Hence, the optimal r should not include any decrease positions. So r must be less than the first index after i where f(s[j]) < s[j].
But what about positions where f(s[j]) = s[j] (neutral)? Including them doesn't change the digit. So it doesn't affect comparison with stopping earlier at those positions because if we stop at j-1, at j we have original s[j]; if we continue, at j we have f(s[j]) = s[j] same. So they are equal at j, and we need to look further. So neutral positions don't decide; we may continue through them if later there is an increase, because by continuing we can reach the increase. But careful: If we continue through neutral positions, the string up to those neutral positions is the same as if we had stopped earlier? Let's compare r = j-1 vs r >= j. At index j, both have same digit if neutral, so they are tied. Then at index j+1, r=j-1 gives original s[j+1], while r>=j+1 gives f(s[j+1]) if we also transform j+1. So the decision hinges on the next non-neutral position. So we should continue through neutrals as long as we eventually hit an increase, but we must ensure we do not encounter a decrease before that increase. So we should continue through neutrals, but stop just before the first decrease after i.
Thus, it seems optimal to set r = the last index such that for all j from i to r, f(s[j]) >= s[j]? Actually, we cannot include any j with f(s[j]) < s[j] because that would make the string worse. However, if we have f(s[j]) >= s[j] for all j from i to some point, then including all up to that point yields a string that is at least as large as original at every position, and strictly larger at i (since f(s[i]) > s[i]). But is it always optimal to extend as far as possible while f(s[j]) >= s[j]? Let's test.
Consider s = "1 5 6". f(1)=1 (neutral), f(5)=4 (decrease), f(6)=9 (increase). The leftmost increase is at index 2 (digit 6->9). i=2? But check index1: f(5)=4 <5, not increase. index2: f(6)=9 >6, increase. So i=2. Now f(s[2])>s[2]. Starting at i=2, then we have only r=2 (since n-1=2). So string: "1 5 9". That's the answer? But could we also start earlier? Starting at index1 would cause a decrease at index1 (5->4) which would make first difference a decrease, so that's worse. Starting at index2 is best. So answer "159". That matches extending while f(s[j])>=s[j] from i, but only index2 itself, since index1 is before i. So fine.
Now consider a more subtle case: i is some early increase, then after i we have neutral, then later an increase, but no decrease in between. For example: s = "1 2 3 4". f(1)=1, f(2)=3 (>2), f(3)=3 (=3), f(4)=5 (>4). Leftmost increase is at index1 (2->3). So i=1. For j>=i: index1: 3 (>2); index2: f(3)=3 =3 (neutral); index3: f(4)=5 (>4). There's no decrease. According to the idea, we should extend as far as possible while f(s[j])>=s[j]. That would include index2 (neutral) and index3 (increase). So r = n-1 = 3. Resulting string: index0:1, index1:3, index2:3, index3:5 => "1335". If we stop at r=1: "1 3 3 4" => "1334". Compare "1335" vs "1334": at index3, 5 > 4, so r=3 is better. So yes, extending through neutral to include later increase yields larger.
Now consider case where after i, there is neutral, then a decrease, then later increase. For instance: s = "1 2 3 4 5". f(2)=3 (increase at i=1), f(3)=3 (neutral), f(4)=3 (decrease, because 4->3), f(5)=6 (increase). So indices: 0:1 (neutral), 1:2->3 (increase, i=1), 2:3->3 (neutral), 3:4->3 (decrease), 4:5->6 (increase). If we extend while f>=s, we would stop before the decrease? f(4) < 4, so we cannot include index3. So the maximum r while maintaining f(s[j]) >= s[j] is r=2 (since index1 and 2 satisfy >=, index3 fails). So r=2 yields string: "1 3 3 4 5"? Wait: apply to indices 1-2: digits: index0:1, index1:3, index2:3, index3:4 (original), index4:5 => "13345". If we consider r=4 (including through decrease and later increase): apply to 1-4: index0:1, index1:3, index2:3, index3:3, index4:6 => "13336". Compare "13345" vs "13336": At index3, first differing? Both have index0=1, index1=3, index2=3; at index3, r=2 gives 4, r=4 gives 3. So r=2 is larger. So stopping before decrease is better. So indeed we should not include any decrease.
Now consider case where after i, there is neutral, then decrease, but the decrease is followed by a neutral? Still worse.
So it seems the optimal r is the largest index such that for all j in [i, r], f(s[j]) >= s[j]. But is that always optimal? Let's test edge: what if f(s[i]) > s[i] (increase) but for some j > i, f(s[j]) < s[j] (decrease) but also there exists a k > j such that f(s[k]) > s[k] and also between j and k, all f(s) are either neutral or increase? But if we include j, the string becomes worse at j. Could it be that by including j, we get to transform k to a larger digit, and that the benefit at k might outweigh the loss at j? However, lexicographic order is determined at the first position where strings differ. If we compare two strings: one that stops before j (r = j-1) and one that continues (r >= j). At position j, the stop version has s[j] (higher if f(s[j]) < s[j]), the continue version has f(s[j]) (lower). So regardless of what comes after, the stop version is larger because at the first differing position it has a larger digit. Thus any r that includes a decrease cannot be better than stopping right before that decrease. Therefore, the optimal r must not include any index where f(s[j]) < s[j]. So r must be ≤ the index just before the first decrease after i.
But what if the decrease occurs exactly at i? That can't because i is an increase, so f(s[i]) > s[i]. So decrease not at i.
Thus, the optimal r is the last index before the first decrease after i (inclusive). That is, let j0 = the smallest index > i such that f(s[j0]) < s[j0]; if such exists, then optimal r = j0 - 1. If no such decrease after i, then r can go to n-1.
But is it always optimal to extend to the maximum possible under that condition? Could there be a case where even though f(s[j]) >= s[j] for all j from i to some r, we might get a larger string by stopping earlier? For example, suppose after i, we have a neutral, then an increase, but the increase is followed by a neutral, then later something? But if we stop earlier, we would have original digits after that point. Since original digits might be smaller than transformed digits if those later positions are increases. So extending further (as long as no decrease) will either keep the digit the same (if neutral) or increase it (if increase) compared to original. So at the first position where we differ between stopping earlier and later, the later version will have digit either equal (if neutral) or larger (if increase). If it's equal, we need to look further. Eventually, if we include any increase, at that position the extended version will have larger digit, making it larger. If after i all subsequent positions are neutral, then extending doesn't change the digits, so all r produce the same string from i+1 onward? Actually if all are neutral, then f(s[j]) = s[j] for all j > i, then applying f or not yields same digit. So any r yields the same string. So it's indifferent.
Thus, extending as far as possible (while avoiding decreases) yields a string that is lexicographically at least as large as any shorter r (since the first difference between a shorter r and a longer r will occur at the first position where the longer r includes a digit that is different from original; if that difference is an increase, then longer is larger; if it's neutral, then they are equal and we continue; if it's a decrease, we wouldn't include it). So indeed, longer r (while maintaining no decrease) is at least as good.
Therefore, the optimal strategy: Find the leftmost index i such that f(s[i]) > s[i]. If none, output s. Otherwise, set l = i; set r to be the largest index such that for all j in [l, r], f(s[j]) >= s[j]; i.e., extend to the right as long as f(s[j]) >= s[j]; then apply the transformation to segment [l, r].
But is it always correct to set l = i? Could there be a scenario where starting earlier than i (with neutral digits) and then including i as part of the segment gives the same first increase at i? That would be essentially the same as l=i if the earlier digits are neutral. But what if earlier digits are not all neutral? If there is an earlier index k < i where f(s[k]) > s[k], then i wouldn't be leftmost increase. So by definition, no earlier increase. Could there be an earlier index where f(s[k]) < s[k] (decrease) but we could also include some later increase that makes the overall string larger than the one starting at i? Let's examine. Suppose we have s: index0: digit 2, f(2)=1 (decrease); index1: digit 1, f(1)=9 (increase). Leftmost increase is at index1. Starting at index1 yields string: at index0 unchanged 2, index1 becomes 9 -> "29". What if we start at index0? Then segment [0,1]: index0 becomes 1 (decrease), index1 becomes 9. Resulting string: "19". Compare "29" vs "19": at index0, 2 > 1, so "29" is larger. So starting earlier with a decrease is worse. So indeed l = i is best.
But could there be a case where we start earlier and include some digits that become larger, but those digits are not originally increases? Wait, to become larger, need f(d) > d. So if there is no earlier index with f(d) > d, then we can't get increase earlier. So l = i is fine.
Thus the greedy solution appears correct.
But is there any nuance about the possibility that we might not want to start at the leftmost increase because maybe by starting later we can avoid having to stop early due to a decrease that occurs shortly after, and instead start at a later increase that allows us to extend further (over more increases) and thus produce a larger suffix? However, as argued, any string that has its first increase at a later index will be lexicographically smaller than one that has its first increase at an earlier index, regardless of suffix. So earlier increase always dominates. So we must start at the earliest increase.
But wait, consider a scenario where the leftmost increase i has f(s[i]) > s[i], but after i, there is a decrease at i+1, so we cannot extend beyond i. So the resulting string will have increase at i, then the rest original. Meanwhile, there is another increase at j > i, and from j onward there are many increases and no decreases, so if we started at j, we could transform a long segment and get a string where many digits are increased after j. Could that string be larger than the one from i? Compare: The string from i has first increase at i; the string from j has first increase at j. At position i, the string from i has digit f(s[i]) > s[i], while the string from j has original s[i] (since we didn't change it). So at index i, the first string has a larger digit. Hence it is lexicographically larger. So indeed the earlier increase wins.
Thus the greedy holds.
Now, what if there is no index with f(s[i]) > s[i]? Then we cannot get any increase. However, could we apply the operation and still get a number equal to s? That's possible if we apply to segment where all f(s[i]) = s[i]; but that's same as s. Or maybe we could apply to a segment where some digits become larger but some become smaller, but overall the first difference is an increase? That's impossible because the first non-identity digit in the segment would be the first change. If that first change is a decrease, then the number becomes smaller. If it's an increase, we would have an index with f>d, which contradicts the assumption that no such index exists. So the best is s.
But wait, there is a corner: what if we choose a segment that starts at a position where f(s[l]) = s[l] (neutral) and then later we have f(s[k]) > s[k]? But the first change would be at that later k, and that's an increase, so that would be an index with f > d, contradicting the assumption that no such index exists. So indeed, if no f(d) > d for any digit in the string, then we cannot get any increase anywhere. So answer = s.
Thus the greedy solution: find first i with f(s[i]) > s[i]; then transform from i onward as long as f(s[j]) >= s[j]; that is, continue while condition holds; stop when encountering f(s[j]) < s[j] or end of string.
But is it always correct to continue while f(s[j]) >= s[j]? We must check that after i, if f(s[j]) == s[j] (neutral), it's okay to continue, but is it ever beneficial to stop earlier than the first decrease? Suppose after i, we have a neutral, then a decrease later. Continuing through the neutral yields same digits as stopping before neutral? Let's examine: if we stop at i (r=i), then from i+1 onward we have original digits. If we continue through neutral (say j is neutral), then at j we have same digit as original, so no difference. Then at the next position (which might be a decrease) we would have to decide to stop before it. So continuing through neutral doesn't change anything until we hit a decrease. So it's safe to go through neutrals. So the optimal r is the last index before the first decrease after i. That's equivalent to extending while f(s[j]) >= s[j] until we either hit a decrease or end.
But is there any scenario where f(s[j]) >= s[j] for all j from i to some r, but it's better to stop earlier because after r+1 (original) is actually larger than transformed at r+1 if we had continued? But we wouldn't continue beyond r if we stop at r. So we compare r and r+1. If f(s[r+1]) < s[r+1], then we wouldn't include it; we stop at r. So that's consistent.
Thus algorithm:
- Read n, string a, mapping f[1..9].
- Compute for each digit its transformed digit.
- Scan from left to right to find first index i where f[a[i]] > a[i].
- If none, output a.
- Else, set l = i.
- Then, for j from l to n-1: if f[a[j]] >= a[j], replace a[j] with f[a[j]] and continue; else break (stop replacing). Actually we must apply transformation to contiguous segment only. So we should replace from l up to the last j where condition holds.
- Output the resulting string.
But we must ensure that we only apply the operation once, and we are applying it to exactly segment [l, r]. Our method effectively modifies the string from l onward while condition holds, but careful: We are modifying the string in place; after we break, we stop. That yields the same as applying transformation to segment [l, r] where r is last index with f>=.
But is it possible that after we break (encounter a decrease), there might be later positions where f(s[j]) > s[j] again? But we cannot include them because the segment must be contiguous; if we skip the decrease, we cannot later resume transformation. So we must stop before the decrease. That is consistent.
Thus the answer is to produce the string after applying transformation from the first increase index to as far right as possible without encountering a decrease.
Let's test this on provided examples.
Example 1:
n=4, a="1337"
f: 1->1, 2->2, 3->5, 4->4? Wait mapping: Input "1 2 5 4 6 6 3 1 9". So f(1)=1, f(2)=2, f(3)=5, f(4)=4, f(5)=6, f(6)=6, f(7)=3, f(8)=1, f(9)=9.
Digits: 1->1 (neutral), 3->5 (increase), 3->5 (increase), 7->3 (decrease). First increase at index 1 (0-indexed? string "1 3 3 7": index0='1' neutral, index1='3' ->5 >3, so i=1. Then from index1 onward, check f>=? index1: 5>=3 yes, replace. index2: f(3)=5>=3 yes, replace. index3: f(7)=3 <7, so stop. So segment [1,2]. Result: index0=1, index1=5, index2=5, index3=7 => "1557". That matches output "1557".
Example 2:
n=5, a="11111"
f: 9 8 7 6 5 4 3 2 1 (i.e., f(1)=9, f(2)=8, f(3)=7, f(4)=6, f(5)=5, f(6)=4, f(7)=3, f(8)=2, f(9)=1). So f(1)=9 >1, so first increase at index0. Then extend while f>=? For digit 1, f=9 >=1, so replace; all digits are 1, f(1)=9 >1, so all indices satisfy f>=. So replace all => "99999". Output matches.
Example 3:
n=2, a="33"
f: all 1. So f(3)=1 <3. No digit with f > d. So output original "33". Output matches.
So the greedy works on examples.
But is there any edge case where this greedy fails? Let's consider cases where there is an increase at i, but after i we have a neutral, then a decrease, then later another increase. As argued, including the decrease is bad. So we stop before the first decrease. But could it be that by starting at a later increase j (after the decrease), we might get a longer segment of increases, leading to a suffix that is lexicographically larger from j onward compared to the suffix from i with only a short segment? However, the overall string from i still wins because of earlier increase.
But what about the case where the first increase i is such that f(s[i]) > s[i] but if we transform only that digit, we get a certain string; if we do nothing (i.e., no operation), we get original. But we would transform because it's larger. So yes.
Now consider a scenario where there is an increase at i, but after i, we have a neutral, then a decrease, but the decrease position itself might be transformed to a digit that is equal to original? No, decrease means smaller.
So greedy stands.
But wait, there is another nuance: We assumed that the first increase index i is the leftmost index where f(s[i]) > s[i]. However, what if we can get an increase at an earlier index by starting the segment earlier, even if that earlier index originally has f(s[k]) <= s[k]? Is that possible? Because if we start earlier, we apply f to that earlier index, but that earlier index's digit becomes f(s[k]). For it to be an increase, we need f(s[k]) > s[k]. So if f(s[k]) is not > s[k], it won't become larger. So cannot get increase at that index. So leftmost possible increase index is indeed the smallest index where f(s[i]) > s[i].
Thus algorithm is straightforward.
But wait: Could there be a scenario where we get an increase at an earlier index not by directly applying f to it, but by some indirect effect? No.
So solution: Scan from left to find first position where f(digit) > digit. If none, output original string. Otherwise, from that position, iterate to the right, replacing each digit with f(digit) as long as f(digit) >= digit. Stop when f(digit) < digit (or end). Output modified string.
We need to be careful about replacing: Since we might need to output the string, we can just construct the output string by iterating.
Complexity O(n).
But I recall a Codeforces problem "Maximum Increase" or something similar. I'm confident this is the intended solution.
However, I should double-check with more tricky cases.
Consider s = "2 1", f(2)=3 (>2), f(1)=1 (=1). First increase at index0 (2->3). Then from index0 extend: index0:3>=2, replace; index1: f(1)=1>=1 (equal), so continue? Condition f>=? f(1)=1 >=1 true, so we would replace index1 with 1 (same). So resulting string "31". Is that optimal? Let's examine other possibilities: Could we choose segment [0,0] only => "21"? Actually segment [0,0] yields "31"? Wait original "21". If we apply to [0,0]: digit0 becomes 3, digit1 remains 1 => "31". If we apply to [0,1]: digit0 becomes 3, digit1 becomes 1 => also "31". So same. So fine.
Consider s = "1 2", f(1)=2 (>1), f(2)=1 (<2). First increase at index0 (1->2). Then from index0 extend: index0:2>=1 ok, replace; index1: f(2)=1 <2, so stop. So result: "21". Other options: segment [0,0] => "21" as well; segment [1,1] would be "11" (since f(2)=1, so "11"), which is smaller; no segment yields "22" because we cannot due to decrease at index1. So "21" is best.
Now consider s = "1 9", f(1)=2 (>1), f(9)=1 (<9). First increase at index0, then index1 f(9)=1<9 so stop => "29". That seems best. Could we get "19"? no. "19" is original. So "29" > "19". Good.
Now consider s = "2 2 2", f(2)=1 (<2) and f(1)=9 (>1) (but digit 1 not present). Actually f(2) is decrease, no increase. So output "222". That matches.
Now consider s = "2 1 3", f(2)=1 (<2), f(1)=2 (>1), f(3)=4 (>3). Here leftmost increase is at index1 (1->2). Then from index1 extend: index1:2>=1 ok; index2: f(3)=4>=3 ok; continue? no further. So result: "2 2 4"? Actually original: [2,1,3] -> after transformation on [1,2]: index0 unchanged 2, index1 becomes 2, index2 becomes 4 => "224". Compare other options: If we start at index2 only => "214"? That's smaller. If we start at index0 (segment [0,2]): index0 becomes 1 (decrease) -> "124"? That's smaller. So "224" is best.
Now consider scenario where after first increase, we have f(s[j]) >= s[j] for many, but eventually we hit a decrease. Could there be a case where it's beneficial to stop earlier than the first decrease not at the immediate before but even earlier? Suppose we have: s = "1 5 6". f(1)=2 (>1), f(5)=6 (>5), f(6)=5 (<6). First increase at index0 (1->2). Then extend: index0:2>=1; index1: f(5)=6>=5; index2: f(6)=5<6, so stop before index2 => segment [0,1] yields "2 6 6"? Wait: index0=2, index1=6, index2=6? Actually original index2=6 unchanged => "266". But if we had stopped at index0 only: "2 5 6" => "256". Compare "266" vs "256": at index1, 6>5, so "266" larger. So extending through index1 (which is increase) is good. So stop before decrease is optimal.
Now consider scenario where after first increase, we have a neutral then later a decrease, and after that decrease, there is another increase, but also maybe the decrease digit is such that if we skip it, we can't get later increase. So best is to stop before decrease.
All good.
But wait, there's another nuance: The condition for extending is f(s[j]) >= s[j]. However, what if f(s[j]) == s[j] for many, then later we encounter a decrease, but before that decrease there is an increase? Actually, if there is an increase before the decrease, we would include it because it satisfies f>=. So we include all increases until the first decrease.
Now, consider the possibility that we might want to start the segment later than the first increase, if the first increase leads to a situation where we cannot extend far enough (due to an early decrease) and the resulting suffix might be smaller lexicographically than starting at a later increase that allows a much longer stretch of increases? But as argued, earlier increase dominates. Let's test with numbers to be absolutely sure.
Suppose we have s = "2 9 1 9". f mapping: f(2)=3 (>2), f(9)=1 (<9), f(1)=9 (>1), f(9)=1 (<9). So digits: index0:2->3 increase; index1:9->1 decrease; index2:1->9 increase; index3:9->1 decrease.
According to algorithm: first increase at index0. Then extend while f>=: index0:3>=2 ok; index1: f(9)=1 <9, so stop => r=0. So transformed string: index0=3, index1=9, index2=1, index3=9 => "3919". Compare with starting at index2 (second increase): first increase at index2 -> segment [2,2] because after index2, index3 decrease, so r=2 => string: index0=2,1=9,2=9,3=9? Wait: index2 becomes 9, index3 unchanged 9? Actually original index3=9 unchanged -> "2999"? Let's compute: original "2919"? Wait original: s = "2 9 1 9". Starting at index2: apply to index2 only: digit2 becomes 9, others unchanged: => "2999"? Actually index0=2, index1=9, index2=9, index3=9 -> "2999". Compare "3919" vs "2999". At index0, "3" > "2", so "3919" is larger. So indeed earlier increase wins.
What about starting at index2 and including index3? Cannot because index3 is decrease; would produce "2991"? which is smaller than "2999"? Actually include index3: index3 becomes 1 => "2991", even smaller. So best from index2 is "2999". So earlier increase yields larger.
Now consider a more complex case where the first increase yields a digit that is only slightly larger, but later increase yields a much larger digit? For example, s = "1 9 9 9 ...". f(1)=2 (increase by 1), f(9)=9 (neutral) maybe. Then first increase at index0 gives "2 9 9...". Starting later at some index where f(9)=9? no increase. So earlier increase wins.
But suppose f(1)=2, and f(9)=9 (neutral), but if we start at index0, we get "2 9 9...", while original is "1 9 9..." - obviously better.
Thus seems fine.
Edge case: What if the first increase is at index i, but f(s[i]) is only marginally larger, and if we skip it (i.e., don't transform at i) we could start at a later index j where we can transform a long segment and get a string that is lexicographically larger because at some earlier position, the two strings are equal (original) until j, but at j we get a huge increase that maybe outweighs the earlier missed increase? But lexicographic comparison: At index i, the string that skips i would have original digit s[i] (which is smaller than f(s[i]) in the other string). So at index i, the first string (with transformation at i) has larger digit, so it wins. So skipping i is never beneficial.
Thus greedy is correct.
But we must also consider possibility of performing zero operations. The greedy already handles the case where no increase exists, output original.
Now, is there any scenario where the optimal operation is to apply the transformation to a segment that does not start at the first increase, but still yields a larger overall string because the first increase is at same index? That would be same as starting at that index.
Thus solution is as described.
Let's confirm with some random tests mentally.
I'll write a quick mental simulation: n=5, s="12345", f: f(1)=1, f(2)=3, f(3)=3, f(4)=3, f(5)=6. Leftmost increase at index1 (2->3). Extend while f>=: index1:3>=2 ok; index2: f(3)=3>=3 ok; index3: f(4)=3<4? Actually 3<4 so stop at index2. So result: "1 3 3 4 5" => "13345". Could there be a better segment like [1,4]? That would include index3 (decrease) => "13336", which is smaller. So ok.
What if f(4)=4 (neutral) then we could extend to index3. If f(5)=6 increase, we could extend to index4? but need to check index4: f(5)=6>=5 yes, so extend to end. So result "1 3 3 4 6" => "13346". Good.
Now consider f mapping where there is an increase at index0, but after that there is a decrease, but later there is a big increase, but also earlier increase is small. Still earlier wins.
Now consider if the first increase is at index i, but f(s[i]) > s[i] only by a little, and if we skip it, we could later transform a segment that makes the whole string larger from some later index, but also we could also transform the earlier increase in addition? But we cannot because including earlier increase forces us to either include or exclude the segment. If we include the later increase, we must have a contiguous segment that either includes i or not. If we include i, we get the increase at i. If we don't include i, we don't. So skipping i is only possible if the segment does not cover i. So we can't have both unless the segment starts after i. So we have to choose between including i or not. Including i gives earlier larger digit, so it's better.
Thus the greedy is solid.
Now, confirm with constraints: n up to 2e5, O(n) fine.
Implementation details: Read n (though not needed but okay), read string a, read 9 integers into array f[1..9]. Convert each char to digit int d = a[i] - '0'. Then:
- Create result string res = a (or we can modify a copy).
- flag started = false.
- For i from 0 to n-1:
if not started:
if f[d] > d:
started = true
// we will start transforming from this index and onward as long as condition holds
// But we also need to transform this index and subsequent ones while condition holds.
// So we can enter a second loop.
else:
// we are in the transformation phase; continue while f[d] >= d; else break.
But we can just: find the first index l where f[d] > d. If not found, output a.
Then from l to n-1, while (i <= n-1 and f[d] >= d) replace and i++ else break.
But careful: We must ensure that after we break, we stop processing (i.e., we don't transform later indices even if they satisfy condition, because segment is contiguous). So we can do:
int l = -1;
for(int i=0;i<n;i++) {
int d = a[i]-'0';
if(f[d] > d) {
l = i;
break;
}
}
if(l == -1) output a.
else {
// construct output
StringBuilder sb = new StringBuilder(a.substring(0, l)); // prefix unchanged
int i = l;
while(i < n) {
int d = a[i]-'0';
if(f[d] >= d) {
sb.append(f[d]);
i++;
} else {
break;
}
}
// append remaining unchanged part
sb.append(a.substring(i));
output sb.toString();
}
Wait, we need to be careful: In the while loop, we are replacing from l to as long as condition holds. But we also need to consider that we might have to stop when f[d] < d. However, what about the case where at l, f[d] > d, obviously f[d] >= d holds. So we will include l. Then we proceed.
But is it always correct to stop at first index where f[d] < d? Yes, because if we continue, that index would become smaller. So we break. Then we append the rest unchanged.
Edge case: what if after we break, there are later indices where f[d] >= d again? We ignore because segment must be contiguous. Could it be that it's better to not break at the first decrease, but rather continue and then later increase might make the string larger overall? We argued no because the first decrease would make the string smaller at that index compared to stopping earlier, and lexicographic order would be decided there. Let's test with an explicit example to be sure: s = "1 2 3". f(1)=1 (neutral), f(2)=1 (decrease), f(3)=9 (increase). So first increase? Actually f(2)=1 <2, decrease; f(3)=9>3 increase. So leftmost increase is at index2 (digit 3). According to algorithm, l=2, then while: index2: 9>=3 -> replace, then i=3 end; result: "1 2 9" -> "129". What if we started at index1? That would be decrease at index1 (2->1), then increase at index2 (3->9): segment [1,2] yields "1 1 9" -> "119". Compare "129" vs "119": at index1, 2 > 1, so "129" larger. So algorithm yields correct.
Now consider a case where the first increase is at index0, but then at index1 there's a decrease, and at index2 there's a huge increase. According to algorithm, we stop at index0, result: f(s[0]), then original s[1], s[2], ... So we get increase at 0, then original at 1 (maybe high), then original at 2 (maybe not increased). If we instead took segment [0,2] (including decrease at 1 and increase at 2), we would have at index0 increased, at index1 decreased, at index2 increased. Compare these two strings (stop at 0 vs include through 2). At index0, both have same increased digit (since both include index0). At index1, stop version has original s[1] (which is > f(s[1]) because f(s[1]) is decrease), so stop version has larger digit at index1. Therefore stop version is lexicographically larger regardless of index2. So indeed stop before decrease is better. So correct.
Thus algorithm stands.
Now, one more nuance: The operation can be performed no more than once, meaning we could also choose to not perform it. In our algorithm, we only perform if there's an increase. But what if there is no increase, but we could still perform the operation and get a string that is equal to the original? That's the same. So output original is fine.
Double-check with scenario where f(d) = d for all digits. Then no increase, output original. That's correct.
Now, scenario where there is an increase, but after the first increase, there is a decrease at some index, and beyond that there are many neutrals and maybe another increase later. Our algorithm stops before the decrease, resulting in a string with original digits after the stop, which may include later increases as original (not transformed). But could it be that it's better to not transform the first increase at all, but instead start at the later increase and transform a longer segment that yields a larger suffix? However, as argued, having the first increase at an earlier position yields a larger digit at that earlier position, so it's better. Let's test with concrete numbers to be absolutely sure.
Example: s = "2 1 1 9". f(2)=3 (increase), f(1)=1 (neutral), f(9)=5 (increase but also decrease? Actually 9->5 is decrease). Wait we need later increase. Let's design: f(2)=3 (increase), f(1)=1 (neutral), f(9)=9? That's neutral. To have later increase, we need a digit d where f(d) > d. Let's have digit 3? But we don't have 3. Let's set s = "2 1 3". f(2)=3 (increase), f(1)=1 (neutral), f(3)=4 (increase). Then first increase at index0. According to algorithm: l=0; then while: index0:3>=2 -> replace; index1: f(1)=1>=1 (neutral) -> replace (but it's same 1); index2: f(3)=4>=3 -> replace; so we transform all => "3 1 4"? Actually original "2 1 3". After full transform: index0=3, index1=1, index2=4 => "314". If we instead did not transform index0 but started at index2 only: segment [2,2] => "2 1 4" => "214". Compare "314" vs "214": at index0, 3 > 2, so "314" wins. So early increase is better.
But suppose early increase is very small, and later increase is huge. Example: s = "1 9 9 9 9 9 9". f(1)=2 (increase by 1), f(9)=1 (decrease). First increase at index0. Then index1 f(9)=1 <9, so stop => result "2 9 9 9 9 9 9" => "2999999". If we skip index0 and instead start at later? There's no later increase because f(9) is decrease. So not.
Need later increase: f(9) must be >9? impossible because max digit is 9. So later increase can only be for digits <9. So if we have a lot of 9's, they can't be increased. So the only possible increases are for digits less than 9. So the first increase is for the first digit less than 9 that maps to a higher digit. So it's plausible that the first increase might be only a slight increase (e.g., 1->2) while later there could be a larger jump (e.g., 8->9). But still, the early increase occurs at an earlier position, which is more significant.
Consider s = "1 8 8 8". f(1)=2, f(8)=9. So first increase at index0 (1->2). Then index1: f(8)=9>=8, so we continue; index2: 9>=8; index3: 9>=8. So we transform all => "2 9 9 9" => "2999". If we didn't transform index0, we could start at index1: segment [1,3] => "1 9 9 9" => "1999". Compare "2999" > "1999". So early wins.
Now consider s = "1 9 8". f(1)=2, f(9)=9 (neutral), f(8)=9 (increase). First increase at index0 (1->2). index1: f(9)=9>=9 neutral, continue; index2: f(8)=9>=8, continue => "2 9 9" => "299". If skip index0, start at index2: "1 9 9" => "199". "299">"199". So early wins.
Now consider s = "2 1 8". f(2)=3 (increase), f(1)=1 (neutral), f(8)=9 (increase). First increase at index0. According to algorithm: transform all because index0:3>=2, index1:1>=1, index2:9>=8 => "3 1 9" => "319". If skip index0, start at index2: "2 1 9" => "219". "319">"219". So early wins.
Thus it's consistent.
Now, what about the scenario where there is no increase at all? We output original.
Now, I think the solution is correct.
But wait, I recall a Codeforces problem "C. Replace To Make...?" Actually the problem I'm thinking of is Codeforces Round #... It might be problem "A. Digital Root" no. Actually I recall a problem "C. Replace Digits" but not exactly. I'll check memory: This problem appears to be Codeforces Round #... Probably "A. Digits Sequence (Hard Version)"? No.
Anyway, the solution is straightforward.
But let's double-check the condition "f(s[j]) >= s[j]" for extending. Could there be a case where f(s[j]) > s[j] but it's still beneficial to stop before it? That would be weird because if we stop before j, we have original s[j]; if we include j, we have f(s[j]) which is larger. So including it yields a larger string (since all earlier digits same). So we should include it. So yes.
Now, what about the scenario where f(s[j]) = s[j]? Including it yields same digit, so it doesn't hurt; but it might allow us to include later increases. So we should include it as long as later there is an increase before any decrease. But our while loop includes neutrals, and stops only when encountering a decrease. That will automatically include neutrals and then later if there is an increase after neutrals but before any decrease, it will be included because we haven't stopped. However, if after some neutrals, there is a decrease, we will stop before the decrease, thereby not including the neutrals that are after? Actually, we include neutrals until we hit the decrease. The neutrals before the decrease are included, but not the decrease. But what if after the decrease, there is an increase? We won't get it. But is it ever beneficial to skip some neutrals before a decrease to be able to include later increase? That would require the segment to be non-contiguous; we can't skip.
Thus algorithm is optimal.
Now, I want to consider the possibility of performing the operation on a segment that starts before the first increase but includes it, and all earlier digits are neutral. That yields same as starting at i. So fine.
Now, one more nuance: The operation can be performed at most once, but we could also choose to apply it to an empty segment? The problem says non-empty contiguous subsegment. So we must choose a non-empty segment if we perform operation. So our algorithm only applies if there is an increase; that segment is non-empty because l is defined and we apply at least that digit.
Edge case: n=1. Then we check if f(d) > d: if yes, replace; else output original.
Example: n=1, a="5", f(5)=5 -> output "5". If f(5)=6 -> output "6". Good.
Now, confirm with a tricky scenario: s = "2 3", f(2)=3 (>2), f(3)=2 (<3). Then first increase at index0. Then index1: f(3)=2<3, so stop. Output "2 3"? Wait: index0 becomes 3? Actually transformation on [0,0] yields "33"? Let's compute: original "23". If we apply to index0 only: digit0 becomes 3 => "33". If we apply to index0-1: both become 3 and 2 => "32". So best is "33". Our algorithm: l=0, replace index0 with 3, then check index1: f(3)=2<3 so break, then append rest (index1 unchanged = 3?) Wait careful: original index1 is 3, unchanged. So after transformation of index0, we have index0=3, index1 remains 3 (since we didn't transform it) -> "33". That's correct.
Now consider s = "3 2", f(3)=4 (>3), f(2)=1 (<2). Then l=0, index0 becomes 4, index1: f(2)=1<2 so break, output "4 2"? Actually index1 unchanged =2 -> "42". Is that optimal? Option segment [0,0] -> "42". Segment [0,1] -> "41". So "42" best.
Now consider s = "2 3 4", f(2)=3 (>2), f(3)=3 (=3), f(4)=3 (<4). l=0, index0=3, index1: f(3)=3>=3, include (replace with 3, same), index2: f(4)=3<4 so break, output: "3 3 4" => "334". Alternative: segment [0,0] -> "334"? Actually segment [0,0] gives index0=3, index1=3 unchanged? Wait original index1=3, so "33 4"? -> "334". Same. So okay.
Now consider a scenario where there is an increase at index i, but the condition f(s[j]) >= s[j] fails at some j > i, but what if at that j, f(s[j]) < s[j] but if we stop at j-1, we get original s[j]; but what if we start the segment later, at some index k > i, where f(s[k]) > s[k] and then we can extend further without encountering a decrease until much later, producing a long transformed suffix that might be lexicographically larger after some point? But as argued, the string with early increase is lexicographically larger overall because at index i we have a larger digit. However, what if the early increase is at index i, but the transformed digit f(s[i]) is only marginally larger (say 2->3), and the rest of the string after i is largely unchanged because we stop early, while the alternative that starts at later index k might produce a string that at index i is original s[i] (which is 2), but at index i+1 maybe they are equal, at i+2 maybe they are equal, ... up to k-1 they are equal, but at k, the alternative has a much larger digit (e.g., 1->9) while the early-increase version at k has original digit (maybe small). So compare early version (call it A) and later version (call it B). At index i, A has digit 3, B has digit 2. So A > B. So A wins. So indeed early increase wins regardless of later digits.
Thus the greedy is solid.
Now, I'll think about potential pitfalls: The mapping f is given for digits 1-9, and digits are from 1 to 9. So no zeros. Good.
Implementation note: When reading the string, we must treat each character as digit. Use integer conversion.
Now, check that we correctly handle the case where we have first increase but after that we have f(s[j]) >= s[j] for all j to the end. Then we replace all from l to end. That's fine.
Now, one more nuance: The problem statement says "You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in a, and replace each digit x from this segment with f(x)." So we can also choose to not perform the operation. Our algorithm does that if no increase.
Now, is there any scenario where performing the operation even without any increase could lead to a number equal to the original but maybe we might want to do it? That doesn't change anything.
Thus solution is final.
Now, before finalizing, I'll test with some random generated cases using reasoning.
Consider n=5, s = "5 1 4 3 2". f: f(5)=6, f(1)=1, f(4)=4, f(3)=5, f(2)=2. So increases: 5->6 at index0, 3->5 at index3. First increase at index0. Check condition for extending: index0:6>=5 -> replace; index1: f(1)=1>=1 neutral -> replace (1); index2: f(4)=4>=4 neutral -> replace (4); index3: f(3)=5>=3 -> replace (5); index4: f(2)=2>=2 neutral? Actually 2>=2 true, replace (2). So all indices satisfy >=, so transform all => "6 1 4 5 2" => "61452". Could there be a better by starting later? Starting at index3 only: "5 1 4 5 2" => "51452". "61452" > "51452". So correct.
Now, consider f where some digits are decreased but later increases are there but we stop early: s = "3 1 4 1 5". f(3)=4 (increase), f(1)=1 (neutral), f(4)=3 (decrease), f(5)=6 (increase). First increase at index0 (3->4). Extend: index0:4>=3 ok; index1:1>=1 ok; index2: f(4)=3<4 stop. So result: "4 1 4 1 5" => "41415". Alternative: start at index4? index4: f(5)=6>5, but no earlier increase, but there is a decrease at index2? Actually f(4)=3 decrease, but index4 is increase. If we start at index4: segment [4,4] -> "3 1 4 1 6" => "31416". Compare "41415" vs "31416": at index0, 4 > 3, so "41415" wins. So correct.
Now, consider scenario where first increase is at i, but the condition f(s[i+1]) >= s[i+1] is true, but f(s[i+2]) < s[i+2], so we stop at i+1. However, what if we decide to stop earlier, say at i only? Would that ever be better than stopping at i+1? Stopping at i+1 includes index i+1 which might be neutral or increase. If it's neutral, stopping at i and at i+1 produce same digit at i+1? Actually if it's neutral, f(s[i+1]) = s[i+1], so both strings have same digit at i+1; so they are equal up to i+1. But then at i+2, the version that stopped at i has original s[i+2], while version that stopped at i+1 also has original s[i+2] because we stopped before i+2? Wait: If we stop at i, we transformed only [i,i], so at index i+1 we have original s[i+1]; at index i+2 we have original s[i+2]. If we stop at i+1, we transformed [i,i+1], so at index i+1 we have f(s[i+1]) which equals s[i+1] (neutral), so same as original; at index i+2 we have original s[i+2] as well. So both yield identical string! So it doesn't matter. If it's increase, then including it yields larger digit at i+1, which makes the string larger. So stopping later (including increase) is better or equal. So we should extend as far as allowed.
Thus algorithm stands.
Now, confirm with a corner: if the first increase is at index i, but f(s[i+1]) >= s[i+1] is false because it's a decrease, we stop at i. That's fine.
Now, consider the possibility of multiple increases, but the segment could be chosen to start at a neutral position before the first increase, but with all earlier neutrals, same effect. So it's fine.
Thus the solution is O(n) and simple.
But wait, is there any scenario where it's beneficial to start the segment after the first increase? That would mean we deliberately skip an increase, which seems suboptimal.
Thus final answer.
Now, implement.
Implementation steps:
-
Read integer n.
-
Read string a.
-
Read array f of size 10 (index 1-9).
-
Convert string to char array or just process.
-
Set flag start = false.
-
Build result in StringBuilder.
-
Iterate i from 0 to n-1:
d = a[i] - '0'
if (!start) {
if f[d] > d:
start = true
result.append(f[d])
else:
result.append(a[i])
} else {
// we are inside segment
if f[d] >= d:
result.append(f[d])
else {
// stop transforming, append the rest as original and break
result.append(a.substring(i))
break;
}
}
But careful: If we break, we must not continue loop; we can break out. After loop, if we never broke (i.e., reached end), we have the result.
Alternatively, we can do as earlier: find l, then construct.
I'll implement the scanning method.
Pseudo-code:
int n;
string s;
int f[10];
cin >> n >> s;
for (int i=1; i<=9; ++i) cin >> f[i];
string res;
bool transforming = false;
bool operation_done = false; // actually we start transforming only if we decide to.
for (int i=0; i<n; ++i) {
int d = s[i] - '0';
if (!transforming) {
if (f[d] > d) {
transforming = true;
res += char(f[d] + '0');
} else {
res += s[i];
}
} else {
if (f[d] >= d) {
res += char(f[d] + '0');
} else {
// stop, append the rest unchanged
res += s.substr(i);
break;
}
}
}
if (!transforming) // no increase found, res is original
cout << s << endl; // but res already equals s? Actually we never transformed, so res is s. So fine.
else
cout << res << endl;
But note: In the else block when we break, we appended the rest substring. We must ensure we don't double append after loop. So break works.
Edge: If we never entered transforming (i.e., no increase), we just output s. But our code as written would have transforming false all the way, and res built by appending each original digit. So res equals s. So we can just output res.
Now, we need to be careful: if we break, we exit loop, and we don't process further. So it's fine.
Now, test on examples:
Example1:
s="1337", f[1]=1,f[3]=5,f[7]=3...
i=0: d=1, f=1 not >, so res+='1', transforming false.
i=1: d=3, f=5 >3 => transforming=true, res+='5'.
i=2: d=3, f=5 >=3 => res+='5'.
i=3: d=7, f=3 <7 => else: res+= s.substr(3) => "7", break. res="1557". Good.
Example2: all 1s, f[1]=9>1, so at i=0 start transforming, and then for all, f>=, so res becomes all 9's. No break. Output "99999". Good.
Example3: s="33", f[3]=1 not >, so never start, res="33". Output "33". Good.
Now, consider scenario where we start transforming and then we never hit a decrease, we go to end. Works.
Consider scenario where we start transforming at last character. Then after appending transformed digit, loop ends. res contains transformed last digit, rest unchanged before. That's fine.
Now, check memory and time: O(n).
One more nuance: The operation can be performed at most once. In our algorithm, we only apply one contiguous segment because we start transforming at first increase and then continue contiguously until we stop. That's exactly one segment.
Thus solution accepted.
But I want to double-check with a scenario where the first increase occurs, but the condition f >= holds for some indices, then we encounter a decrease, we stop. But what if after that decrease, there are more digits where f >= again? We ignore them, but could there be a scenario where it's better to not start at the first increase, but start later after the decrease to get a longer stretch of increases? However, as argued, starting later yields a string that is lexicographically smaller because at the index of first increase, it's original digit which is less than the transformed digit. So it's worse. So ignoring later ones is fine.
Now, check if there is any scenario where there is no increase, but we might want to perform the operation anyway to get a number that is equal to the original? That's same as original. So no.
Thus final.
Now, before final answer, let's think about if there is any scenario where f(s[i]) > s[i] but we might not want to start at i because maybe by starting later we could get a string that is equal to original up to some point and then become larger at a later index, but the overall string might be lexicographically larger than the one with early increase? That can't happen because at i, the later-start string has original digit, which is smaller than the increased digit at i from early-start string. So early-start string is larger.
Wait, could there be a scenario where the early increase results in a digit that is actually smaller than some other digit that would appear if we didn't increase? No, because lexicographic comparison is digit by digit. For the early-start string, at index i, the digit is f(s[i]) > s[i]. For the later-start string, at index i, the digit is s[i]. So early-start's digit is greater. So early-start wins.
But consider the case where the early increase is at index i, but what if we can also increase some earlier index by starting earlier? But we can't because there is no earlier index with f > d.
Thus done.
Now, implement.
Potential pitfalls: The input string may contain newline; need to handle reading.
Now, code in C++ or Python? The user didn't specify language, but as ChatGPT we can output solution description. Usually the answer expects to output the number. But I'll provide explanation and code.
Since the problem is from Codeforces (maybe 1155A or something), typical solution is as described.
I'll produce final answer with explanation and code in C++.
But before final, I'll test some random cases with a script mentally? Let's do a few:
Case: n=3, a="212", f: f(2)=3, f(1)=1, f(2)=3 (so f(2)=3). Then digits: index0=2->3 increase, index1=1->1 neutral, index2=2->3 increase. First increase at 0. Then extend: index0:3>=2 include, index1:1>=1 include, index2:3>=2 include => all => "313"? Actually original "212": after transform all: index0=3, index1=1, index2=3 => "313". If we only transform from index0 to 0: "312"? Actually "212"->[0,0] => "312"? Wait index0=3, index1=1 (unchanged), index2=2 (unchanged) => "312". "313" > "312". So correct.
Case: n=3, a="212", f(2)=3, f(1)=2 (increase), f(2)=3. Then increases at index0? 2->3 >2, index1:1->2 >1, so first increase at index0. Extend: index0:3>=2, index1:2>=1, index2:3>=2 => all => "323"? Wait original "212": index0=3, index1=2, index2=3 => "323". Could we get larger by starting at index1? That would be "2 2 3"? Actually segment [1,2]: index0=2, index1=2, index2=3 => "223". Compare "323" > "223". So ok.
Now, consider tricky case: s = "199", f(1)=2, f(9)=1. First increase at index0 (1->2). index1: f(9)=1<9, stop => result "299"? Wait index1 unchanged 9, index2 unchanged 9 => "299". Could there be a better segment like [2,2]? But index2 is 9, f(9)=1 (<9) so not increase. So only early. So "299" is max.
Now, consider s = "129", f(1)=2, f(2)=2 (neutral), f(9)=1 (decrease). First increase at index0 (1->2). index1: f(2)=2>=2 neutral, include; index2: f(9)=1<9 stop => result "2 2 9"? Actually index0=2, index1=2, index2=9 => "229". Original "129". Could we do segment [1,1]? f(2)=2 neutral gives same; segment [2,2]? decrease => "121". So "229" best.
Now, consider s = "129", f(1)=1, f(2)=3, f(9)=9. First increase at index1 (2->3). Then index2: f(9)=9>=9 neutral, include => result "1 3 9" => "139". Check alternative: segment [0,2]? includes index0 neutral (1->1) and index1 increase, index2 neutral => same "139". So fine.
Now, consider scenario where there are multiple increases but also decreases in between. Our algorithm will stop at the first decrease after start. Could it be beneficial to start after that decrease to get later increases? But as argued earlier, the string with earlier increase (even if short) is larger. But test numerically to be absolutely sure: Let's construct a case with numbers: s = "2 1 1 9". Wait we need a digit that can be increased after a decrease. Let's define digits:
s = "2 3 1 9". f(2)=3 (increase), f(3)=2 (decrease), f(1)=9 (increase), f(9)=9 (neutral).
First increase at index0 (2->3). Now extend: index0:3>=2 ok; index1: f(3)=2 <3 -> decrease, so stop. Result: transform only index0 => "3 3 1 9"? Actually index0 becomes 3, index1 unchanged 3, index2 unchanged 1, index3 unchanged 9 => "3319". Now consider starting at index2 (1->9): segment [2,2] or [2,3]? f(9)=9 neutral, so best to include index3? But neutral, so string: index0=2,1=3,2=9,3=9 => "2399". Compare "3319" vs "2399": At index0, 3 > 2, so "3319" wins. So early wins.
Now make the increase at index0 very small, like 2->3, and the later increase huge, like 1->9. Still early wins.
But consider if the early increase is at index0, but the digit at index0 is 1, and f(1)=2, a small increase, and the later increase is at index2, where digit is 1, f(1)=9. The early version yields at index0:2, index1 maybe something, index2 maybe 1 (unchanged). Later version yields at index0:1 (original), index1: unchanged, index2:9. Compare 2... vs 1...: 2 > 1, so early wins.
Thus algorithm stands.
Now, consider scenario where there is no increase at all, but we might still apply operation to a segment where some digits become larger and some become smaller, but the net effect might be a number that is lexicographically larger than original because the first change is an increase? But if there is no digit where f(d) > d, then for every digit, f(d) <= d. So if we apply the operation to any segment, the first digit where f(d) != d will have f(d) < d (since cannot be >). That means the first difference is a decrease, making the number smaller. Therefore, we should not apply any operation. So original is optimal.
But wait, what about the possibility that f(d) = d for some digits, and we could choose a segment that starts with a neutral digit, then later has a digit where f(d) = d (still neutral), and no digit with f(d) > d, then the transformed string equals original. So same.
Thus algorithm holds.
Now, I'll also think about the scenario where f(d) > d for some digits, but the first such digit is at index i, but maybe it's better to start the segment earlier at some index where f(d) = d (neutral) and then at i we get increase, but also maybe we could include some earlier digit that becomes larger due to f? But if earlier digit becomes larger, then we would have an earlier increase, contradicting that i is first index with f>d. So earlier digits must be either neutral or decrease. Including a decrease earlier would be bad, so we avoid. So start at i.
Now, could we start at a later index where f>d and get a larger increase amount? But we already have an increase at i. Even if f(s[i]) is only 1 more, it's still larger than original at i. So it's better than any later increase.
Thus the greedy is proven.
Now, I'd like to think if there's any scenario where we might want to apply the operation to a segment that doesn't include the first increase but still yields a string that is lexicographically larger because the first increase might be "overwritten" by a later digit? No.
Now, the solution seems too simple; however, I recall a Codeforces problem with exactly this solution.
Thus final answer: implement as described.
Now, I'll produce final answer with explanation and C++ code.
But as ChatGPT, I should output the answer? The user says "You are given ... Output ...". The user expects the maximum number. But the conversation is in the context of problem solving; maybe they want the solution description? The initial message is a problem statement, and they likely want a solution code. Usually in these tasks, I provide an explanation and then the code. So I'll output a C++ solution.
Implementation details in C++:
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
int f[10];
for (int i = 1; i <= 9; ++i) cin >> f[i];
string res;
bool started = false;
for (int i = 0; i < n; ++i) {
int d = s[i] - '0';
if (!started) {
if (f[d] > d) {
started = true;
res += char(f[d] + '0');
} else {
res += s[i];
}
} else {
if (f[d] >= d) {
res += char(f[d] + '0');
} else {
// stop and copy the rest
res += s.substr(i);
break;
}
}
}
cout << res << endl;
return 0;
}
But we must be careful: When we break, we have appended the rest; but we should ensure we don't append again. That's fine.
Also, if we never break, we already built the whole string.
Now, test with given examples.
Edge Cases: n=200000, string length large, should be fine.
One more nuance: In C++, string::substr(i) creates a new substring from i to end. That's O(n) in worst case if we call it many times, but we only call it once at most, so O(n) total.
Alternatively, we could just loop after break, but it's fine.
Now, final answer.
But before final, I want to double-check a tricky scenario: Suppose we start transforming, then we include some neutrals, then we encounter a decrease, we stop and append the rest. However, what if the rest includes digits that we might have wanted to transform if we had started later? But we stop; but as argued, that's okay because we already have a larger string than any that starts later. But could there be a scenario where the first increase is at i, but after we stop at the first decrease, the remaining part contains another increase, and the resulting string (with early increase) might be lexicographically smaller than a string that starts later and includes that later increase? Let's compare:
Let A = string from algorithm: prefix up to i-1 unchanged, at i increased, then from i+1 to some point maybe unchanged (including a decrease? Actually we stop before decrease, so at i+1...j-1 we transformed (if any neutrals/increases) but we stop at the first decrease at position k, so positions i+1..k-1 are transformed (with f >=). At position k, we have original s[k] (since we stopped before). After that, positions k+1... are original (maybe includes later increases).
Let B = string from starting at later index p (p > i) where f(s[p]) > s[p], and we extend as far as possible without decrease. B has prefix up to p-1 unchanged (so at i, it's original s[i] which is less than f(s[i]) because f(s[i])>s[i]). So at the first index where A and B differ, which is i (since A changed i, B didn't), A has larger digit. So A > B. So A wins.
Thus algorithm's result is maximum.
Now, what about the possibility of performing zero operation? Our algorithm would produce original if no increase. So that's covered.
Thus solution is correct.
Now, I'll present final answer with explanation and code. ```cpp
#include
#include
using namespace std;
int main() {
int n;
string a;
cin >> n >> a;
int f[10]; // f[1..9]
for (int i = 1; i <= 9; ++i)
cin >> f[i];
string ans;
bool inSegment = false; // have we started the transformation?
for (int i = 0; i < n; ++i) {
int d = a[i] - '0';
if (!inSegment) {
// still before the segment
if (f[d] > d) { // first increase -> start segment
inSegment = true;
ans += char(f[d] + '0');
} else {
ans += a[i];
}
} else {
// we are inside the segment
if (f[d] >= d) { // still beneficial or neutral
ans += char(f[d] + '0');
} else {
// first decrease: stop the segment and copy the rest unchanged
ans += a.substr(i);
break; // nothing more to do
}
}
}
// If we never entered the segment, ans already equals the original number.
cout << ans << endl;
return 0;
}