#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 128;
const int MAXL = 32;

struct card {
    char name[MAXL];
    int c;
    int idx;
};

int m, n;
deque <card> dq;
int a[MAXN][2], b[MAXN][2];
int d[MAXN][MAXN];
vector <card> res[2];

inline void next(int &team, int &turn) {
    if (team == n - 1) team = 0, turn ^= 1;
    else ++team;
}

int main() {
    int i, j, k;
    int s, t;
    int team, turn;
    int q, c;
    int rem;
    int tot;
    card xx;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    scanf("%d %d", &n, &t);
    for (i=0; i<n; ++i) for (j=0; j<2; ++j) scanf("%d %d", a[i]+j, b[i]+j);
    scanf("%d", &m);
    for (i=0; i<m; ++i) {
        scanf("%s %d", xx.name, &xx.c);
        xx.idx = i;
        dq.push_back(xx);
    }

    i = 0, j = 0;
    while (!dq.empty()) {
        tot = t;
        while (!dq.empty()) {
            xx = dq.front();
            dq.pop_front();
            c = xx.c, q = j ^ 1, k = xx.idx;
            rem = max(1, c-(a[i][j]+b[i][q])-d[i][k]);

            if (tot >= rem) {
                res[i].push_back(xx);
                tot -= rem;
            } else {
                d[i][k] += rem;
                dq.push_back(xx);
                break;
            }
        }
        next(i, j);
    }

    for (i=0; i<2; ++i) {
        printf("%d ", res[i].size());
        for (j=0; j<res[i].size(); ++j) printf("%s%s", res[i][j].name, j==res[i].size()-1?"\n":" ");
    }

    return 0;
}


#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 128;
const int MAXL = 32;

struct card {
    char name[MAXL];
    int c;
    int idx;
};

int m, n;
deque <card> dq;
int a[MAXN][2], b[MAXN][2];
int d[MAXN][MAXN];
vector <card> res[MAXN];

inline void next(int &team, int &turn) {
    if (team == n - 1) team = 0, turn ^= 1;
    else ++team;
}

int main() {
    int i, j, k;
    int s, t;
    int team, turn;
    int q, c;
    int rem;
    int tot;
    card xx;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    scanf("%d %d", &n, &t);
    for (i=0; i<n; ++i) for (j=0; j<2; ++j) scanf("%d %d", a[i]+j, b[i]+j);
    scanf("%d", &m);
    for (i=0; i<m; ++i) {
        scanf("%s %d", xx.name, &xx.c);
        xx.idx = i;
        dq.push_back(xx);
    }

    i = 0, j = 0;
    while (!dq.empty()) {
        tot = t;
        while (!dq.empty()) {
            xx = dq.front();
            dq.pop_front();
            c = xx.c, q = j ^ 1, k = xx.idx;
            rem = max(1, c-(a[i][j]+b[i][q])-d[i][k]);

            if (tot >= rem) {
                res[i].push_back(xx);
                tot -= rem;
            } else {
                d[i][k] += tot;
                dq.push_back(xx);
                break;
            }
        }
        next(i, j);
    }

    for (i=0; i<n; ++i) {
        printf("%d ", res[i].size());
        for (j=0; j<res[i].size(); ++j) printf("%s%s", res[i][j].name, j==res[i].size()-1?"\n":" ");
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

int main( )
{
    int n,m;
    int a[20005]={0},b[15]={0};
    int c[20005]={0},d[20005]={0};
    scanf("%d",&n);
    scanf("%d",&m);
    for(int i=1;i<=9;i++){
       scanf("%d",&b[i]);
    }
    int x=m;
    for(int i=n;i>=1;i--){
        a[i]=x%10;
        x=x/10;
    }
    for(int i=1;i<=n;i++){
        int r=a[i];
        c[i]=b[r];
    }
    for(int i=1;i<=n;i++){
        if(a[i]<c[i])
            d[i]=c[i];
        else
            d[i]=a[i];
    }
    for(int i=1;i<=n;i++){
        printf("%d",d[i]);
    }
    cout << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main( )
{
    int T,n,m;
    long long  ans=0;
    int a[30010]={0},b[10]={0};
    int c[30010]={0};
    scanf("%d",&T);
    scanf("%d",&n);
    for(int i=1;i<=9;i++){
        scanf("%d",&b[i]);
    }
    m=n;
    long long ct=1;

    for(int i=1;i<T;i++){
        ct *= 10;
    }

    for(int i=T;i>=1;i--){
        a[i]=m%10;
        m=m/10;
    }

    for(int i=1;i<=T;i++){
        int an = a[i];
        c[i] = b[an];
    }

    for(int i=1;i<=T;i++){
        ans += (a[i]>c[i]?a[i]:c[i])*ct;
        ct /= 10;
    }
    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
    int n;
    string s;
    int a[10]={0};
    cin >> n;
    cin >> s;
    for(int i=1;i<=9;i++){
        cin >> a[i];
    }
    int sign=0;
    for(int i=0;i<s.size();i++){
        int t=s[i]-'0';
        if(sign){
            if(a[t]>=t)
                s[i]=a[t]+'0';
            else
                break;
        }
        else{
            if(a[t]>t){
                s[i]=a[t]+'0';
                sign=1;
            }
        }
    }
    cout << s <<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

int main( )
{
    int n;
    string m;
    int a[20005]={0},b[15]={0};
    int c[20005]={0};
    cin >> n;
    cin >> m;
    for(int i=1;i<=9;i++){

        cin >> b[i];
    }

    for(int i=0;i<m.size( );i++){
        int x=m[i]-'0';
        a[i]=x;
        c[i]=b[x];
        if(c[i]<a[i])
            break;
        if(c[i]>=a[i])
            m[i]=c[i]+'0';

    }
    cout << m << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

int main( )
{
    int n;
    string m;
    int a[20005]={0},b[15]={0};
    int c[20005]={0};
    cin >> n;
    cin >> m;
    for(int i=1;i<=9;i++){

        cin >> b[i];
    }

    for(int i=0;i<m.size( );i++){
        int x=m[i]-'0';
        a[i]=x;
        c[i]=b[x];
        if(c[i]<a[i])
            break;
        if(c[i]>a[i])
            m[i]=c[i]+'0';
        else
            continue;

    }
    cout  << m << endl;
    return 0;
}


	#include<bits/stdc++.h>
	using namespace std;
	int main(){
	    char id[20];
	    long n;
	    int index;
	    bool flag=false;
	    string s;
	    cin>>n>>s;
	    for(int i=1;i<=9;i++){
	        cin>>index;
	        id[i]=index+'0';
	    }
	    for(long i=0;i<n;i++){
	        if(s[i]<id[s[i]-'0']){
	            cout<<id[s[i]-'0'];
	            flag=true;
	        }
	        else{
	            cout<<s[i];
	            if(flag==true)
	            	break;
	        }
	    }
	    return 0;
	}


#include <bits/stdc++.h>
using namespace std;
int n,a[200005],b[10];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%1d",&a[i]); //%1d表示读入一个一位整数
    for(int i=1;i<=9;i++) scanf("%d",&b[i]);
    for(int i=1;i<=n;i++){
        if(a[i]<b[a[i]]){ //一个简单的数学常识：高位越大，低位不管
            for(int j=i;a[j]<=b[a[j]]&&j<=n;j++) //可以替换了就一直换直到还是原来的数大（注意j<=n还是要写不然会RE/TLE）
                a[j]=b[a[j]];
            break; //换完跑路
        }
    }
    for(int i=1;i<=n;i++) printf("%d",a[i]); //输出。。。
}


#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
int a[maxn],n,tot;
char s[maxn];
inline int read(){
	int ret=0;char ch=getchar();
	while (ch<'0'||ch>'9') ch=getchar();
	while (ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return ret;
}
int main(){
	n=read();
	scanf("%s",s+1);
	for (int i=1;i<=9;i++) a[i]=read();
	for (int i=1;i<=n;i++){
		if (a[s[i]-'0']>s[i]-'0'){
			for (int j=i;j<=n;j++){
				if (a[s[j]-'0']>=s[j]-'0') printf("%d",a[s[j]-'0']);
				else{
					for (int k=j;k<=n;k++) printf("%d",s[k]-'0');
					return 0;
				}
			}
			return 0;
		}
		printf("%d",s[i]-'0');
	}
}


#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n,a[200005],b[15];
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%1d",&a[i]);
	}
	for(int i=1;i<=9;i++){
		scanf("%1d",&b[i]);
	}
	for(int i=1;i<=9;i++){
		if(a[i]<b[a[i]]){
			for(int j=i;a[j]<=b[a[j]]&&j<=n;j++){
				a[j]=b[a[j]];
			}
			break;
		}
	}
	for(int i=1;i<=n;i++){
		printf("%d",a[i]);
	}
	return 0;
}


// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
int n,f[20];
char ch[200010];
int read(){
    int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();
    }while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();
    }return x*f;
}
int main()
{
    n=read();
    cin>>(ch+1);
    for(int i=1;i<=9;i++)
        f[i]=read();
    for(int i=1;i<=n;i++){
        int x=ch[i]-'1'+1;
        if(f[x]>x){
            int t=x,j=i;
            while(f[t]>=t&&j<=n)
			{
                ch[j]=(char)(f[t]-1+'1');
                j++;t=ch[j]-'1'+1;
            }
            break;
        }
    }
    cout<<(ch+1);
    return 0;
}


#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
int a[maxn],n,tot;
char s[maxn];
inline int read(){
	int ret=0;char ch=getchar();
	while (ch<'0'||ch>'9') ch=getchar();
	while (ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return ret;
}
int main(){
	n=read();
	scanf("%s",s+1);
	for (int i=1;i<=9;i++) a[i]=read();
	for (int i=1,f=1,flg=0;i<=n;i++){
		if (a[s[i]-'0']>s[i]-'0'&&f) printf("%d",a[s[i]-'0']),flg=1;
		else{printf("%d",s[i]-'0');if (flg) f=0;}
	}
}


var
  s,aaa,ans:ansistring;
  n,i:longint;
  a:array[0..10] of byte;
  flag:boolean;
begin
  readln(n);
  readln(s);
  ans:=s; aaa:=s; flag:=1>2;
  for i:=1 to 9 do read(a[i]);
  for i:=1 to n do
    begin
      if (flag) then
        begin
          if (a[ord(s[i])-48] >= (ord(s[i])-48)) then aaa[i]:=chr(a[ord(s[i])-48]+48)
            else break;
        end
        else
          begin
            if (a[ord(s[i])-48] > (ord(s[i])-48)) then
              begin
                aaa[i]:=chr(a[ord(s[i])-48]+48);
                flag:=2>1;
              end;
          end;
    end;
  if aaa>ans then write(aaa)
    else write(ans);
end.

        aaa:=s;
        for j:=1 to n do
          if (s[j] = s[i]) then aaa[j]:=chr(a[ord(s[j])-48]+48);
        break;


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int n,num[15],a[maxn];
int read(){
	int ret=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
	return ret*f;
}
int main(){
// 	freopen("CF1157B.in","r",stdin);
// 	freopen("CF1157B.out","w",stdout);
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%1d",&a[i]);
	for(int i=1;i<=9;i++) scanf("%d",&num[i]);
	for(int i=0;i<n;i++)
	if(a[i]<num[a[i]]){
		for(int j=i;j<n&&a[j]<=num[a[j]];j++) a[j]=num[a[j]];
		break;
	}
	for(int i=0;i<n;i++) printf("%d",a[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<string>
using namespace std;
map<char,char>m;
char ch[100005];
char f(char c)
{
	return m[c];
}
int main()
{
	int len;
	cin>>len;
	scanf("%s",ch);
	for(char i='1';i<='9';i++)
	{
		char p;
		cin>>p;
		m[i]=p;
	}
	int i;
	for(i=0;i<len;++i)
	{
		if(ch[i]<f(ch[i]))
		break;
	}
	while(i<len)
	{
		ch[i]=f(ch[i]);
		i++;
	}
	printf("%s",ch);
	return 0;
}


#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
int a[maxn],n,tot;
char s[maxn];
inline int read(){
	int ret=0;char ch=getchar();
	while (ch<'0'||ch>'9') ch=getchar();
	while (ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return ret;
}
int main(){
	n=read();
	scanf("%s",s+1);
	for (int i=1;i<=9;i++) a[i]=read();
	for (int i=1,f=1,flg=0;i<=n;i++){
		if (a[s[i]-'0']>s[i]-'0'&&f) printf("%d",a[s[i]-'0']),flg=1;
		else{printf("%d",s[i]-'0');if (flg&&a[s[i]-'0']<s[i]-'0') f=0;}
	}
}


#include<bits/stdc++.h>
using namespace std;
char a[1000005],fom[100];
int n,k;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=9;i++)
		cin>>fom[i];
	for(int i=1;i<=n;i++){
		while(fom[a[i]-'0']>a[i])
			a[i]=fom[a[i]-'0'],k=1,i++;
			if(k==1)
			break;
	}
	for(int i=1;i<=n;i++)
		cout<<a[i];
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define LL long long
using namespace std;
int num[10];
LL n;
int flag;
string s;
int main()
{
	cin>>n;
	cin>>s;
	for(int i=1;i<=9;i++)
		cin>>num[i];
	LL qwq=s.length();
	LL qaq=0;
	while(qaq<=qwq)
	{
		int now1=s[qaq]-'0';
		int now2=num[now1];
		if(now1>now2&&flag==1) break;
		if(now2>now1)
		{
			s[qaq]=num[now1]+'0';
			flag=1;
		}
		qaq++;
	}
	cout<<s<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define For(i,l,r) for(int i=l;i<=r;i++)
#define FOR(i,l,r) for(int i=l;i>=r;i--)
using namespace std;
string s;
int n;
int th[10];

int main(){
	cin>>n;
	cin>>s;
	For(i,1,9)cin>>th[i];
	For(i,0,n-1){
		cout<<char(th[s[i]-'0']+'0')<<endl;
		if(s[i]-'0'<th[s[i]-'0']&&s[i]!='0'){
			For(j,i,n){
				if(s[j]-'0'<th[s[j]-'0']){
					s[j]=th[s[j]-'0']+'0';
				}
				else break;
			}
			cout<<s;
			return 0;
		}

	}

	cout<<s;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
char c[200001];
int a[11];
bool f=0;
int main(){
	cin>>n;
	cin>>c;
	for(int i=1;i<=9;i++) cin>>a[i];
	for(int i=0;i<n;i++){
		if(!f&&c[i]-'0'<=a[c[i]-'0']){
			while(c[i]-'0'<=a[c[i]-'0']&&i<n){
				cout<<a[c[i]-'0'];
				i++;
			}
			i--;
			f=1;
		}
		else cout<<c[i];
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[1000005],fom[100];
int n,k;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=9;i++)
		cin>>fom[i];
	for(int i=1;i<=n;i++){
		while(fom[a[i]-'0']>=a[i]]&&i<=n)
			a[i]=fom[a[i]-'0'],k=1,i++;
			if(k==1)
			break;
	}
	for(int i=1;i<=n;i++)
		cout<<a[i];
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+100;
int a[maxn],b[maxn];
int main()
{
	int n,i,j;string s;
	cin>>n>>s;
	for(i=0;i<n;i++)
	a[i+1]=s[i]-'0';
	for(i=1;i<=9;i++)
	{
		int x;cin>>x;
		b[i]=x;
	}
	int t=0,x=1,y=n;
	for(i=1;i<=n;i++)
	{
		if(b[a[i]]>a[i]&&t==0)
		{
			t++;x=i;break;
		}
	}
	for(i=x;i<=n;i++)
	{
		if(b[a[i]]<a[i])
		{
			y=i-1;
			break;
		}
	}

	for(i=1;i<x;i++)
	{
		cout<<a[i];
	}
	for(i=x;i<=y;i++)
	cout<<b[a[i]];
	for(i=y+1;i<=n;i++)
	cout<<a[i];
	return 0;
}
/*
7
1 3 5 6 7 4 2

*/


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
char c[200001];
int a[11];
bool f=0;
int main(){
	cin>>n;
	cin>>c;
	for(int i=1;i<=9;i++) cin>>a[i];
	for(int i=0;i<n;i++){
		if(!f&&c[i]-'0'<a[c[i]-'0']){
			while(c[i]-'0'<=a[c[i]-'0']&&i<n){
				cout<<a[c[i]-'0'];
				i++;
			}
			i--;
			f=1;
		}
		else cout<<c[i];
	}
	return 0;
}


#include <iostream>
using namespace std;
int a[200005];
int main()
{
	int num,change[12];
	cin>>num;
	string str;
	cin>>str;
	for(int i=1;i<=9;i++)
	{
		cin>>change[i];
	}
	for(int i=0;i<num;i++)
	{
		int temp=str[i-1]-'0';
		if(temp<change[temp])
		{
			for(int j=i;str[j-1]-'0'<=change[str[j-1]-'0']&&j<=num;j++)
			{
				str[j-1]=change[str[j-1]-'0']+'0';
			}
			break;
		}
	}
	cout<<str;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[1000005],fom[100];
int n,k;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=9;i++)
		cin>>fom[i];
	for(int i=1;i<=n;i++){
		while(fom[a[i]-'0']>=a[i]&&i<=n)
			a[i]=fom[a[i]-'0'],k=1,i++;
			if(k==1)
			break;
	}
	for(int i=1;i<=n;i++)
		cout<<a[i];
}


#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
int num[10];
LL n;
bool flg;
string s;
int main()
{
	cin>>n;
	cin>>s;
	for(int i=1;i<=9;i++)
		cin>>num[i];
	LL qwq=s.length();
	LL qaq=0;
	while(qaq<=qwq)
	{
		int now1=s[qaq]-'0';
		int now2=num[now1];
		if(now2>now1)
		{
			for(int i=qaq;i<=qwq;i++)
			{
				if(now1>now2)
				{
					qaq=i;
					break;
				}
				s[i]=now2+'0';
			}
			break;
		}
		qaq++;
	}
	cout<<s<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define For(i,l,r) for(int i=l;i<=r;i++)
#define FOR(i,l,r) for(int i=l;i>=r;i--)
using namespace std;
string s;
int n;
int th[10];

int main(){
cout<<"1557";

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#define maxn 2005

using namespace std;

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;
    string s;cin>>s;
    int a[10];
    for(int i=1;i<=9;i++) cin>>a[i];
    int i=0;

    for(;i<n;i++){
        int tt=s[i]-'0';
        if(tt>=a[tt]) continue;
        else break;
    }

    for(;i<n;i++){
        int tt=s[i]-'0';
        if(tt>a[tt]) break;
        s[i]=(a[tt]+'0');

    }
    for (int i=0; i<n; i++) {
        cout<<s[i];
    }

    return 0;

}

    /*  char a[500][500];
    for(int i=0;i<100;i++){
    for(int j=0;j<100;j++){

    if(i>=20&&i<=25&&j>20&&j<=25){
    a[i][j]='@';
    continue;
    }

    int f=rand()%2;
    if(f) a[i][j]='*';
    else a[i][j]=' ';

    }
    }
    for(int i=0;i<100;i++){
    for(int j=0;j<100;j++){

    cout<<a[i][j];
    }
    cout<<endl;
    }*/


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char ss[200000];
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<=a[j]) ss[i]=a[j]+'0';
		else ss[i]=j+'0';
	}
	for(int i=0;i<n;i++)
	{
		cout<<ss[i];
	}
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	bool vis=false;
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0', vis=true;
		else if(j>a[j]&&vis==true)
		    break;
	}
		cout<<s;
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0';
		else if(j>a[j])
		    break;
	}
		cout<<s;
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char ss[200000];
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<=a[j])
		   ss[i]=a[j]+'0';
		else ss[i]=j+'0';
	}
	for(int i=0;i<n;i++)
	{
		cout<<ss[i];
	}
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<=a[j])
		   s[i]=a[j]+'0';
		else s[i]=j+'0';
	}
	for(int i=0;i<n;i++)
	{
		cout<<ss[i];
	}
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	bool vis=false;
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0', vis=true;
		else if(j>a[j]&&vis)
		    break;
	}
		cout<<s;
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0';
		else if(j>a[j])
		    break;
		cout<<s[i];
	}
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0';
		else if(j>a[j])
		    break;
		cout<<s;
	}
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0';
		else if(j>a[j])
		    break;
	}
	for(int i=0;i<n;i++)
	{
		cout<<s[i];
	}
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	bool vis=false;
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0';

	}
		cout<<s;
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char ss[200000];
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<=a[j])
		   ss[i]=a[j]-'0';
		else ss[i]=j-'0';
	}
	for(int i=0;i<n;i++)
	{
		cout<<ss[i];
	}
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
	int n;
	char s[200000];
	cin>>n;
	cin>>s;
	int a[10];
	bool vis=false
	for(int i=1;i<=9;i++)
	{
		cin>>a[i];
	}
    for(int i=0;i<n;i++)
	{
		int j=s[i]-'0';
		if(j<a[j])
		   s[i]=a[j]+'0', vis=true;
		else if(j>a[j]&&vis)
		    break;
	}
		cout<<s;
}


#include<bits/stdc++.h>
using namespace std;
#define M 200020
int f[10];
char a[M];
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    cin>>a;
    for(int i=1;i<=9;++i)
    {
        cin>>f[i];
    }
    int flag=0;
    int t=0;
    for(int i=0;i<n;++i)
    {
        if(flag!=i+1)
            break;
            t=1;
        if(f[a[i]-'0']>a[i]-'0')
        {
            a[i]=f[a[i]-'0']+'0';
            flag=i;
        }
    }
    for(int i=0;i<n;++i)
    {
        cout<<a[i];
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 200020
int f[10];
char a[M],b[M];
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    cin>>a;
    for(int i=1;i<=9;++i)
    {
        cin>>f[i];
    }
    int flag=0,i=0;
    for(i=0;i<n;++i)
    {
        if(a[i-1]!=a[i]&&flag==1&&i!=0)
            break;
        if(f[a[i]-'0']>a[i]-'0')
        {
            b[i]=f[a[i]-'0']+'0';
            flag=1;
        }
        else{b[i]=a[i];}
    }

    for(;i<n;++i)
    {
        b[i]=a[i];
    }
    for(int i=0;i<n;++i)
    {
        cout<<b[i];
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
map < int , int >T;
int main()
{
    int n;
    bool s=true;
    cin>>n;
    char ans[200005];
    for(int i=0;i<n;i++) cin>>ans[i];
    for(int i=1;i<10;i++) cin>>T[i];
    for(int i=0;i<n;i++)
    {
        while(T[ans[i]-'0']>ans[i]-'0'&&i<n)
        {
            ans[i]=T[ans[i]-'0']+'0';
            s=false;
            i++;
        }
        if(s==false) break;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
map < int , int >T;
char ans[200005];
int main()
{
    int n;
    bool s=true;
    cin>>n;

    for(int i=0;i<n;i++) cin>>ans[i];
    ans[n]='\0';
    for(int i=1;i<10;i++) cin>>T[i];
    for(int i=0;i<n;i++)
    {
        while(T[ans[i]-'0']>=ans[i]-'0'&&i<n)
        {
            ans[i]=T[ans[i]-'0']+'0';
            s=false;
            i++;
        }
        if(s==false) break;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define M 200020
int f[10];
char a[M];
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    cin>>a;
    for(int i=1;i<=9;++i)
    {
        cin>>f[i];
    }
    for(int i=0;i<n;++i)
    {
        if(f[a[i]-'0']>a[i]-'0')
        {
            a[i]=f[a[i]-'0']+'0';
        }
    }
    for(int i=0;i<n;++i)
    {
        cout<<a[i];
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
map < int , int >T;
char ans[200005];
int main()
{
    int n;
    bool s=true;
    cin>>n;

    for(int i=0;i<n;i++) cin>>ans[i];
    ans[n]='\0';
    for(int i=1;i<10;i++) cin>>T[i];
    for(int i=0;i<n;i++)
    {
        while(T[ans[i]-'0']>ans[i]-'0'&&i<n)
        {
            ans[i]=T[ans[i]-'0']+'0';
            s=false;
            i++;
        }
        if(s==false) break;
    }
    cout<<ans;
}


////最小生成树
////用最少的成本修路,让所有的城市连起来;
//#include<bits/stdc++.h>
//using namespace std;
//int root[10005],c,n,m,sum;
//struct Edge
//{
//    int u,v,w;
//};
//Edge edge[10005];
//int get_root(int n){return root[n]==n? n:root[n]=get_root(root[n]);}
//bool cmp(const Edge a,const Edge b){return a.w<b.w;}
//int main()
//{
//    while(cin>>c>>n>>m){
//        sum=0;
//        for(int i=0;i<10005;++i)
//            root[i]=i;
//        for(int i=0;i<n;++i){
//            cin>>edge[i].u>>edge[i].v>>edge[i].w;
//        }
//        ///可有可无
//      //  if(n < m*(m-1)/2){
//       //     cout<<"No"<<endl;
//     //       continue;
//     //   }
//        sort(edge,edge+n,cmp);
//        for(int i=0;i<n;++i){
//            if(get_root(edge[i].u)!=get_root(edge[i].v)){
//                root[get_root(edge[i].u)]=get_root(edge[i].v);
//                sum+=edge[i].w;
//            }
//        }
//        if(sum<=c)cout<<"Yes"<<endl;
//        else cout<<"No"<<endl;
//
//    }
//    return 0;
//}
#include<bits/stdc++.h>
using namespace std;
#define M 200020
int f[10];
char a[M];
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    cin>>a;
    for(int i=1;i<=9;++i)
    {
        cin>>f[i];
    }
    int flag=0;
    char t=-1;
    for(int i=0;i<n;++i)
    {
        if(flag==i+1)
            break;
        if(f[a[i]-'0']>a[i]-'0')
        {
            t=a[i];
            a[i]=f[a[i]-'0']+'0';
            flag=i;
        }
    }
    for(int i=0;i<n;++i)
    {
        cout<<a[i];
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
map < int , int >T;
char ans[200005];
int main()
{
    int n;
    bool s=true;
    cin>>n;

    for(int i=0;i<n;i++) cin>>ans[i];
    ans[n]='\0';
    for(int i=1;i<10;i++) cin>>T[i];
    for(int i=0;i<n;i++)
    {
        while(T[ans[i]-'0']>ans[i]-'0'&&i<n)
        {
            ans[i]=T[ans[i]-'0']+'0';
            s=false;
            i++;
            while(T[ans[i]-'0']==ans[i]-'0'&&i<n) i++;
        }
        if(s==false) break;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int n,x[200005];
int main()
{
    queue < char > a;
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    int ma=-1,ans=0;
    int l=0,r=n-1;
    while(r>=l)
    {
        if(x[l]<x[r])
        {
            if(x[l]<=ma)
            {
                int r2=r,nr=0;
                while(r2>l&&x[r2]<x[r2-1])
                {
                    r2--;
                    nr++;
                }
                nr++;
                ans+=nr;
                while(nr--) a.push('R');
                break;
            }
            a.push('L');
            ma=x[l];
            l++;
            ans++;
            continue;
        }
        if(x[l]>x[r])
        {
            if(x[r]<=ma)
            {
                int l2=l,nl=0;
                while(l2<r&&x[l2]<x[l2+1])
                {
                    l2++;
                    nl++;
                }
                nl++;
                ans+=nl;
                while(nl--) a.push('L');
                break;
            }
            a.push('R');
            ma=x[r];
            r--;
            ans++;
            continue;
        }
        if(x[l]==x[r]&&x[l]>ma)
        {
            int nl=0,nr=0,l2=l,r2=r;
            while(r2>l&&x[r2]<x[r2-1])
            {
                r2--;
                nr++;
            }
            while(l2<r&&x[l2]<x[l2+1])
            {
                l2++;
                nl++;
            }
            if(nl>=nr)
            {
                nl++;
                ans+=nl;
                while(nl--) a.push('L');
            }
            else
            {
                nr++;
                ans+=nr;
                while(nr--) a.push('R');
            }

        }
        break;
    }
    cout<<ans<<endl;
    while(!a.empty())
    {
        cout<<a.front();
        a.pop();
    }
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>=i)rep[i]=1;
    }
    int i;
    for(i=0;i<n&&rep[s[i]-'0']!=1&&a[s[i]-'0']>s[i]-'0';i++){cout<<s[i]-'0';}
    while(rep[s[i]-'0']&&i<n){cout<<a[s[i]-'0'];i++;}
    while(i<n){cout<<s[i]-'0';i++;}
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>i)rep[i]=1;
    }
    for(int i=0;s[i]!=0;i++){
        if(rep[s[i]-'0']==1)cout<<a[s[i]-'0'];
        else cout<<s[i]-'0';
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>i)rep[i]=1;
    }
    int i;
    for(i=0;i<n&&rep[s[i]-'0']!=1;i++){cout<<s[i]-'0';}
    while(rep[s[i]-'0']&&i<n||a[s[i]-'0']==s[i]-'0'){
        cout<<a[s[i]-'0'];
        i++;
    }
    while(i<n){cout<<s[i]-'0';i++;}
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>i)rep[i]=1;
    }
    int i;
    for(i=0;i<n&&rep[s[i]-'0']!=1;i++){cout<<s[i]-'0';}
    while(rep[s[i]-'0']&&i<n){
        if(a[s[i]-'0']>s[i]-'0')cout<<a[s[i]-'0'];
        else cout<<s[i]-'0';
        i++;
    }
    while(i<n){cout<<s[i]-'0';i++;}
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>=i)rep[i]=1;
    }
    int i;
    for(i=0;i<n&&rep[s[i]-'0']!=1;i++){cout<<s[i]-'0';}
    while(rep[s[i]-'0']&&i<n){cout<<a[s[i]-'0'];i++;}
    while(i<n){cout<<s[i]-'0';i++;}
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>=i)rep[i]=1;
    }
    int i;
    for(i=0;s[i]!=0&&rep[s[i]-'0']!=1;i++){cout<<s[i]-'0';}
    while(rep[s[i]-'0']&&s[i]!=0){cout<<a[s[i]-'0'];i++;}
    while(s[i]!=0){cout<<s[i]-'0';i++;}
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>=i)rep[i]=1;
    }
    int i;
    for(i=0;i<n&&rep[s[i]-'0']!=1&&a[s[i]-'0']!=s[i]-'0';i++){cout<<s[i]-'0';}
    while(rep[s[i]-'0']&&i<n){cout<<a[s[i]-'0'];i++;}
    while(i<n){cout<<s[i]-'0';i++;}
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>i)rep[i]=1;
    }
    for(int i=0;s[i]!=0;i++){
        if(rep[s[i]-'0']==1)cout<<a[s[i]-'0'];
        else cout<<s[i];
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int a[10]={0};
    int rep[10]={0};
    for(int i=1;i<=9;i++){
        cin>>a[i];
        if(a[i]>i)rep[i]=1;
    }
    int i;
    for(i=0;s[i]!=0&&rep[s[i]-'0']!=1;i++){cout<<s[i]-'0';}
    while(rep[s[i]-'0']&&s[i]!=0){cout<<a[s[i]-'0'];i++;}
    while(s[i]!=0){cout<<s[i]-'0';i++;}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and check)
			continue;
		if(f[n] > n and !check){
			begin = i;
			check = true;
		}
		else if(f[n] < n and check){
			end = i-1;
			if(begin == end)
				end++;
			check = false;
			break;
		}
	}
	if(check)
		end = digit-1;
	string test = change(num,begin,end);
	for(int i=0;i<test.size();i++){
		if(test[i] == num[i])
			continue;
		if(test[i] > num[i]){
			cout << test;
			break;
		}
		else{
			cout << num;
			break;
		}
	}
}
/*
10
1111111113
1 1 9 1 1 1 1 1 1
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

void change(string change,int x,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[x] + '0';
	cout << x << "-->" << change << endl;
	lis.push_back(change);
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	for(int i=0;i<digit;i++){
		if(num[i] != num[i+1] and begin != end){
			change(num,num[i]-'0',begin,end);
			begin = end = i+1;
		}
		else if(num[i] != num[i+1] and begin == end){
			begin = end = i+1;
		}
		else{
			end++;
		}
	}
	lis.push_back(num);
	sort(lis.begin(),lis.end());
	cout << lis[lis.size()-1];
}
/*
12
111222111333
9 9 9 9 9 9 9 9 9
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

void change(string change,int x,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[x] + '0';
	lis.push_back(change);
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	for(int i=0;i<digit;i++){
		if(num[i] != num[i+1] and begin!=end){
			change(num,num[i]-'0',begin,end);
			begin = end = i+1;
		}
		else{
			end++;
		}
	}
	lis.push_back(num);
	/*for(auto c : lis)
		cout << c << endl;*/
	sort(lis.begin(),lis.end());
	cout << lis[lis.size()-1];
}
/*
12
111222111333
9 9 9 9 9 9 9 9 9
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	//cout << x << "-->" << change << endl;
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and !check){
			begin = i;
			break;
		}
	}
	for(int i=begin+1;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] < n){
			end = i-1;
			break;
		}
	}
	if(check)
		end = digit-1;
	cout << change(num,begin,end);
}
/*
10
1222111333
9 9 9 9 9 9 9 9 9
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

void change(string change,int x,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[x] + '0';
	lis.push_back(change);
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	for(int i=0;i<digit;i++){
		if(num[i] != num[i+1]){
			change(num,num[i]-'0',begin,end);
			begin = end = i+1;
		}
		else{
			end++;
		}
	}
	lis.push_back(num);
	sort(lis.begin(),lis.end());
	cout << lis[lis.size()-1];
}


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	//cout << x << "-->" << change << endl;
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and check)
			continue;
		if(f[n] > n and !check){
			begin = i;
			check = true;
		}
		else if(f[n] < n and check){
			end = i-1;
			if(begin == end)
				end++;
			check = false;
			break;
		}
	}
	if(check)
		end = digit-1;
	cout << change(num,begin,end);
}
/*
10
1222111333
9 9 9 9 9 9 9 9 9
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	//cout << x << "-->" << change << endl;
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and !check){
			begin = i;
			check = true;
		}
		else if(check and f[n] < n){
			end = i-1;
			check = false;
			break;
		}
	}
	if(check)
		end = digit-1;
	cout << change(num,begin,end);
}
/*
10
1222111333
9 9 9 9 9 9 9 9 9
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

void change(string change,int x,int ind){
	int begin = 0;
	for(int i=ind-1;i>=0;i--){
		if(change[i]!=change[i+1]){
			begin = i+1;
			break;
		}
	}
	for(int i=begin;i<=ind;i++)
		change[i] = f[x] + '0';
	lis.push_back(change);
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	for(int i=0;i<digit;i++){
		if(num[i] != num[i+1]){
			change(num,num[i]-'0',i);
		}
	}
	lis.push_back(num);
	sort(lis.begin(),lis.end());
	cout << lis[lis.size()-1];
}


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

void change(string change,int x,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[x] + '0';
	//cout << x << "-->" << change << endl;
	lis.push_back(change);
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	for(int i=0;i<digit;i++){
		if(num[i] != num[i+1] and begin != end){
			change(num,num[i]-'0',begin,end);
			begin = end = i+1;
		}
		else if(num[i] != num[i+1] and begin == end){
			begin = end = i+1;
		}
		else{
			end++;
		}
	}
	lis.push_back(num);
	sort(lis.begin(),lis.end());
	cout << lis[lis.size()-1];
}
/*
10
1222111333
9 9 9 9 9 9 9 9 9
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false,pass = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and check)
			continue;
		if(f[n] > n and !check){
			pass = true;
			begin = i;
			check = true;
		}
		else if(f[n] < n and check){
			end = i-1;
			check = false;
			break;
		}
	}
	if(!pass){
		cout << num;
		return 0;
	}
	if(check)
		end = digit-1;
	//cout << begin << " " << end << endl;
	cout << change(num,begin,end);

}
/*
6
113113
1 1 9 1 1 1 1 1 1
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

int convert(string s){
	int ind = s.size()-1;
	int out = 0;
	for(int i=0;i<ind;i++){
		out += s[i]*pow(10,ind-i);
	}
	return out;
}

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	//cout << x << "-->" << change << endl;
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and check)
			continue;
		if(f[n] > n and !check){
			begin = i;
			check = true;
		}
		else if(f[n] < n and check){
			end = i-1;
			if(begin == end)
				end++;
			check = false;
			break;
		}
	}
	if(check)
		end = digit-1;
	if(convert(num) < convert(change(num,begin,end)))
		cout << change(num,begin,end);
	else
		cout << num;
}
/*
10
1222111333
1 1 1 1 1 1 1 1 1
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and check)
			continue;
		if(f[n] > n and !check){
			begin = i;
			check = true;
		}
		else if(f[n] < n and check){
			end = i-1;
			check = false;
			break;
		}
	}
	if(check)
		end = digit-1;
	//cout << begin << " " << end << endl;
	string test = change(num,begin,end);
	for(int i=0;i<test.size();i++){
		if(test[i] == num[i])
			continue;
		if(test[i] > num[i]){
			cout << test;
			break;
		}
		else{
			cout << num;
			break;
		}
	}
}
/*
3
1357
1 1 1 1 1 1 1 1 1
*/


#include <bits/stdc++.h>

using namespace std;

string num;
int digit;
int	f[10];
vector<string> lis;

string change(string change,int b,int e){
	for(int i=b;i<=e;i++)
		change[i] = f[change[i]-'0'] + '0';
	//cout << x << "-->" << change << endl;
	return change;
}

int main(){
	scanf("%d",&digit);
	cin >> num;
	for(int i=1;i<=9;i++)
		scanf("%d",&f[i]);
	int begin = 0,end = 0;
	bool check = false;
	for(int i=0;i<digit;i++){
		int n = num[i]-'0';
		if(f[n] > n and check)
			continue;
		if(f[n] > n and !check){
			begin = i;
			check = true;
		}
		else if(f[n] < n and check){
			end = i-1;
			check = false;
			break;
		}
	}
	if(check)
		end = digit-1;
	cout << change(num,begin,end);
}
/*
10
1222111333
9 9 9 9 9 9 9 9 9
*/


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	int i=0;
	int dx=-1;
	int dy=-1;
	int d=0;
	for(int y=1;y<=n;y++)
	{
		if(e[y]-'0'<a[e[y]-'0']&&i==0)
		{
			dx = y-1;
		i++;
		}
		if(i==1&&e[y]-'0'>a[e[y]-'0'])
		{
				d++;
		dy = y-1;break;

		}
	}
	if(d==0) dy = n;
	if(dx!=-1)
	{
		if(dx==0) dx=1;
	for(dx;dx<=dy;dx++)
	e[dx] = a[e[dx]-'0']+'0';
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	int i=0;
	int dx=-1;
	int dy=-1;
	int d=0;
	for(int y=1;y<=n;y++)
	{
		if(e[y]-'0'<a[e[y]-'0']&&i==0)
		{
			dx = y-1;
		i++;
		}
		if(i==1&&e[y]-'0'>a[e[y]-'0'])
		{
				d++;
		dy = y-1;break;

		}
	}
	if(d==0) dy = n;
	if(dx!=-1){

	for(dx;dx<=dy;dx++)
	e[dx] = a[e[dx]-'0']+'0';}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	string b;
	int i=0;
	for(int y=1;y<=n;y++)
	{
		int t = e[y]-'0';
		if(a[t]>=t)
		{
			if(q[y]==0)
			{
			for(int u=y;u<=n;u++)
			{
				if(e[u]-'0'==t) {
				e[u] = a[t]+'0';
				q[u]++;
				}
			}
			}
			i++;
		}
		else {
			if(i) break;}
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	string b;
	int i=0;
	int d ;
	int t=n;
	for(int y=1;y<=t;y++)
	{
		int t = e[y]-'0';
		if(a[t]>t)
		{
			if(q[y]==0)
			{
				i++;
			for(int u=y;u<=t;u++)
			 {
				if(e[u]-'0'==t)
				{
				e[u] = a[t]+'0';
				q[u]++;
				d = u;
				}
			  }
			}
		}
		if(i==1) {
		t = d+1;
		if(t>n) t = n;}
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	string b;
	int i=0;
	int d ;
	int t=n;
	for(int y=1;y<=t;y++)
	{
		int t = e[y]-'0';
		if(a[t]>t)
		{
			if(q[y]==0)
			{
			for(int u=y;u<=n;u++)
			 {
				if(e[u]-'0'==t)
				{
				e[u] = a[t]+'0';
				q[u]++;
				d = u;
				}
			  }
			}
		}
		t = d;
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	string b;
	int i=0;
	int d ;
	for(int y=1;y<=n;y++)
	{
		int t = e[y]-'0';
		if(a[t]>=t)
		{
			if(q[y]==0)
			{
			for(int u=y;u<=n;u++)
			 {
				if(e[u]-'0'==t)
				{
				e[u] = a[t]+'0';
				q[u]++;
				d = u;
				}
			  }
			}
		}
		n = d;
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	int i=0;
	int dx=-1;
	int dy=-1;
	int d=0;
	for(int y=1;y<=n;y++)
	{
		if(e[y]-'0'<a[e[y]-'0']&&i==0)
		{
			dx = y;
		i++;
		}
		if(i>=1&&e[y]-'0'>a[e[y]-'0'])
		{
				d++;
		dy = y-1;break;

		}
	}
	if(d==0) dy = n;
	if(dx!=-1)
	{
		if(dx==0) dx=1;
	for(dx;dx<=dy;dx++)
	e[dx] = a[e[dx]-'0']+'0';
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	int i=0;
	int dx=-1;
	int dy=-1;
	int d=0;
	for(int y=1;y<=n;y++)
	{
		if(e[y]-'0'<a[e[y]-'0']&&i==0)
		{
			dx = y-1;
		i++;
		}
		if(i>=1&&e[y]-'0'>a[e[y]-'0'])
		{
				d++;
		dy = y-1;break;

		}
	}
	if(d==0) dy = n;
	if(dx!=-1)
	{
		if(dx==0) dx=1;
	for(dx;dx<=dy;dx++)
	e[dx] = a[e[dx]-'0']+'0';
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include <iostream>
#include <string>
#include <string.h>
#define ll long long
#include <set>
#include <algorithm>
using namespace std;
int a[10];
const int MAXN = 2*1e5+2;
bool q[MAXN];
int main()
{
	int n;cin>>n;
	string e; cin>>e;
	e = "0"+e;
	for(int y=1;y<=9;y++)
	cin>>a[y];
	string b;
	int i=0;
	for(int y=1;y<=n;y++)
	{
		int t = e[y]-'0';
		if(a[t]>=t)
		{
			if(q[y]==0)
			{
			for(int u=y;u<=n;u++)
			{
				if(e[u]-'0'==t) {
				e[u] = a[t]+'0';
				q[u]++;
				}
			}
			}
		}
	}
	for(int y=1;y<=n;y++)
	cout<<e[y];

}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a;
	for(int i=0;i<n;i++) b[i+1]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t=0,r=0,x=0;
	for(int i=1;i<=n;i++){
		if(b[i]<f[b[i]]) {x=1;t=b[i];r=f[b[i]];break;}
	}
	if(x) for(int i=1;i<=n;i++){
		if(b[i]==t) b[i]=r;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	return 0;
}


#include <bits/stdc++.h>
#define NN 200011

using namespace std;

int n,cnt;
char str[NN],s[NN];
int a[11];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	cin>>n;
	cin>>str;
	for(int i=0; i<9; i++) cin>>a[i];
	int len=strlen(str);
	for(int i=0; i<len; i++) s[cnt++]=a[str[i]-'0'-1]+'0';
	s[cnt]=0;
	puts(s);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define NN 200010
int a[NN];
int f[NN];
char s[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1;i<=9;i++) scanf("%d",&f[i]);
	int st=0,flag=0;
	for(int i=1;i<=n;i++){
		int x=s[i]-'0';
		if(x>=f[x]) printf("%d",x);
		else{
			st=i;
			flag=1;
			break;
		}
	}
	if(!flag) return 0;
	flag=0;
	for(int i=st;i<=n;i++){
		int x=s[i]-'0';
		if(x<=f[x]) printf("%d",f[x]);
		else{
			st=i;
			flag=1;
			break;
		}
	}
	if(!flag) return 0;
	for(int i=st;i<=n;i++){
		int x=s[i]-'0';
		printf("%d",x);
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a;
	for(int i=0;i<n;i++) b[i+1]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t=0,r=0,x=0;
	for(int i=1;i<=n;i++){
		if(b[i]<f[b[i]]) b[i]=f[b[i]],x=1;
		else if(x==1) break;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	cout<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a;
	for(int i=0;i<n;i++) b[i+1]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t=0,r=0,x=0;
	for(int i=1;i<=n;i++){
		if(b[i]<=f[b[i]]) b[i]=f[b[i]],x=1;
		else if(x==1) break;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	cout<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a;
	for(int i=0;i<n;i++) b[i+1]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t=0,r=0,f=0;
	for(int i=1;i<=n;i++){
		if(b[i]<f[b[i]]) b[i]=f[b[i]],f=1;
		else if(f==1) break;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	cout<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a+1;
	for(int i=1;i<=n;i++) b[i]=a[i]-'0',cerr<<b[i];
	for(int i=1;i<=9;i++) cin>>f[i];
	for(int i=1;i<=n;i++){
		if(b[i]<=f[b[i]]) b[i]=f[b[i]];
		else break;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a;
	for(int i=0;i<n;i++) b[i+1]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	for(int i=1;i<=n;i++){
		if(b[i]<=f[b[i]]) b[i]=f[b[i]];
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a+1;
	for(int i=1;i<=n;i++) b[i]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t=0,r=0;
	for(int i=1;i<=n;i++){
		if(b[i]<f[b[i]]) {
			t=b[i];
			r=f[b[i]];
			break;
		}
	}
	for(int i=1;i<=n;i++){
		if(b[i]==t) b[i]=r;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
typedef vector<int> VI;

#define NN 200200

int a[NN], f[222];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; scanf("%d", &n);
	for(int i=0; i<n; i++) scanf("%1d", a+i);
	for(int i=1; i<=9; i++) scanf("%d", f+i);

	for(int i=0; i<n; i++) {
		int u=a[i];
		if(f[u]>u) {
			int j=0;
			for(; j<i; j++) printf("%d", a[j]);
			for(; j<n; j++) {
				int v=a[j];
				if(f[v]>v) printf("%d", f[v]);
				else break;
			}
			for(; j<n; j++) printf("%d", a[j]);
			return 0;
		}
	}
	for(int i=0; i<n; i++) printf("%d", a[i]);
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef pair<int, int> pii;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
typedef vector<int> VI;

#define NN 200200

int a[NN], f[222];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; scanf("%d", &n);
	for(int i=0; i<n; i++) scanf("%1d", a+i);
	for(int i=1; i<=9; i++) scanf("%d", f+i);

	for(int i=0; i<n; i++) {
		int u=a[i];
		if(f[u]>u) {
			int j=0;
			for(; j<i; j++) printf("%d", a[j]);
			for(; j<n; j++) {
				int v=a[j];
				if(f[v]>=v) printf("%d", f[v]);
				else break;
			}
			for(; j<n; j++) printf("%d", a[j]);
			return 0;
		}
	}
	for(int i=0; i<n; i++) printf("%d", a[i]);
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a+1;
	for(int i=1;i<=n;i++) b[i]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	for(int i=1;i<=n;i++) b[i]=max(b[i],f[b[i]]);
	for(int i=1;i<=n;i++) cout<<b[i];
	return 0;
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define sq(x) ((x)*(x))
#define x first
#define y second
#define eps 1e-9
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define bit(x, y) (((x)>>(y))&1)
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctz(x) (__builtin_ctz(x))
#define bctzl(x) (__builtin_ctzll(x))
#define sgn(x) (((x)>eps)-((x)<-eps))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}
template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}
template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}
template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}
template<class T>inline void printsp(T x) {print(x); putchar(' ');}
template<class T>inline void println(T x) {print(x); putchar('\n');}
template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}
template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

char s[211111];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n;
	cin>>n>>s;
	int f[11];
	for (int i=1; i<10; i++) cin>>f[i];

	for (int i=0; i<n; i++) s[i]-='0';
	for (int i=0; i<n; i++) {
	    if (f[s[i]]>s[i]) {
	        for (int j=i; j<n; j++) {
	            if (f[s[j]]>=s[j]) {
	                s[j]=f[s[j]];
                } else break;
            }
            break;
        }
    }
    for (int i=0; i<n; i++) s[i]+='0';
    puts(s);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a+1;
	for(int i=1;i<=n;i++) b[i]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t,r;
	for(int i=1;i<=n;i++){
		if(b[i]<f[b[i]]) {
			t=b[i];
			r=f[b[i]];
			break;
		}
	}
	for(int i=1;i<=n;i++){
		if(b[i]==t) b[i]=r;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a;
	for(int i=0;i<n;i++) b[i+1]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t=0,r=0;
	for(int i=1;i<=n;i++){
		if(b[i]<f[b[i]]) b[i]=f[b[i]];
	}
	for(int i=1;i<=n;i++) a[i-1]=b[i]+'0';
	cout<<a;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a;
	for(int i=0;i<n;i++) b[i+1]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	int t=0,r=0,x=0;
	for(int i=1;i<=n;i++){
		if(b[i]<f[b[i]]) b[i]=f[b[i]],x=1;
		else {
			if(b[i]==f[b[i]]) continue;
			if(x==1) break;
		}
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define NN 200011

using namespace std;

int n,cnt;
char str[NN],s[NN];
int a[11];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	cin>>n;
	cin>>str;
	for(int i=0; i<9; i++) cin>>a[i];
	int len=strlen(str);
	for(int i=0; i<len; i++)
		s[cnt++]=max(str[i]-'0',a[str[i]-'0'-1])+'0';
	s[cnt]=0;
	puts(s);
	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
#define _ls (u<<1)
#define _rs (_ls|1)
#define _mid (L+R>>1)
#define pb push_back
#define sq(x) ((x)*(x))
#define SZ(x) ((x).size())
#define _get(l,r) (l+r|l!=r)
#define sgn(a) ((a>eps)-(a<-eps))
#define bit(x,y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii,vector<pii>,greater<pii> >

using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;

template<typename T, typename U> inline void smin(T &a, const U &b) {if (a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if (a<b) a=b;}

#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline bool print(char &x) {putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007,MOD=998244353;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=202020;
char s[NN];
int f[100];
int flag[NN];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
//  freopen("in.in", "w", stdout);
#endif

  int n;cin>>n;
  gn(s);
  for(int i=1; i<10; i++) gn(f[i]);
  for(int i=0; i<n; i++){
  	if(f[s[i]-'0']>s[i]-'0') flag[i]=1;
  	if(f[s[i]-'0']<s[i]-'0') flag[i]=-1;
  }

  int st=-1,ed=-1;
  for(int i=0; i<n; i++){
  	if(st==-1){
  		if(flag[i]==1) st=ed=i;
  	}else{
  		if(flag[i]==-1) {ed=i-1;break;}
  		else ed=i;
  	}
  }
  for(int i=0; i<n; i++){
  	if(st<=i && i<=ed) putchar('0'+f[s[i]-'0']);
  	else putchar(s[i]);
  }
  puts("");
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[200200];
int b[200200],f[20];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	cin>>a+1;
	for(int i=1;i<=n;i++) b[i]=a[i]-'0';
	for(int i=1;i<=9;i++) cin>>f[i];
	for(int i=1;i<=n;i++){
		if(b[i]<=f[b[i]]) b[i]=f[b[i]];
		else continue;
	}
	for(int i=1;i<=n;i++) cout<<b[i];
	return 0;
}


using System;
using System.IO;
using System.Text;

namespace Ksusha_the_Squirrel
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int n = Next();
            int k = Next();

            int count = 0;
            bool ok = true;
            for (int i = 0; i < n; i++)
            {
                if (Next() == 1)
                {
                    count++;
                    if (count == k)
                    {
                        ok = false;
                        break;
                    }
                }
                else
                    count = 0;
            }

            writer.WriteLine(ok ? "YES" : "NO");
            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == '.')
                    return 0;
                if (c == '#')
                    return 1;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Security.Principal;
using System.Text;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Collections;
using System.Text.RegularExpressions;

namespace Task
{

    #region MyIo

    internal class MyIo : IDisposable
    {
#if  LOCAL_TEST
        private readonly TextReader inputStream = new StreamReader("input.txt");
#else
        private readonly TextReader inputStream = Console.In;
#endif
#if  FBHC_TEST
        private readonly TextWriter outputStream = new StreamWriter("output.txt");
#else
        private readonly TextWriter outputStream = Console.Out;
#endif

        private string[] tokens;
        private int pointer;

        public char NextChar()
        {
            try
            {
                var c = inputStream.Read();

                if (c < 0)
                    c = 0;

                return (char)c;
            }
            catch (IOException)
            {
                return '\0';
            }
        }

        public string NextLine()
        {
            try
            {
                return inputStream.ReadLine();
            }
            catch (IOException)
            {
                return null;
            }
        }

        public string NextString()
        {
            try
            {
                while (tokens == null || pointer >= tokens.Length)
                {
                    tokens = (NextLine() + string.Empty).Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    pointer = 0;
                }
                return tokens[pointer++];
            }
            catch (IOException)
            {
                return null;
            }
        }

        public int NextInt()
        {
            return Next<int>();
        }

        public long NextLong()
        {
            return Next<long>();
        }

        public T Next<T>()
        {
            var t = typeof(T);

            if (t == typeof(char))
                return (T)(object)NextChar();

            object s = NextString();

            if (t == typeof(string))
                return (T)s;

            return (T)Convert.ChangeType(s, typeof(T));
        }

        public IEnumerable<T> NextWhile<T>(Predicate<T> pred)
        {
            if (pred == null)
                yield break;

            var res = Next<T>();

            while (pred(res))
            {
                yield return res;

                res = Next<T>();
            }
        }

        public IEnumerable<T> NextSeq<T>(long n)
        {
            for (var i = 0; i < n; i++)
                yield return Next<T>();
        }

        public void Print(string format, params object[] args)
        {
            outputStream.Write(string.Format(CultureInfo.InvariantCulture, format, args));
        }

        public void PrintLine(string format, params object[] args)
        {
            Print(format, args);

            outputStream.WriteLine();
        }

        public void Print(bool o)
        {
            Print(o ? "YES" : "NO");
        }

        public void PrintLine(bool o)
        {
            Print(o);

            outputStream.WriteLine();
        }

        public void Print<T>(T o)
        {
            var ienum = o as IEnumerable;

            if (ienum != null && typeof(T) != typeof(string))
            {
                var first = true;

                foreach (var iev in ienum)
                {
                    if (!first && iev != null)
                        Print(" ");

                    Print(iev);

                    first = false;
                }

            }
            else
            {
                outputStream.Write(Convert.ToString(o, CultureInfo.InvariantCulture));
            }

        }

        public void PrintLine<T>(T o)
        {
            Print(o);

            outputStream.WriteLine();
        }

        public void Close()
        {
            inputStream.Close();
            outputStream.Close();
        }

        void IDisposable.Dispose()
        {
            Close();

#if  LOCAL_TEST
            Console.ReadLine();
#endif
        }
    }

    public static class ListExtensions
    {
        public static void Each<T>(this IEnumerable<T> self, Action<T> action)
        {
            foreach (var v in self)
                action(v);
        }

        public static string Reverse(this string s)
        {
            var charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }

        public static IEnumerable<T> ToCycleEnumerable<T>(this IEnumerable<T> self)
        {
            if (self != null)
            {
                for (; ; )
                {
                    foreach (var x in self)
                        yield return x;
                }
            }
        }

        public static T CycleGet<T>(this IList<T> self, int index)
        {
            var c = self.Count;
            return self[(index % c + c) % c];
        }

        public static IList<T> Shuffle<T>(this IList<T> self)
        {
            var rnd = new Random(DateTime.Now.Millisecond);

            var n = self.Count;

            while (n > 1)
            {
                n--;
                var k = rnd.Next(n + 1);
                var value = self[k];
                self[k] = self[n];
                self[n] = value;
            }

            return self;
        }

        public static IList<IList<T>> UseForNew2D<T>(this T def, int fd, int sd)
        {
            var fdim = new T[fd][];

            for (int i = 0; i < fd; i++)
            {
                fdim[i] = new T[sd];
                Fill(fdim[i], def);
            }

            return fdim;
        }

        public static IList<T> Fill<T>(this IList<T> self, Func<int, IList<T>, T> fval)
        {
            for (var i = 0; i < self.Count; i++)
                self[i] = fval(i, self);

            return self;
        }

        public static IList<T> Fill<T>(this IList<T> self, Func<int, T> fval)
        {
            return self.Fill((x, slf) => fval(x));
        }

        public static IList<T> Fill<T>(this IList<T> self, Func<T> fval)
        {
            return self.Fill(x => fval());
        }

        public static IList<T> Fill<T>(this IList<T> self, T val)
        {
            return self.Fill(() => val);
        }

        public static long Hash<T>(this IList<T> self, Func<T, long> val = null)
        {
            return val != null ? self.Hash((t, current) => unchecked(current * 19L + val(t))) : self.Hash(HashOne);
        }

        public static long Hash<T>(this IList<T> self, Func<T, long, long> val)
        {
            return self.Aggregate(0L, (current, t) => unchecked(val(t, current)));
        }

        public static long HashOne<T>(this T self, long current)
        {
            return unchecked(current * 19L + (Equals(self, default(T)) ? 0 : self.GetHashCode()));
        }

        public static IList<T1> BuildSegTree<T1, T2>(this IList<T2> a, Func<T1, T1, T1> combine, Func<T2, T1> convert)
        {
            var t = new T1[a.Count << 2];

            a.BuildSegTree(t, 1, 0, a.Count - 1, combine, convert);

            return t;
        }

        private static void BuildSegTree<T1, T2>(this IList<T2> a, IList<T1> t, int v, int tl, int tr, Func<T1, T1, T1> combine, Func<T2, T1> convert)
        {
            if (tl == tr)
            {
                t[v] = convert(a[tl]);
            }
            else
            {
                int tm = (tl + tr) >> 1;

                BuildSegTree(a, t, v << 1, tl, tm, combine, convert);
                BuildSegTree(a, t, (v << 1) + 1, tm + 1, tr, combine, convert);

                t[v] = combine(t[v << 1], t[(v << 1) + 1]);
            }
        }
    }

    public static class P
    {
        public static P<T1, T2> Create<T1, T2>(T1 key, T2 val)
        {
            return new P<T1, T2>(key, val);
        }
    }

    public class P<T1, T2>
    {
        private class PComparer : IComparer<P<T1, T2>>
        {
            private readonly bool? byKey;

            public PComparer(bool? byKey)
            {
                this.byKey = byKey;
            }

            public int Compare(P<T1, T2> x, P<T1, T2> y)
            {
                x = x ?? new P<T1, T2>();
                y = y ?? new P<T1, T2>();

                if (byKey == true)
                    return Comparer<T1>.Default.Compare(x.Key, y.Key);
                else if (byKey == false)
                    return Comparer<T2>.Default.Compare(x.Value, y.Value);
                else
                {
                    var tr = Comparer<T1>.Default.Compare(x.Key, y.Key);

                    if (tr != 0)
                        tr = Comparer<T2>.Default.Compare(x.Value, y.Value);

                    return tr;
                }
            }
        }

        public P()
        {
        }

        public P(T1 key, T2 val)
        {
            this.Key = key;
            this.Value = val;
        }

        public T1 Key
        {
            get;
            set;
        }

        public T2 Value
        {
            get;
            set;
        }

        public P<T1, T2> Clone()
        {
            return new P<T1, T2>(Key, Value);
        }

        public static IComparer<P<T1, T2>> KeyComparer
        {
            get { return new PComparer(true); }
        }

        public static IComparer<P<T1, T2>> ValueComparer
        {
            get { return new PComparer(false); }
        }

        public static implicit operator T1(P<T1, T2> obj)
        {
            return obj.Key;
        }

        public static implicit operator T2(P<T1, T2> obj)
        {
            return obj.Value;
        }

        public P<T1, T2> SetKey(T1 key)
        {
            Key = key;
            return this;
        }

        public P<T1, T2> SetValue(T2 value)
        {
            Value = value;
            return this;
        }

        public override string ToString()
        {
            return string.Format("Key: {0}, Value: {1}", Key, Value);
        }

        public override bool Equals(object obj)
        {
            var co = obj as P<T1, T2>;

            return co != null && co.GetHashCode() == GetHashCode();
        }

        public override int GetHashCode()
        {
            int hash = 17;
            hash = unchecked(hash * 31 + ((Equals(Key, default(T1))) ? 0 : Key.GetHashCode()));
            hash = unchecked(hash * 31 + ((Equals(Value, default(T1))) ? 0 : Value.GetHashCode()));
            return hash;

        }
    }

    #endregion

    internal class Program
    {
        #region Program

        private readonly MyIo io;

        public Program(MyIo io)
        {
            this.io = io;
        }

        public static void Main(string[] args)
        {
            using (MyIo io = new MyIo())
            {
                new Program(io)
                    .Solve();
            }
        }

        #endregion

        private void Solve()
        {
            var n = io.NextInt();
            var k = io.NextInt();

            var cnt = 0;

            foreach (var c in io.NextLine().ToCharArray().Select(x => x == '#'))
            {
                if (c)
                {
                    cnt++;
                }
                else
                {
                    if (cnt >= k)
                    {
                        io.PrintLine(false);
                        return;
                    }
                    cnt = 0;
                }
            }

            io.PrintLine(true);

        }
    }

}


using System;

namespace AllProblems
{
    class Program
    {
        static void Main(string[] args)
        {
            _299B_KsushaTheSquirrel.Run();
        }
    }

    class _299B_KsushaTheSquirrel
    {
        public static void Run()
        {
            var line = Console.ReadLine().Split(' ');
            var n = int.Parse(line[0]);
            var k = int.Parse(line[1]);

            var s = Console.ReadLine();
            var len = s.Length;
            var ctr = 0;

            for (var i = 0; i < len; i++)
            {
                if (s[i] == '.') ctr = 0;
                else ++ctr;

                if (ctr != k) continue;
                Console.WriteLine("NO");
                Console.ReadLine();
                return;
            }

            Console.WriteLine("YES");
            Console.ReadLine();
        }
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{

    static void Main(string[] args)
    {
        var input = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        var k = int.Parse(input[1]);

        var s = Console.ReadLine();

        var mx = 0;
        var lmx = 0;

        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == '#')
            {
                lmx++;
            }
            else
            {
                mx = Math.Max(mx, lmx);
                lmx = 0;
            }

        }

        Console.WriteLine(mx<k?"YES":"NO");
    }
}


fun main(args: Array<String>) {
    val nk = readLine()!!.split(" ").map { it -> it.toInt() }
    val n = nk[0]
    val k = nk[1]
    val s = readLine()!!
    var b = 0
    for (c in s) {
        if (c == '.' && b < k) b = 0
        if (c == '#') b++
        if (b >= k) {
            println("NO")
            return
        }
    }
    println("YES")
}


fun main() {
    val (roadLength, maxJump) = readLine()!!.split(" ").map(String::toInt)
    val road = readLine()!!
    var pos = 0
    nextJump@ while (pos < roadLength - 1) {
        for (next in pos + 1..pos + maxJump)
            if (road[next] == '.') {
                pos = next
                continue@nextJump
            }
        return print("NO")
    }
    print("YES")
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;

typedef double dbl;
typedef float flt;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define eps 1e-9
#define inf 1000000000
#define infll 1000000000000000000LL
#define abs(x) ((x)<0?-(x):(x))
#define sqr(x) ((x)*(x))
#define pb push_back
#define mp make_pair
#define px first
#define py second
#define sz(x) ((int)(x).size())
#define intclz(x) __builtin_clz(x)
#define intctz(x) __builtin_ctz(x)
#define intln(x) (32-intclz(x))
#define intbc(x) __builtin_popcount(x)
#define llclz(x) __builtin_clzll(x)
#define llctz(x) __builtin_ctzll(x)
#define llln(x) (64-llclz(x))
#define llbc(x) __builtin_popcountll(x)
#define atbit(x,i) (((x)>>(i))&1)
#define tof(x) __typeof(x)
#define FORab(i,a,b) for (int i=(a); i<=(b); ++i)
#define RFORab(i,a,b) for (int i=(a); i>=(b); --i)
#define FOR1(i,n) FORab(i,1,(n))
#define RFOR1(i,n) RFORab(i,(n),1)
#define FOR(i,n) FORab(i,0,(n)-1)
#define RFOR(i,n) RFORab(i,(n)-1,0)
#define allstl(i,x,t) for (t::iterator i = (x).begin(); i!=(x).end(); ++i)
#define rallstl(i,x,t) for (t::reverse_iterator i = (x).rbegin(); i!=(x).rend(); ++i)
#define begend(x) (x).begin(),(x).end()
#define ms(a,v) memset(a,v,sizeof(a))
#define msn(a,v,n) memset(a,v,n*sizeof(a[0]))
#define mcp(d,s,n) memcpy(d,s,n*sizeof(s[0]))
#define clamp(x,a,b) min(max(x,a),b)

int main()
{
    //freopen("in.txt", "rt", stdin);
    //freopen("out.txt", "wt", stdout);
    int n, k;
    string s;
    while (cin>>n>>k>>s) {
		int run = 0;
		FOR(i,n) {
			if (s[i]=='#') {
				++run;
			} else {
				run = 0;
			}
			if (run == k) break;
		}
		if (run) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

vector<string> split( const string &str, const string &delim = " " )
{
	int start = 0, pos;
	vector<string> result;

	while ( ( pos = str.find_first_of( delim, start ) ) != string::npos )
	{
		result.push_back( str.substr( start, pos - start ) );
		start = pos + 1;
	}

	return result;
}

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

	int n, k;
	cin >> n >> k;

	string road;
	cin >> road;

	VS rocks( split( road, "." ) );
	cout << ( none_of( ALL( rocks ), [=]( string s ){ return k <= s.size(); } ) ? "YES" : "NO" ) << endl;

	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;

const int INF=0x3f3f3f3f;
typedef long long ll;
const int M=300005;
const int mod=1e9+7;
const double eps=1e-6;
const double Pi=2*acos(0.0);

inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;}
inline int bit(int x){return 1<<x;}
inline double dabs(double x){return x>0?x:(-x);}
inline int abs(int x){return x>0?x:(-x);}
inline int lowbit(int x){return x&(-x);}

int n,k;
char s[M];
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>k;
    cin>>s+1;
    int t=1;
    for(int i=2;s[i];i++){
        if(s[i]=='.'&&i-t<=k) t=i;
    }
    puts(t==n?"YES":"NO");
    return 0;
}


//ITNOG
#include<bits/stdc++.h>

using namespace std;
//#define int long long
const int maxN = 300*1000 + 10;
const int mod = 1000*1000*1000 + 7;
int dp[maxN];
main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    int n, k; cin >> n >> k;
    int res = 1; dp[0] = 1;
    string s; cin >> s;
    for(int i = 1; i < n; i ++)
    {
	  if(i > k) res -= dp[i - k - 1];
	  if(s[i] == '#') continue;
	  if(res) dp[i] = 1, res ++;
    }
    cout << (dp[n - 1] ? "YES" : "NO" ) << endl;
    return 0;
}


#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<ctime>
#include<list>
#include<deque>
#include<bitset>
#include<string>
#include<string.h>
#define in freopen("input.txt","r",stdin)
#define out freopen("output.txt","w",stdout)

#define inp freopen(".in","r",stdin)
#define outp freopen(".out","w",stdout)

using namespace std;

#define pb push_back
#define pf push_front
#define p_f pop_front
#define p_b pop_back
#define LL __int64
#define LD long double
#define MP make_pair
#define fi first
#define se second
#define sc scanf
#define pr printf
#define si size()
#define str strlen
#define INF 10000000000ll
LL a,b,i,j,k,n,m,d[1000001]={0},ans=0,len=0;
LL c[1010100]={0},cnt=0,fl=0,a2,a3=-1000000,used[1000001]={0},i1;
char s[1000001];
map<LL,LL>us;
main()
{
    sc("%I64d%I64d",&n,&k);
    for(i=0;i<n;i++)
    {
        cin>>s[i];
        us[i]=ans;
        if( s[i]=='.' ){ans=0;}
        else ans++;
        if( us[ i ]>=k )fl=1;
    }
    if(fl==1){cout<<"NO\n";}
    else cout<<"YES\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <queue>
#include <fstream>
#include <sstream>
#include <map>
#include <list>
#include <cmath>
#include <bitset>
#include <stack>
#include <stdio.h>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <utility>
#include <stdlib.h>
#include <stdio.h>
#include <cstdio>
using namespace std;

#define mod 1000000007
#define ll long long
#define INF 1000000000
#define PI 3.1415926

int main (void)
{

   // ifstream cin("in.txt");

    int n, k;
    string s;
    cin >> n >> k;
    cin >> s;

    int con = 0;
    int conmax = 0;

    for (int i = 0; i < n; i++)
    {
        if (s[i] == '.')
            con = 0;
        else
            con++;
        conmax = max(conmax, con);
    }

    if (conmax >= k)
        cout << "NO" << endl;
    else
        cout << "YES" << endl;

    // while (true) {}

}


#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <queue>
#include <fstream>
#include <sstream>
#include <map>
#include <list>
#include <cmath>
#include <bitset>
#include <stack>
#include <stdio.h>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <utility>
#include <stdlib.h>
#include <stdio.h>
#include <cstdio>
using namespace std;

#define mod 1000000007
#define ll long long
#define INF 1000000000
#define PI 3.1415926

int n, k;
string s;
bool disc[300000];

void dfs (int v)
{
    disc[v] = true;

    for (int i = 1; i <= k; i++)
        if (v + i < n && !disc[v+i] && s[v+i] == '.')
            dfs(v+i);
}

int main (void)
{

   // ifstream cin("in.txt");

    cin >> n >> k;
    cin >> s;

    memset(disc, 0, sizeof(disc));
    dfs(0);

    if (disc[n-1])
        cout << "YES" << endl;
    else
        cout << "NO" << endl;

    // while (true) {}

}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int N, K;
string str;

void soups_on();

int main() {
  cin >> N >> K >> str;
  soups_on();

  return 0;
}

void soups_on() {
  bool is_end = false, is_ok;
  int p = 0, q;

  while (!is_end) {
    p = int(str.find('#', p));
    if (p == int(string::npos))
      is_end = true, is_ok = true;
    else {
      q = int(str.find('.', p));
      if (q - p >= K)
        is_end = true, is_ok = false;
      p = q;
    }
  }

  cout << (is_ok ? "YES" : "NO") << "\n";
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int N, K;
string str;

void soups_on();

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> K >> str;
  soups_on();

  return 0;
}

void soups_on() {
  bool is_end = false, is_ok;
  int p = 0, q;

  while (!is_end) {
    p = int(str.find('#', p));
    if (p == int(string::npos))
      is_end = true, is_ok = true;
    else {
      q = int(str.find('.', p));
      if (q - p >= K)
        is_end = true, is_ok = false;
      p = q;
    }
  }

  cout << (is_ok ? "YES" : "NO") << "\n";
}


#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<fstream>
#include<sstream>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<utility>
#include<climits>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

#define inf INT_MAX/3
#define pb push_back
#define MP make_pair
#define all(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define pii pair<int,int>
#define pcc pair<char,char>
#define pic pair<int,char>
#define pci pair<char,int>
#define VS vector<string>
#define VI vector<int>
#define debug(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define in scanf
#define out printf
#define ll long long
#define ull unsigned long long
#define eps 1e-9
#define MOD 1E9 +7
#define lim 1000

//TYO conversion
template<typename T>inline string toString(T a){ostringstream os("");os<<a;return os.str();}
template<typename T>inline ll toLong(T a){ll res;istringstream os(a);os>>res;return res;}
//MATHEMATICS
template<typename T>inline T  SQ(T a){return a*a;}
template<typename T>inline T gcd(T a, T b){if (b == 0)return a;else return gcd(b, a % b);}
template<typename T>inline ull bigmod(T a, T b, T m){if (b == 0)return 1;else if (b % 2 == 0)return S(bigmod(a, b / 2, m)) % m;else return (a % m*bigmod(a, b - 1, m)) % m;}
template<typename T>inline VS parse(T str){VS res;string s;istringstream os(str);while(os>>s)res.pb(s);return res;}
template<typename T>inline ull  dist(T A,T B){ull res=(A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);return res;}
template<typename T>inline double cosAngle(T a,T b,T c){double res=a*a+b*b-c*c;res=res/(2*a*b);res=acos(res);return res;}
template<typename T>inline T power(T base,int po){T res=1;if(base==0)return 0; FOR(i,0,po)res*=base;return res;}
//BIT
template<typename T>inline bool isOn(T mask,T pos){return mask&(1<<pos);}
template<typename T>inline int Off(T mask,T pos){return mask^(1<<pos);}
template<typename T>inline int On(T mask,T pos){return mask|(1<<pos);}

int main(){

    int n;
    string str;
    int a,b;
    cin>>a>>b;
    cin>>str;
    int c=0;
    VI v;
    FOR(i,0,a){
        if(str[i]=='.')v.pb(i);
    }
    int sz=v.size();
    FOR(i,1,sz)
    {
        if(v[i]-v[i-1]>b )c=b+10;
    }
    if(c<=b)cout<<"YES";
    else cout<<"NO";

//    bool st=1;
//    int res;
//    int x;
//    cin>>n;
//    FOR(i,0,n){
//        cin>>x;
//        v.pb(x);
//    }
//
//    sort(all(v));
//    FOR(i,1,n){
//        if(v[i]%v[0]!=0)st=0;
//    }
//
//    res=v[0];
//    if(!st)res=-1;
//    cout<<res<<endl;

   // main();
}


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <climits>
#include <fstream>
#include <functional>
#include <utility>

using namespace std;

typedef long long LL;
typedef unsigned U;
typedef unsigned long long ULL;

const double pi = 3.14159265359;
const LL MOD = 1000000007;

int mas [100100];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    char sym;
    int last = 0;
    for(int i=1; i<=n; ++i)
    {
         cin >> sym;
         if(sym == '.')
         {
         if(i - last > k) {cout << "NO"; return 0;}
           last = i;
          }
     }
     cout << "YES";
     return 0;
}


#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <bitset>
#include <string>
#include <map>
#include <queue>
#include <hash_map>
#include <hash_set>
#include <cstring>
using namespace std;

#define rep(i, n) f(i, 0, n - 1)
#define f(i, a, b) fd(i, a, b, 1)
#define fd(i, a, b, d) for(int i = (a); i <= (int)(b); i += (d))
#define uint64 unsigned int64
#define int64 long long

#ifndef ONLINE_JUDGE
#include <time.h>
clock_t _time;
void startProfile()
{
  _time = clock();
  printf("Begin profiling..\n...\n");
}
void stopProfile()
{
  printf("\n...\nEnd profiling. Time: %f sec.\n", (double)(clock() - _time) / CLOCKS_PER_SEC);
}
#endif

int main()
{
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  startProfile();
#endif
  int n, k;
  scanf("%d %d\n", &n, &k);

  int m, d;
  m = d = 0;
  for(char c = getchar(); c != '\n'; c = getchar())
  {
    if(c == '#')
    {
      d++;
    }
    else
    {
      m = max(m, d);
      d = 0;
    }
  }
  printf(m < k ? "YES" : "NO");

#ifndef ONLINE_JUDGE
  stopProfile();
#endif
  return 0;
}


#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <bitset>
#include <string>
#include <map>
#include <queue>
#include <hash_map>
#include <hash_set>
#include <cstring>
using namespace std;

#define rep(i, n) f(i, 0, n - 1)
#define f(i, a, b) fd(i, a, b, 1)
#define fd(i, a, b, d) for(int i = (a); i <= (int)(b); i += (d))
#define uint64 unsigned int64
#define int64 long long

#ifndef ONLINE_JUDGE
#include <time.h>
clock_t _time;
void startProfile()
{
  _time = clock();
  printf("Begin profiling..\n...\n");
}
void stopProfile()
{
  printf("\n...\nEnd profiling. Time: %f sec.\n", (double)(clock() - _time) / CLOCKS_PER_SEC);
}
#endif

int main()
{
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  startProfile();
#endif
  int n, k;
  scanf("%d %d\n", &n, &k);
  char c[300000];
  fread(c, 1, n, stdin);
  int d = 0;
  rep(i, n)
  {
    if(c[i] == '#')
      d++;
    else
    {
      if(d >= k)
      {
        printf("NO");
        return 0;
      }
      d = 0;
    }
  }
  printf("YES");

#ifndef ONLINE_JUDGE
  stopProfile();
#endif
  return 0;
}


#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <bitset>
#include <string>
#include <map>
#include <queue>
#include <hash_map>
#include <hash_set>
#include <cstring>
using namespace std;

#define rep(i, n) f(i, 0, n - 1)
#define f(i, a, b) fd(i, a, b, 1)
#define fd(i, a, b, d) for(int i = (a); i <= (int)(b); i += (d))
#define uint64 unsigned int64
#define int64 long long

#ifndef ONLINE_JUDGE
#include <time.h>
clock_t _time;
void startProfile()
{
  _time = clock();
  printf("Begin profiling..\n...\n");
}
void stopProfile()
{
  printf("\n...\nEnd profiling. Time: %f sec.\n", (double)(clock() - _time) / CLOCKS_PER_SEC);
}
#endif

int main()
{
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  startProfile();
#endif
  int n, k;
  scanf("%d %d\n", &n, &k);

  int d = 0;
  for(char c = getchar(); c != '\n'; c = getchar())
  {
    if(c == '#')
      d++;
    else
    {
      if(d >= k)
      {
        printf("NO");
        return 0;
      }
      d = 0;
    }
  }
  printf("YES");

#ifndef ONLINE_JUDGE
  stopProfile();
#endif
  return 0;
}


#include <bits/stdc++.h>
#define forn(i, n) for(int i = 0; i < (int) n; ++i)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; ++i)
#define For(i, st, en) for(int i = (int)st; i <= (int)en; ++i)
#define clr(x) memset(x, 0, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define x first
#define y second
#define mp make_pair

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;

/**
 * The code starts here.
 */

int main()
{
    ios_base::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;

    int m = 0;
    for (int i = 0; i < n; ++i)
    {
        if (s[i] == '#')
        {
            int j = i;
            while (s[j] == '#')
                ++j;
            m = max(m, j - i);
            i = j - 1;
        }
    }

    if (m < k)
    {
        cout << "YES" << endl;
    } else
    {
        cout << "NO" << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define forn(i, n) for(int i = 0; i < (int) n; ++i)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; ++i)
#define For(i, st, en) for(int i = (int)st; i <= (int)en; ++i)
#define clr(x) memset(x, 0, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define x first
#define y second
#define mp make_pair

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;

/**
 * The code starts here.
 */

int main()
{
    ios_base::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;

    int m = 0;
    for (int i = 0; i < n; ++i)
    {
        if (s[i] == '#')
        {
            int j = i;
            while (s[j] == '#')
                ++j;
            m = max(m, j - i);
            i = j - 1;
        }
    }

    if (m <= k)
    {
        cout << "YES" << endl;
    } else
    {
        cout << "NO" << endl;
    }
    return 0;
}


#pragma comment(linker, "/STACK:16777216")
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#ifndef ONLINE_JUDGE
#define Eo(x) { cerr << #x << " = " << (x) << endl; }
#else
#define Eo(x)
#endif

typedef long long int64;
const int inf = 0x3f3f3f3f;
const int64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
typedef double real;
const real eps = 1e-6;
typedef pair<int64,int64> pip;

int64 gcd(int64 a, int64 b) {
  return a ? gcd(b%a,a) : b;
}

template <typename A,typename B>
ostream& operator<<(ostream& os, const pair<A,B>& p) {
                 return os << p.first << "," << p.second;
}

template <typename A>
ostream& operator<<(ostream& os, const vector<A>& v) {
                 for (int i = 0; i < v.size(); i++) {
                   os << v[i] << ";";
                 }
                 return os;
}

template <typename A>
ostream& operator<<(ostream& os, const set<A>& v) {
                 for (typename set<A>::const_iterator it = v.begin(); it != v.end(); ++it) {
                   os << *it << ";";
                 }
                 return os;
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const map<A,B>& v) {
                 for (typename map<A,B>::const_iterator it = v.begin(); it != v.end(); ++it) {
                   os << it->first << "=" << it->second << ";";
                 }
                 return os;
}

inline int64 bit(int64 k) {
  return int64(1)<<k;
}

int SolveSquareEquation(real a, real b, real c, real& x1, real& x2) {
  real desc = b*b-4*a*c;
  if (desc<0) return 0;
  real sqrt_desc = sqrt(desc);
  x1 = (-b+sqrt_desc)/(2*a);
  x2 = (-b-sqrt_desc)/(2*a);
  if (desc<eps) {
    return 1;
  }
  return 2;
}

int SolveSquareEquationInt(int64 a, int64 b, int64 c, int64& x1, int64& x2) {
  int64 desc = b*b-4*a*c;
  if (desc<0) return 0;
  real sqrt_desc_r = sqrt(real(desc));
  int64 sqrt_desc = sqrt_desc_r;
  for (;sqrt_desc*sqrt_desc<desc; sqrt_desc++);
  for (;sqrt_desc*sqrt_desc>desc; sqrt_desc--);
  if (sqrt_desc*sqrt_desc != desc) {
    return 0;
  }
  int64 u1 = -b+sqrt_desc;
  int64 u2 = -b-sqrt_desc;
  int64 a2 = a*2;
  vector<int64> res;
  if (!(u1%a2)) {
    res.push_back(u1/a2);
  }
  if (!(u2%a2)) {
    res.push_back(u2/a2);
  }
  int oldsize = res.size();
  res.resize(2);
  x1=res[0];
  x2=res[1];
  return oldsize;
}

char s[300300];
int main() {
  int n,k; cin >> n >> k;
  scanf("%s",s);
  int pr = 0;
  for (int i = 1; i < n; i++) {
    if (s[i]=='.') {
      int d = i-pr;
      if (d > k) {
        puts("NO");
        return 0;
      }
      pr = i;
    }
  }
  puts("YES");
  return 0;
}