QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100

#8606. Character Merging

Statistics

You are given a binary string of length $n$. You can merge $k$ adjacent characters into a new character and gain a certain score. The resulting character and the score are determined by these $k$ characters. You need to find the maximum score you can obtain.

Input

The first line contains two integers $n$ and $k$.

The next line contains a binary string of length $n$, representing the initial string.

The next $2^k$ lines each contain a character $c_i$ and an integer $w_i$, where $c_i$ is the resulting character obtained from the $i$-th merging scheme (corresponding to the $i$-th binary string of length $k$ when sorted in ascending order), and $w_i$ is the score obtained from that scheme.

Output

Output a single integer representing the answer.

Examples

Input 1

3 2
101
1 10
1 10
0 20
1 30

Output 1

40

Note

The 3rd to 6th lines represent the 4 merging schemes for binary strings of length 2. $00 \to 1$ gives 10 points, $01 \to 1$ gives 10 points, $10 \to 0$ gives 20 points, and $11 \to 1$ gives 30 points.

Constraints

Data ID $n$ $k$ $w_i$
0 $=10$ $2$ $\le 10^5$
1 $=15$ $3$ $\le 10^5$
2 $=20$ $4$ $\le 10^5$
3 $=25$ $5$ $\le 10^5$
4 $\le 50$ $5$ $\le 10^6$
5 $\le 50$ $6$ $\le 10^6$
6 $\le 50$ $7$ $\le 10^6$
7 $\le 50$ $8$ $\le 10^6$
8 $\le 100$ $3$ $\le 10^7$
9 $\le 100$ $4$ $\le 10^7$
10 $\le 100$ $5$ $\le 10^7$
11 $\le 100$ $6$ $\le 10^7$
12 $\le 200$ $5$ $\le 10^8$
13 $\le 200$ $6$ $\le 10^8$
14 $\le 200$ $7$ $\le 10^8$
15 $\le 200$ $8$ $\le 10^8$
16 $\le 300$ $5$ $\le 10^9$
17 $\le 300$ $6$ $\le 10^9$
18 $\le 300$ $7$ $\le 10^9$
19 $\le 300$ $8$ $\le 10^9$

For 100% of the data, $n \ge 1$, $0 \le c_i \le 1$, $w_i \ge 1$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.