#P16701. [MCO 2026] 制造连招
[MCO 2026] 制造连招
Problem Description
Dragon Evirir is playing a video game, and it wants to maximize the total damage by casting skills continuously.
Evirir can use different skills, numbered . For each skill , its color is and its damage is . There are skill links in total, and each link is a pair of skills , where .
A combo is a skill sequence of length , , such that for all , is one of the skill links. The problem guarantees that the skill links in the input do not form a cycle. In other words, it is impossible to build a combo that uses the same skill twice.
The total power of the combo is computed as follows. Let there be a multiplier , initially . For , do the following in order:
- If , do nothing.
- Otherwise:
- If skills and have the same color, multiply by .
- If skills and have different colors, set to .
- Then, the power of skill is .
The total power of the combo is the sum of the powers of all cast skills.
For example, suppose a combo contains the following skills in order: , , , , , , , where means a skill with damage and color . The computation is as follows:
| Color | |||||||
| Damage | |||||||
| Multiplier | |||||||
| Power | |||||||
Therefore, the total power of this combo is .
Clearly, Evirir wants to cast a combo with the maximum total power. To do this, it installed a cheat that can change the color of any skill to a fixed color . Evirir can use this cheat at most times (that is, it can modify the colors of at most skills).
Find the maximum total power Evirir can achieve. If the maximum total power is strictly greater than , output .
Input Format
The first line contains four integers , , , and , separated by spaces.
Then there are lines. The -th line contains two integers and , separated by a space.
Then there are lines. The -th line contains two integers and , separated by a space.
Output Format
Output one integer, the maximum possible total power. If it is strictly greater than , output . If it is exactly equal to , you should output .
6 7 2 1
3 1
2 3
4 1
5 2
1 5
2 0
0 1
3 4
2 3
0 3
3 5
1 2
0 5
65
5 4 0 5
2 0
3 1
2 0
3 0
13 0
0 1
1 2
2 3
3 4
65
4 3 4 0
100000000 0
100000000 1
100000000 2
100000000 3
0 1
1 2
2 3
-1
Hint
Hint
This sample satisfies Subtasks 4 and 7.
A visualization is given below. An arrow from skill to skill means there is a skill link , i.e., Evirir can use skill immediately after skill . The large circle in the upper-left corner explains what each number means.
:::align{center}
:::
Evirir can change the colors of at most skills to color . It can change the colors of skills and to , and then cast the skill sequence . The total power is $(1 \times 3) + (2 \times 2) + (4 \times 4) + (8 \times 5) + (1 \times 2) = 65$.
An example that is not a combo is the skill sequence , because is not one of the skill links.
This sample satisfies Subtasks 2, 3, 4, 5, 6, and 7.
There are skills and skill links. , so Evirir cannot modify the color of any skill. The optimal combo is casting . The total power is $(2 \times 1) + (3 \times 1) + (2 \times 1) + (3 \times 2) + (13 \times 4) = 65$.
This sample satisfies Subtasks 1, 3, 4, 5, 6, and 7.
Since , Evirir can change the colors of skills , , and to color . The optimal combo is casting . The total power is $(10^8 \times 1) + (10^8 \times 2) + (10^8 \times 4) + (10^8 \times 8) > 10^9$. Since the maximum possible total power is strictly greater than , output .
Scoring
For all testdata, the input satisfies the following Constraints:
- $0 \le M \le \min\left(10^5, \frac{N(N - 1)}{2}\right)$
- For all ,
- For all ,
- . Note that may equal , i.e., a color that is not used.
- For all , and
- For all , the pairs are all distinct.
- It is guaranteed that the skill links do not form a cycle. In other words, it is impossible to build a combo that uses the same skill twice.
| Subtask | Points | Additional Constraints |
|---|---|---|
| , and for all , . | ||
| , and for all , . | ||
| , and for all , . | ||
| , and for all , . | ||
| , and for all , . | ||
| --- |
Translated by ChatGPT 5