func main() var n: int :: cui@inputInt() var k: []char :: cui@input() var a: []int :: #[10]int for i(1, 9) do a[i] :: cui@inputInt() end for if(n < ^k) do cui@print("-1\n") ret elif(n > ^k) for i(1, 9) do cui@print("\{i}".repeat(a[i])) end for do cui@print("\n") ret end if block var ans: []char :: #[n]char var j: int :: 0 for i(9, 1, -1) for(1, a[i]) do ans[j] :: '0'.offset(i) do j :+ 1 end for end for if(ans <= k) do cui@print("-1\n") ret end if end block var ans: []char :: #[n]char var greaterFlag: bool :: false var smallerFlag: bool :: false for i(0, n - 1) if(!smallerFlag) for j(1, 9) if(a[j] <> 0) if(greaterFlag) do a[j] :- 1 do ans[i] :: '0'.offset(j) skip i elif('0'.offset(j) = k[i]) do a[j] :- 1 do ans[i] :: '0'.offset(j) skip i elif('0'.offset(j) > k[i]) do greaterFlag :: true do a[j] :- 1 do ans[i] :: '0'.offset(j) skip i end if end if end for do smallerFlag :: true end if for j(9, 1, -1) if(a[j] <> 0) do a[j] :- 1 do ans[i] :: '0'.offset(j) skip i end if end for end for if(ans <= k) do @permutationNext(ans) end if do cui@print("\{ans}\n") end func func permutationNext(array: []char): bool var left: int :: ^array - 2 while(left >= 0 & array[left] >= array[left + 1]) do left :- 1 end while if(left < 0) ret false end if var right: int :: ^array - 1 while(array[left] >= array[right]) do right :- 1 end while var tmp: char :: array[left] do array[left] :: array[right] do array[right] :: tmp do left :+ 1 do right :: ^array - 1 while(left < right) do tmp :: array[left] do array[left] :: array[right] do array[right] :: tmp do left :+ 1 do right :- 1 end while ret true end func