#include <bits/stdc++.h> #include <atcoder/modint> using namespace std; using namespace atcoder; //#pragma GCC optimize("Ofast") #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() using ll = long long; using vec = vector<ll>; using mat = vector<vec>; ll N,M,H,W,Q,K,A,B; string S; using P = pair<ll, ll>; using tp = tuple<ll, ll, ll>; const ll INF = (1LL<<60); template<class T> bool chmin(T &a, const T b){ if(a > b) {a = b; return true;} else return false; } template<class T> bool chmax(T &a, const T b){ if(a < b) {a = b; return true;} else return false; } template<class T> void my_printv(std::vector<T> v,bool endline = true){ if(!v.empty()){ for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" "; std::cout<<v.back(); } if(endline) std::cout<<std::endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin>>N>>S; vec c(10, 0); rep(i, 9) cin>>c[i+1]; if(S.size() > N){ cout<<-1<<endl; }else if(size(S) < N){ rep(i, 10) rep(j, c[i]) cout<<i; cout<<endl; }else{ string res = ""; bool eq = true; for(char s : S) { bool ok = false; reps(i, eq ? s - '0' : 0, 10) { if (c[i]) { --c[i]; ok = true; if(i != s - '0') eq = false; res.push_back('0' + i); break; } } if (!ok) { cout << -1 << endl; return 0; } } rep(i, 10) c[i] = 0; if(S == res){ int pos = -1; Rrep(i, N) { ++c[res[i]-'0']; if(i < N - 1 and res[i] < res[i + 1]){ pos = i; break; } } if(pos == -1){ cout<<-1<<endl; return 0; }else{ reps(i, res[pos] - '0' + 1, 10){ if(c[i]){ --c[i]; res[pos] = '0' + i; break; } } //my_printv(c); reps(i, pos + 1, N) { //cout<<res<<endl; rep(j, 10) { if(c[j] > 0){ --c[j]; res[i] = '0' + j; break; } } } } } cout<<res<<endl; } }