//#include #include using namespace std; //using namespace atcoder; using ll = long long; #define all(A) A.begin(),A.end() using vll = vector; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) int main() { ll N; string K; cin >> N >> K; vll Cj(9); rep(i, 9)cin >> Cj[i]; vll Cb = Cj; if (N < K.size()) { cout << -1 << endl; return 0; } else while(N>K.size())K = '0' + K; bool Ch = true; string anj = ""; string an2 = ""; string an3 = ""; vector S; ll itr=0; rep(i, N) { Ch = true; rep(k, 9) { if (K[i] >= (k + '1')||Cj[k] <= 0)continue; //an2 = anj + char(k + '1'); for(ll j=itr;j (k + '1')||Cj[k] <= 0)continue; if (K[i] == (k + '1')) { anj.push_back(K[i]); Cj[k]--; Ch = false; break; } } if (Ch) { rep(j, 9) { rep(k, Cb[j]) { an2.push_back((j + '1')); } } if (an2 > K)cout << an2 << endl; else cout << -1 << endl; return 0; } } cout << an2 << endl; }