#include using namespace std; int main() { int N; string K; cin >> N >> K; vectortmp(9); for(int i = 0; i < 9; i++) { cin >> tmp[i]; } if(N > K.size()) { string S = ""; for(int i = 0; i < 9; i++) { S += string(tmp[i],(char)('0'+i+1)); } cout << S << endl; } else if(N < K.size()) { cout << -1 << endl; } else { string S = ""; bool flag = false; for(int i = 0; i < N; i++) { for(int j = 0; j < 9; j++) { char c = (char)('0'+j+1); if(flag) { if(tmp[j]) { tmp[j]--; S += c; break; } } else { if(K[i] == c && tmp[j]) { tmp[j]--; S += c; break; } if(K[i] < c && tmp[j]) { tmp[j]--; S += c; flag = true; break; } } } if(i+1 != S.size()) { bool ok = false; for(int j = i-1; j >= 0; j--) { for(int k = 0; k < 9; k++) { char c = (char)('0'+k+1); if(tmp[k] && K[j] < c) { tmp[S[j]-'0'-1]++; S[j] = c; ok = true; break; } } if(ok) { break; } tmp[S[j]-'0'-1]++; S.pop_back(); } if(!ok) { cout << -1 << endl; return 0; } flag = true; i = S.size(); } } if(!flag) { bool ok = false; for(int j = N-1; j >= 0; j--) { for(int k = 0; k < 9; k++) { char c = (char)('0'+k+1); if(tmp[k] && K[j] < c) { tmp[S[j]-'0'-1]++; S[j] = c; tmp[k]--; ok = true; break; } } if(ok) { break; } tmp[S[j]-'0'-1]++; S.pop_back(); } if(!ok) { cout << -1 << endl; return 0; } flag = true; for(int i = S.size(); i < N; i++) { for(int j = 0; j < 9; j++) { char c = (char)('0'+j+1); if(flag) { if(tmp[j]) { tmp[j]--; S += c; break; } } else { if(K[i] == c && tmp[j]) { tmp[j]--; S += c; break; } if(K[i] < c && tmp[j]) { tmp[j]--; S += c; flag = true; break; } } } } } cout << S << endl; } }