#P13653. [CERC 2020] Pizzo Collectors

[CERC 2020] Pizzo Collectors

题目描述

Lavish Circle (LC) is the fashionable circular avenue in the residential area of the town. Houses on LC are exceptionally expensive and some of them are currently empty. LC is under heavy control of the town mafia who wants to populate the empty houses with new owners loyal to the mafia. When LC is completely populated, each house owner will live in one house on LC. LC is a circular avenue of houses numbered from 11 to NN, that is for i<Ni < N, ii-th and i+1i+1-th houses are neighboring and also houses NN and 11 are neighboring.

The house owners, both the existing ones and the new ones, fall into few categories according to the sum they can pay the mafia monthly for protecting them. The money is called pizzo and it is typically collected by a person called a pizzo collector (PC). The mafia employs a group of them.

The job of a PC is to go around entire LC exactly once in a month and collect pizzo from the selected houses on the journey. All selected houses on a journey of a PC must have owners of the same pizzo category. The journey must also start and end at the same house, it is a check PC completed the journey correctly. The pizzo is collected from this house only once, at the beginning or at the end of the journey. During his journey, a PC always moves forward by a fixed number of houses, until the PC arrives again to the starting house. That is, the number of houses a PC skips on each move is a non-negative integer dd, which remains constant during the entire journey of this PC. It must hold that (d+1)(d+1) divides NN evenly.

The mafia wants to employ as many PCs as possible. Of course, employing a number of PCs means that some owners quite probably have to pay pizzo more than once in a month, but the mafia does not care... Unfortunately, there is a complication. PCs are peaceful citizens and they do not tend to shoot at each other under normal conditions. However, when two PCs find out that at their respective collection journeys they visit the same set of houses, and it does not matter in which order they visit the houses, the collectors tend to shoot each other and thus attract the police, which is a behavior the mafia wants to avoid at any cost. So, no two collectors who may shoot each other can be employed simultaneously.

The total value of all collected pizzo depends also on the categories of the owners of the newly populated houses. The mafia decides on the category of each new house owner. Obviously the mafia wants to maximize their income. You have been hired as an analyst to find the maximum possible total value of all collected pizzo in one month, when LC gets completely and suitably populated. The mafia is going to decide on the pizzo category of each new house owner based on your recommendations. The number of houses on LC is a non-negative integer power of a prime number.

输入格式

The first line contains integer NN (1N1051 \leq N \leq 10^5), a non-negative integer power of some prime number pp. The second row contains string SS of length NN, which consists of only capital letters of English alphabet and the character "?". The characters of the string represent the houses on LC in the order they appear on LC. The "?" character represents a currently empty house, each of the other characters represent the pizzo category of the house owner.

The next line contains integer kk (1k261 \leq k \leq 26), the number of different pizzo categories. Each of the next kk lines contains an integer pair cic_i viv_i, where cic_i is a capital English character and 1vi1061 \leq v_i \leq 10^6 is the money value which is paid by a house owner of pizzo category cic_i in one visit of a PC to the house.

It is guaranteed that for every category that appears in SS, there is a pair cic_i and viv_i which defines its money value which is paid on the PC's visit.

输出格式

Print the maximum possible total value of all collected pizzo in one month, when LC is completely populated.

4
A?A?
2
A 10
B 25
140
4
A??A
2
A 10
B 25
120