You have a sequence of length , initially equal to .
For each symbol , you are given a positive integer , its value.
There are commands. Command is specified by two distinct symbols and . Executing it simultaneously replaces every current occurrence of in with . If does not occur in , the sequence remains unchanged, but the command still counts as executed.
You may execute a finite sequence of commands in any order. Each command may be executed multiple times, but every given command must be executed at least once.
Let be the set of symbols occurring in after you finish. The final value is : each distinct symbol contributes its value exactly once, regardless of its number of occurrences.
Find the maximum possible final value.
Execute , then . The final sequence is , with value . Symbol must be absent at the end because its outgoing command must be executed and no command can produce it.
Execute , , , and again. The final sequence is , with value . The first command executed makes at least one symbol absent, and the number of distinct symbols can never increase. Since the smallest symbol value is , the value cannot exceed .
There are no commands, so every symbol remains present. Their total value is .
Execute , , , , , , and . The final sequence is , so the value is . The command executed while is absent still counts as executed. At least one symbol must become absent, so losing only the smallest value, , is optimal.
Execute , , , , , , , and . The final sequence is , with value .
Symbol must be absent at the end. Each of the pairs and must lose a symbol when one of its internal commands is first executed. Only a command with source can restore a missing symbol in either pair, and such a command can change the sequence at most once. Therefore at least one additional symbol must remain absent. The shown execution loses values and , the smallest possible total loss.