#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long c[10]; int main() { long long n; string s; cin >> n >> s; long long ans = 0; long long sum = 0, p = 1; for (int i = 1; i <= 9; i++) { cin >> c[i]; } if (s.size() < n) { for (int i = 1; i <= 9; i++) { for (int j = 0; j < c[i]; j++) { cout << i; } } cout << endl; } else if (s.size() > n) { cout << -1 << endl; } else { string ans1 = ""; for (int i = 9; i >= 1; i--) { for (int j = 0; j < c[i]; j++) { ans1 += char(i + '0'); } } if (s >= ans1) { cout << -1 << endl; } else { string ans = ans1; reverse(ans1.begin(), ans1.end()); for (int i = 0; i < n; i++) { auto itr = lower_bound(ans1.begin() + i + 1, ans1.end(), s[i]); if (itr == ans1.end() || *itr > s[i]) { break; } else { swap(ans1[i], *itr); ans = ans1; } } if (ans == s) { for (int i = n - 1; i > 0; i--) { if (ans[i] != ans[i - 1]) { swap(ans[i], ans[i - 1]); break; } } } cout << ans << endl; } } }