#include using namespace std; #define rep(i,n) for(int i = 0; i < int(n); i++) using ll = long long; using P = pair; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; string k; cin >> k; vector c(10); rep(i, 9) cin >> c[i + 1]; vector c2 = c; if (n < (int)k.size()) { cout << -1 << endl; return 0; } // fill 0 string zero = ""; rep(i, n - k.size()) zero += "0"; k = zero + k; // Kより数字を大きくする桁を考える int big = n - 1; rep(i, n) { if (c[k[i] - '0'] == 0) { big = i; break; } c[k[i] - '0']--; } for (; big >= -1; big--) { if (big == -1) { cout << -1 << endl; return 0; } bool ok = false; for (int j = k[big] - '0' + 1; j <= 9; j++) { if (c[j] > 0) ok = true; } if (ok) break; c[k[big] - '0']++; } if (big < 0) { cout << -1 << endl; return 0; } string ans = ""; rep(i, big) { ans += k[i]; c2[k[i] - '0']--; } for (int i = k[big] - '0' + 1; i <= 9; i++) { if (c2[i] > 0) { c2[i]--; ans += to_string(i); break; } } rep(i, 10) { rep(j, c2[i]) { ans += to_string(i); } } cout << ans << endl; return 0; }