atcoder#ABC301D. [ABC301D] Bitmask

[ABC301D] Bitmask

Score : 400400 points

Problem Statement

You are given an integer NN and a string SS consisting of 0, 1, and ?. Let TT be the set of values that can be obtained by replacing each ? in SS with 0 or 1 and interpreting the result as a binary integer. For instance, if S=S= ?0?, we have $T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace$.

Print (as a decimal integer) the greatest value in TT less than or equal to NN. If TT does not contain a value less than or equal to NN, print -1 instead.

Constraints

  • SS is a string consisting of 0, 1, and ?.
  • The length of SS is between 11 and 6060, inclusive.
  • 1N10181\leq N \leq 10^{18}
  • NN is an integer.

Input

The input is given from Standard Input in the following format:

SS

NN

Output

Print the answer.

?0?
2
1

As shown in the problem statement, T={0,1,4,5}T=\lbrace 0,1,4,5\rbrace. Among them, 00 and 11 are less than or equal to NN, so you should print the greatest of them, 11.

101
4
-1

We have T={5}T=\lbrace 5\rbrace, which does not contain a value less than or equal to NN.

?0?
1000000000000000000
5