#include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; int n, m; vector c(10); vector v; vector get_input(){ string s; cin >> s; int n = s.size(); vector ans(n); vector v(n); for(int i = 0; i < n; i++) { char c = s[i]; v[i] = c-'0'; } int carry = 1; for(int i = n-1; i >= 0; i--){ ans[i] = v[i]+carry; if(ans[i] == 10){ ans[i] = 0; carry = 1; }else{ carry = 0; } } if(carry == 1){ vector u(n+1); u[0] = 1; for(int i = 1; i <= n; i++) u[i] = ans[i-1]; return u; } return ans; } string ans; bool dfs(int i, bool big){ // debug_value(i) if(i == m){ cout << ans << endl; return true; } for(int j = 1; j <= 9; j++){ if(!big && j < v[i]) continue; if(c[j] > 0){ c[j]--; ans += j+'0'; if(dfs(i+1, big || j > v[i])) return true; ans.pop_back(); c[j]++; } } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> n; v = get_input(); for(int i = 1; i <= 9; i++) cin >> c[i]; int k = accumulate(c.begin(), c.end(), 0); string ans; m = v.size(); if(k < m){ cout << -1 << endl; return 0; } if(k > m){ for(int i = 1; i <= 9; i++){ for(int j = 0; j < c[i]; j++) cout << i; } cout << endl; return 0; } if(!dfs(0, false)) cout << -1 << endl; }