#P8147. [JRKSJ R4] Salieri

    ID: 6315 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>二分2022洛谷原创O2优化主席树虚树AC 自动机

[JRKSJ R4] Salieri

Description

Salieri discovered nn patterns for making music. He represents the ii-th pattern as a string sis_i, and the initial beauty of this pattern is viv_i.
Now Salieri wants to produce mm pieces of music. Each time, his inspiration can be represented as a string SS. Let cnticnt_i be the number of occurrences of sis_i in SS. Then the final beauty of the piece made using pattern ii is wi=cnti×viw_i=cnt_i\times v_i.
Of course, Salieri wants the final beauty to be as large as possible, but he found that under this inspiration, the top k1k-1 most beautiful pieces have already been made by Mozart, so he can only make the kk-th most beautiful piece. Please output this final beauty.

Formal statement: Given nn strings sis_i, each with a weight viv_i. For each of the mm queries, you are given a string SS and a constant kk. Let cnticnt_i be the number of occurrences of sis_i in SS. Find the kk-th largest value among cnti×vicnt_i\times v_i.

Input Format

The first line contains two integers n,mn,m.
The next nn lines each contain a string sis_i and an integer viv_i.
The next mm lines each contain a string SS and an integer kk.

Output Format

Output one integer per line, representing the answer.

4 2
ab 2
a 2
ba 2
b 1
bbaba 2
aab 1
4
4
15 4
ba 18
cbc 74
aac 54
ba 77
a 66
c 96
cdb 47
dc 45
cb 62
db 88
dda 93
db 34
b 81
acd 100
da 80
bcaacbbdcbabcda 4
bccac 3
abdbaca 5
cbdaaaacaaca 3
124
66
77
108

Hint

Let LL be the total length of all ss.

Subtask\text{Subtask} n,mn,m\le LL\le Special property Score
11 10310^3 5×1035\times10^3 None 1010
22 10510^5 2020
33 10510^5 5×1055\times10^5 k=1k=1 1010
44 3×1043\times10^4 2×1052\times10^5 k5k\le5 2020
55 None
66 10510^5 5×1055\times10^5

For 100%100\% of the testdata, 1n,m1051\le n,m\le10^5, L5×105L\le5\times10^5.

At all times, S\sum |S| is of the same order as LL. Only four characters a,b,c,d\texttt a,\texttt b,\texttt c,\texttt d will appear in ss and SS. vi103v_i\le10^3, and knk\le n.

QQ截图20220128131353.png

Translated by ChatGPT 5