CF BUDDY
← Problems·

1157B · Long Number

1300 · greedy

Problem: You are given a long decimal number aa consisting of nn digits from 11 to 99. You also have a function ff that maps every digit from 11 to 99 to some (possibly the same) digit from 11 to 99.

You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in aa, and replace each digit xx from this segment with f(x)f(x). For example, if a=1337a = 1337, f(1)=1f(1) = 1, f(3)=5f(3) = 5, f(7)=3f(7) = 3, and you choose the segment consisting of three rightmost digits, you get 15531553 as the result.

What is the maximum possible number you can obtain applying this operation no more than once?

Input Format: The first line contains one integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of digits in aa.

The second line contains a string of nn characters, denoting the number aa. Each character is a decimal digit from 11 to 99.

The third line contains exactly 99 integers f(1)f(1), f(2)f(2), ..., f(9)f(9) (1f(i)91 \le f(i) \le 9).

Output Format: Print the maximum number you can get after applying the operation described in the statement no more than once.

Sample Cases

Case 1

Input

4
1337
1 2 5 4 6 6 3 1 9

Output

1557

Case 2

Input

5
11111
9 8 7 6 5 4 3 2 1

Output

99999

Case 3

Input

2
33
1 1 1 1 1 1 1 1 1

Output

33

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback