import java.io.*; import java.util.*; class Main { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]); char a[]=s[1].toCharArray(); int i,j,c[]=new int[9],m=a.length; s=bu.readLine().split(" "); for(i=0;i<9;i++) c[i]=Integer.parseInt(s[i]); if(m>n) {System.out.print(-1); return;} String ans=""; boolean pos=true; for(i=0;i0 && j>=cur) {ch=j; break;} if(ch==-1) {pos=false; break;} ans+=(char)(ch+1+'0'); c[ch]--; if(ch>cur) break; } if(!pos) {System.out.print(-1); return;} if(ans.length()0) ans+=(char)(i+1+'0'); System.out.print(ans); return; } char a2[]=ans.toCharArray(); if(!equal(a2,a)) {System.out.print(ans); return;} int max=n-1; for(i=n-2;i>=0;i--) if(a2[i]a2[max]) max=i; if(i==-1) {System.out.print(-1); return;} char t=a2[max]; a2[max]=a2[i]; a2[i]=t; Arrays.sort(a2,i+1,n); System.out.print(new String(a2)); } static boolean equal(char a[],char b[]) { for(int i=0;i