PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000138. Rắn độc đào thoát (Snake Escaping)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Rắn độc đào thoát (Snake Escaping)

Snake Escaping

Source: Japanese Olympiad in Informatics

Version: Phuoc Hung OJ Extended

Problem Statement

The JOI Laboratory keeps 2L2^L poisonous snakes numbered from 00 through 2L−12^L-1. Each snake has exactly LL body parts, ordered from head to tail. Every part is either blue or red.

Write snake ii in binary using exactly LL digits, from the most significant to the least significant bit. A 0 denotes a blue part, whereas a 1 denotes a red part.

Every snake has an integer toxicity between 00 and 99. The string SS has length 2L2^L; its character at position i+1i+1 (one-based string indexing) gives the toxicity of snake ii.

The laboratory receives reports for QQ days. The report for day dd is a string TdT_d with LL characters. For each body part:

  • 0 requires the part to be blue;
  • 1 requires the part to be red;
  • ? provides no information about the color of the part.

All reports are accurate. Escaped snakes are captured the same day and may escape again on subsequent days. Thus, each day's report is considered independently of previous days.

For each day, compute the sum of toxicities of all snakes whose identifiers are consistent with that day's report. Count each matching snake exactly once.

Input

  • The first line contains integers L,QL,Q: the number of body parts per snake and the number of reporting days.
  • The second line contains string SS of exactly 2L2^L digits, giving the toxicities in increasing snake-identifier order.
  • Each of the next QQ lines contains a length-LL string TdT_d describing the report for day dd.

Output

Print QQ lines. Line dd contains one integer: the sum of toxicities of all snakes matching TdT_d.

Subtasks

  • Subtask 1 (5 points): 1≤L≤101\le L\le10; 1≤Q≤1 0001\le Q\le1\,000; ∣S∣=2L|S|=2^L; SS contains digits 0–9; each TdT_d has length LL and contains only 0, 1, ?.
  • Subtask 2 (7 points): 1≤L≤101\le L\le10; 1≤Q≤1 000 0001\le Q\le1\,000\,000; ∣S∣=2L|S|=2^L; SS contains digits 0–9; each TdT_d has length LL and contains only 0, 1, ?.
  • Subtask 3 (10 points): 1≤L≤131\le L\le13; 1≤Q≤1 000 0001\le Q\le1\,000\,000; ∣S∣=2L|S|=2^L; SS contains digits 0–9; each TdT_d has length LL and contains only 0, 1, ?.
  • Subtask 4 (53 points): 1≤L≤201\le L\le20; 1≤Q≤50 0001\le Q\le50\,000; ∣S∣=2L|S|=2^L; SS contains digits 0–9; each TdT_d has length LL and contains only 0, 1, ?.
  • Subtask 5 (25 points): 1≤L≤201\le L\le20; 1≤Q≤1 000 0001\le Q\le1\,000\,000; ∣S∣=2L|S|=2^L; SS contains digits 0–9; each TdT_d has length LL and contains only 0, 1, ?.

Examples

Example 1

Input

3 5
12345678
000
0??
1?0
?11
???

Output

1
10
12
12
36

Explanation

There are 23=82^3=8 snakes numbered 00 through 77, with toxicities 1,2,3,4,5,6,7,81,2,3,4,5,6,7,8. For 000, only snake 00 matches: 11. For 0??, snakes 0,1,2,30,1,2,3 match: 1+2+3+4=101+2+3+4=10. For 1?0, snakes 44 (100) and 66 (110) match: 5+7=125+7=12. For ?11, snakes 33 and 77 match: 4+8=124+8=12. Finally, ??? matches all eight snakes, giving 1+2+3+4+5+6+7+8=361+2+3+4+5+6+7+8=36.