1157B.md (1477B)
1 Source: https://codeforces.com/problemset/problem/1157/B 2 3 B. Long Number 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given a long decimal number $a$ consisting of $n$ digits from $1$ to $9$. You also have a function $f$ that maps every digit from $1$ to $9$ to some (possibly the same) digit from $1$ to $9$. 22 23 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)$. For example, if $a = 1337$, $f(1) = 1$, $f(3) = 5$, $f(7) = 3$, and you choose the segment consisting of three rightmost digits, you get $1553$ as the result. 24 25 What is the maximum possible number you can obtain applying this operation no more than once? 26 27 Input 28 29 The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of digits in $a$. 30 31 The second line contains a string of $n$ characters, denoting the number $a$. Each character is a decimal digit from $1$ to $9$. 32 33 The third line contains exactly $9$ integers $f(1)$, $f(2)$, ..., $f(9)$ ($1 \le f(i) \le 9$). 34 35 Output 36 37 Print the maximum number you can get after applying the operation described in the statement no more than once. 38 39 Examples 40 41 Input 42 43 ``` 44 4 45 1337 46 1 2 5 4 6 6 3 1 9 47 ``` 48 49 Output 50 51 ``` 52 1557 53 ``` 54 55 Input 56 57 ``` 58 5 59 11111 60 9 8 7 6 5 4 3 2 1 61 ``` 62 63 Output 64 65 ``` 66 99999 67 ``` 68 69 Input 70 71 ``` 72 2 73 33 74 1 1 1 1 1 1 1 1 1 75 ``` 76 77 Output 78 79 ``` 80 33 81 ```