#include #include #include using namespace std; using ll = long long; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n; cin>>n; string k; cin>>k; vector c(10,0); for(int i = 1;i<=9;i++) cin>>c[i]; if(n!=k.size()){ int m = k.size(); if(m>n) { cout<<-1< bool { if(ni==k.size()) return true; int p = k[ni] - '0'; if(c[p]!=0&&ni+1!=k.size()){ c[p]--; ans += k[ni]; bool ok = dfs(dfs,ni+1); if(ok) return true; c[p]++; ans.pop_back(); } for(int i = p+1;i<=9;i++){ if(c[i]==0) continue; ans += '0' + i; c[i]--; for(int j = 1;j<=9;j++){ for(int l = 0;l