#P5319. [BJOI2019] 奥术神杖

[BJOI2019] 奥术神杖

Description

The war on the continent of Bezorath against the disaster legion has reached a stalemate. The elves who have lived on Bezorath for generations began searching for ancient artifacts left by the gods, hoping to use their mysterious power to defeat the disaster legion.

After paying a painful price, the elves retrieved a well-preserved staff from a perilous ancient battlefield. However, after the “Twilight of the Gods War”, which all history books consider taboo, the arcane gems inlaid on the staff were already damaged, and its divine power was almost completely exhausted. The elf leaders decided, in the Supreme Council, to mobilize the whole nation to collect the remaining arcane gems, and offered a huge reward to craftsmen across the land to repair the staff.

As the 501st inheritor of the divine arts lineage, you have accepted this difficult and sacred mission.

From left to right, the staff has nn arcane gems inlaid. There are 1010 types of arcane gems, represented by the digits "0123456789". Some positions are already damaged, represented by ".". You need to fill every damaged position using intact arcane gems (the number of each type is unlimited, and you are not allowed to replace gems that are not damaged). An ancient spellbook records mm spells (Si,Vi)(S_i, V_i), where SiS_i is a non-empty digit string, and ViV_i is the divine power that this combination can trigger.

The initial divine power of the staff is Magic=1Magic = 1. Whenever the staff contains a consecutive segment of gems equal to SiS_i, the divine power MagicMagic is multiplied by ViV_i. But if the staff contains too many spells, it becomes impure and its power decreases: let cc be the number of spells contained in the staff (if the spell type is the same but appears at different positions, it is counted multiple times). The final divine power of the staff is Magicc\sqrt[c]{Magic}. (If c=0c = 0, then the final divine power of the staff is 11.)

For example, if there are two spells (01,3)(01, 3) and (10,4)(10, 4), then the divine power of the staff "0101" is 3×4×33\sqrt[3]{3 \times 4 \times 3}.

You need to maximize the final divine power of the repaired staff, and output any one solution.

Input Format

The first line contains two positive integers nn and mm, representing the number of gems and the number of spells.

The second line contains a string TT of length nn, representing the initial staff.

The next mm lines each contain a non-empty digit string SiS_i and a positive integer ViV_i, representing each spell.

Output Format

Output the gems inlaid on the final staff from left to right. If there are multiple solutions, output any one.

4 3
....
1 2
2 2
3 1
2019
5 4
..0..
0 2
00 2
01 4
10 3
11012
18 6
...2.1.0.1..1.0..1
011 6
111 4
010 12
121 7
101 5
10 3
121211203112120121

Hint

Sample 1: the final divine power of the staff is 22.

Sample 2: the final divine power of the staff is 2×3×43\sqrt[3]{2 \times 3 \times 4}.

Let s=i=1mSis = \sum_{i=1}^{m} |S_i|, i.e. the sum of the lengths of all spell strings.

Translated by ChatGPT 5