#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; int C[10]; int N; string K; string f(int d) { string res = ""; int CC[10]; memcpy(CC, C, sizeof(C)); bool is_over = false; for (int i = 1; i <= N; ++i) { int v = K[i - 1] - '0'; if (i < d) { if (CC[v] > 0) { CC[v]--; res += to_string(v); } else { return res; } } else if (not is_over) { bool found = false; for (int j = v + 1; j <= 9 && not found; ++j) { if (CC[j] == 0) continue; found = true; CC[j]--; res += to_string(j); } if (not found) { if (CC[v] > 0 && i < N) { CC[v]--; res += to_string(v); } else { return res; } } else { is_over = true; } } else { for (int j = 1; j <= 9; ++j) { if (CC[j] == 0) continue; CC[j]--; res += to_string(j); break; } } } return res; } int main() { cin >> N >> K; for (int i = 1; i <= 9; ++i) { cin >> C[i]; } if (f(1).size() != N) { cout << -1 << endl; } else { int ok = 1; int ng = N; while (abs(ok - ng) >= 2) { int d = (ok + ng) / 2; string res = f(d); if (res.size() == N) { ok = d; } else { ng = d; } } cout << f(ok) << endl; } return 0; }