Bob is learning binary numbers. To help Bob memorize the first (from
to
) binary representations, his teacher, Mr. Ecurb, designs a binary game. In this game, Bob is given a binary sequence
, which only consists of
0
and 1
, and substitution rules, where the
rule (
) replaces number
's
-bit binary representation with character
(
) and generates value
. If number
's
-bit binary representation occurs in the sequence
, Bob can apply this rule to replace it with character
and get value
. Bob's objective is to achieve the maximum value by using these substitution rules on
. Can you write a program to help Bob?
Constraints
For all subtasks:
Subtask | Points | Additional constraints |
---|---|---|
No additional constraints. |
Input Specification
The first line contains two integers, and
, the length of the binary sequence and the length of the binary representation.
The second line contains a binary sequence, .
lines of input follow. The
line contains two integers,
and
, a substitution rule to convert the number
's
-bit binary representation to character
with value
, (
,
).
Output Specification
Print one integer, the maximum value Bob can achieve by using the substitution rules on sequence .
Sample Input
3 2
101
1 8
1 8
0 16
1 30
Sample Output
38
Explanation of Sample Output
There are substitution rules:
- Rule 1 replaces binary representation
with
to get value
- Rule 2 replaces binary representation
with
to get value
- Rule 3 replaces binary representation
with
to get value
- Rule 4 replaces binary representation
with
to get value
For the input sequence , Bob can use Rule 2 to convert
to
with value
and then use Rule 4 to convert
to
to get value
. The total value is
.
Comments