#include int recursion(char K[], int c[], char ans[], int flag) { if (K[0] == 0) return 0; int i; if (flag != 0) { for (i = 1; i <= 9; i++) if (c[i] > 0) break; c[i]--; ans[0] = '0' + i; return recursion(&(K[1]), c, &(ans[1]), 1); } if (c[K[0] - '0'] > 0) { c[K[0] - '0']--; ans[0] = K[0]; if (recursion(&(K[1]), c, &(ans[1]), 0) == 0) return 0; c[K[0] - '0']++; } for (i = K[0] - '0' + 1; i <= 9; i++) if (c[i] > 0) break; if (i > 9) return -1; c[i]--; ans[0] = '0' + i; return recursion(&(K[1]), c, &(ans[1]), 1); } int main() { int i, j, N, c[10]; char K[1000010], ans[500005] = {}; scanf("%d %s", &N, &(K[500005])); for (i = 1, c[0] = 0; i <= 9; i++) scanf("%d", &(c[i])); for (i = 0; i < 500005; i++) K[i] = '0'; for (; K[i] != 0; i++); for (j = i - 1; K[j] == '9'; K[j--] = '0'); K[j]++; if (recursion(&(K[i-N]), c, ans, 0) == 0) printf("%s\n", ans); else printf("-1\n"); fflush(stdout); return 0; }