OCC '19 G3 - Binary Game

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 0.6s
Memory limit: 512M

Problem type

Bob is learning binary numbers. To help Bob memorize the first 2k (from 0 to 2k1) binary representations, his teacher, Mr. Ecurb, designs a binary game. In this game, Bob is given a binary sequence S, which only consists of 0 and 1, and 2k substitution rules, where the ith rule (0i2k1) replaces number i's k-bit binary representation with character ci (ci=0 or 1) and generates value vi. If number i's k-bit binary representation occurs in the sequence S, Bob can apply this rule to replace it with character ci and get value vi. Bob's objective is to achieve the maximum value by using these substitution rules on S. Can you write a program to help Bob?

Constraints

For all subtasks:

1|S|300

2k8

Subtask Points Additional constraints
1 28 1|S|50
2 32 1|S|200
3 40 No additional constraints.

Input Specification

The first line contains two integers, |S| and k, the length of the binary sequence and the length of the binary representation.

The second line contains a binary sequence, S.

2k lines of input follow. The ith line contains two integers, ci and vi, a substitution rule to convert the number i's k-bit binary representation to character ci with value vi, (ci=0 or 1, 1vi109).

Output Specification

Print one integer, the maximum value Bob can achieve by using the substitution rules on sequence S.

Sample Input

Copy
3 2
101
1 8
1 8
0 16
1 30

Sample Output

Copy
38

Explanation of Sample Output

There are 4 substitution rules:

  • Rule 1 replaces binary representation 00 with 1 to get value 8
  • Rule 2 replaces binary representation 01 with 1 to get value 8
  • Rule 3 replaces binary representation 10 with 0 to get value 16
  • Rule 4 replaces binary representation 11 with 1 to get value 30

For the input sequence 101, Bob can use Rule 2 to convert S to 11 with value 8 and then use Rule 4 to convert 11 to 1 to get value 30. The total value is 38.


Comments

There are no comments at the moment.