#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> #include <string> #include <sstream> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; typedef unsigned long long ull; using P=pair<ll,ll>; const int INF=1001001001; const int mod=1e9+7; void solve(){ int n; string x; cin>>n>>x; vector<int>s(n); rep(i,n){s[i]=x[i]-'0';} vector<int>cnt(10); string mx; for(int i=1;i<=9;i++){ cin>>cnt[i]; rep(j,cnt[i]){ mx+=(char)('0'+i); } } sort(mx.rbegin(),mx.rend()); int X=x.size(); if(n<X||mx<=x){cout<<-1<<endl;return;} sort(mx.begin(),mx.end()); if(n>X){cout<<mx<<endl;return;} auto c2=cnt; int lst=0; rep(i,n){ bool ok=false; for(int j=s[i]+1;j<=9;j++){ if(c2[j]!=0){ok=true;} } if(ok){chmax(lst,i);} if(c2[s[i]]==0){break;} c2[s[i]]--; } string ans; rep(i,lst){ ans+=x[i]; cnt[s[i]]--; } for(int i=s[lst]+1;i<=9;i++){ if(cnt[i]!=0){ ans+=(char)(i+'0'); cnt[i]--; break; } } for(int i=1;i<=9;i++){ if(cnt[i]!=0){ ans+=string(cnt[i],(char)(i+'0')); } } cout<<ans<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }