#P10333. [UESTCPC 2024] 打字

[UESTCPC 2024] 打字

Problem Description

You are given a trie with nn nodes, rooted at node 11. For every other node, the edge from its parent to it has a character, and for nodes with the same parent, these characters are all different. The path from the root to each node represents a word, formed by concatenating the characters on the path in order. Each node ii has a weight cic_i, meaning the word represented by this node needs to be written cic_i times.

You have a cache, and you may choose any number of words to store in the cache. When writing a word, typing one character costs aa. After finishing a complete word, you may choose to end the current word and start spelling the next word. The cache will, based on the characters you have already typed, suggest the lexicographically smallest word in the cache that has this typed string as a prefix. At this time, you may pay cost bb to directly output this word, and end the spelling of the current word, then start spelling the next word. Note that a string is also considered a prefix of itself.

Find the minimum total cost to finish spelling all words on the trie.

Input Format

The first line contains three integers n,a,bn,a,b (1n2×105,0a,b104)(1\leq n\leq 2\times 10^5,0\leq a,b\leq 10^4), representing the size of the trie, the cost to type one character, and the cost to output a word using the cache.

Then follow nn lines. On the ii-th line, the input first contains two integers ci,kic_i,k_i (0ci104,0ki<n)(0\leq c_i\leq 10^4,0\leq k_i<n), representing the weight on node ii and the number of children of node ii. Then follow kik_i integers soni,1,soni,2,,soni,kison_{i,1},son_{i,2},\ldots,son_{i,k_i}, representing the indices of the children of node ii, given in increasing lexicographical order of the characters on the edges from node ii to these children.

It is guaranteed that c1=0c_1=0.

Output Format

Output one integer in one line, representing the minimum cost to finish spelling all words on the trie.

4 2 3
0 2 3 2
3 0
2 1 4
4 0
22

Hint

[Sample Explanation]

Assume the children of each node are encoded as a,b,a,b,\ldots in increasing lexicographical order. The optimal strategy is to put aaaa into the cache. Then aaaa can be typed using suggestions at a cost of 3×43\times 4, while aa and bb are typed directly. The total cost is 3×4+2×1×2+2×1×3=223\times 4+2\times 1\times 2+2\times 1\times 3=22.

Translated by ChatGPT 5