// yuki 1630 Sorting Integers (Greater than K) // 2021.8.10 #include typedef long long ll; #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) int in() { // 非負整数の入力 int n = 0; int c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } int ins(char *s) { // 文字列の入力 スペース以下の文字で入力終了 char *p = s--; do *++s = gc(); while (*s > ' '); *s = 0; return s - p; } void outs(char *s) { while (*s) pc(*s++); } int N; char K[500005]; int w; int f[10]; int main() { int i, j, k; N = in(), w = ins(K); if (N < w) goto ERR; for (i = 1; i <= 9; ++i) f[i] = in(); if (w == N) { for (i = 0; i < w; ++i) { k = K[i] & 0xf; if (f[k]) f[k]--; else { for (j = k+1; j <= 9; ++j) if (f[j]) { K[i] = j+'0', f[j]--, K[i+1] = 0; outs(K); goto NT; } for (j = 9; !f[j]; --j); K[N = i] = j+'0'; break; } } for (i = N-1; i >= 0; --i) { if (K[i] < K[i+1]) { k = K[i] & 0xf, f[k]++; for (j = k+1; ; ++j) if (f[j]) { K[i] = j+'0', f[j]--, K[i+1] = 0; outs(K); goto NT; } } f[K[i] & 0xf]++; } goto ERR; } NT: for (i = 1; i <= 9; ++i) while (f[i]--) pc('0'+i); pc('\n'); return 0; ERR:puts("-1"); return 0; }