#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } int main() { int n; cin >> n; string k; cin >> k; int c[9] = {}; for (auto &e : c) cin >> e; string l, r; for (int i = 1; i <= 9; ++i) { l += string(c[i - 1], '0' + i); r += string(c[i - 1], '0' + i); } reverse(begin(r), end(r)); if (r < k) { cout << -1 << endl; return 0; } if (k < l) { cout << l << endl; return 0; } int last = 0, rem[9] = {}; copy(begin(c), end(c), begin(rem)); for (int i = 0; i < n; ++i) { int d = k[i] - '0'; bool found = false; for (int j = d + 1; j <= 9; ++j) { found |= 0 < rem[j - 1]; } if (found) { last = i; } if (k[i] == '0' || --rem[k[i] - '1'] < 0) { break; } } string ans; copy(begin(c), end(c), begin(rem)); for (int i = 0; i < last; ++i) { ans += k[i]; --rem[k[i] - '1']; } for (int i = k[last] - '0' + 1; i <= 9; ++i) { if (0 < rem[i - 1]) { ans += '0' + i; --rem[i - 1]; break; } } for (int i = 1; i <= 9; ++i) { while (rem[i - 1]--) { ans += '0' + i; } } cout << ans << endl; }