Which pairs contribute to the answer?
Call the initially present viruses original viruses and those inserted during changes new viruses. Pairs of original viruses contribute equally to both extinction risks. The answer is therefore the sum of similarities over unordered pairs containing at least one new virus. All terms are nonnegative, so subtracting the original-only risk from the full risk gives the required absolute difference.
Store the strings in a trie. Each non-root node represents a nonempty prefix. The number of nodes on the common prefix of two strings equals their similarity. If a node's subtree contains original viruses and new viruses, its contribution is
Summing over all non-root nodes gives the answer. A pair of new viruses must be counted only once. Distinct virus types with identical structures still count separately.
Subtask 1: direct simulation
After each change, explicitly modify every string and append the new viruses. Compute the longest common prefix for every pair containing a new virus. With and , the time complexity is and the memory complexity is . This subtask has and .
Subtask 2: one change
Let the only change be . If original structures start with after the mutation, the answer is . An original string qualifies exactly when it did not initially start with , or when its first two characters were both . A one-character string becomes empty. Writing , the solution takes time.
Subtask 3: only the letter A
Every new virus always has an empty or one-character structure. If there are nonempty original structures and nonempty new structures, the answer is .
An original string of length first becomes empty after changes. It then alternates between empty and length one. Count initial lengths and maintain, separately for both parities, the number of originals that have already reached zero. At time , an original structure is empty exactly when its initial length is at most and has the same parity as .
A virus inserted at time has length one at time exactly when is even. Maintain insertion totals by time parity as well. The total time complexity is , with memory.
Moving the root of the trie
Let be the current root and the mutation letter. Let be the child of , creating an empty node if that child does not exist.
Moving the root from to removes the leading from strings that previously started with . Every other string gains a leading , represented by an edge from the new root to the old root. The old root's former child link must be removed.
Inserting new viruses after the mutation is equivalent to inserting empty strings at the old root before moving it. These strings become exactly the one-character string after the move. Inserting empty strings at the root does not immediately change the answer because no nonempty prefix is added.
After adding these new viruses to the old root's count, let be the counts at before the move. Subtract because becomes the root. Subtract the counts of from those of , and add the contribution of the resulting to the answer. Set the counts of to the total counts. The move itself leaves all other subtree counts unchanged.
Merging subtrees with equal edge labels
If already had a child , it conflicts with , which is also becoming a child. The node represents strings that originally started with . The two subtrees now represent the same prefix and must be merged.
When merging nodes , subtract their previous contributions, combine their counts, and add the contribution of the combined node. A child present on only one side can be transferred unchanged. Children present on both sides under the same letter must also be merged.
The reference solution uses an explicit work stack, avoiding recursive calls even for very long strings. Each merge makes one node unreachable. Thus, the total number of node merges is bounded by the number of nodes ever created.
The initial trie has at most nodes, and each change creates at most one node. For alphabet size , the total time and memory complexities are both . The full task has , giving linear complexity in .
Subtasks 4, 5, and 6
For subtasks 4 and 5, specialize the same algorithm to child arrays of sizes and . Their time and memory complexities are and , respectively.
In subtask 6, exactly one new virus is inserted throughout the entire process. Thus always holds and . A trie implementation using only the contribution solves this subtask in time and memory. The full solution must retain the term for pairs of new viruses.
Integer bounds
At most new viruses are inserted. A virus inserted at step has final length at most . Original-new pairs contribute at most . New-new pairs are maximized by inserting viruses at every step and increasing the length of every existing new structure at each later step. Their maximum contribution is .
The exact maximum answer is therefore , which is at the full bounds. This is attained by using identical initial strings of one letter and alternating mutations of two other letters while inserting viruses each time. The intermediate product before division is also at most , within signed -bit range.
There are at most viruses, and a single-node contribution is at most . The original-only risk is at most . Even if both risks are computed separately, the full risk is at most , safely within signed -bit range. The reference solution directly accumulates prefix contributions and does not evaluate these large polynomial formulas.
At most nodes are allocated, including the dummy node. The reference solution maintains a bitmask of existing child letters and visits only those children while merging. Each full-task node uses bytes, so the node array occupies about MiB. Reserving the maximum capacity in advance avoids holding both old and new arrays during reallocation. The merge work stack and input buffers also fit within the MiB limit.
The validator checks bounds before accumulating an integer. Intermediate seed-hash values are below , random-state multiplication is below , and the generator's product is at most . No judging component requires an integer type wider than bits.
Solution written by GPT6