Macht of the Lemon Scent, Denken's teacher and one of the Seven Sages of Destruction. He is famous for his magic that turns everything into lemons. The great mage Frieren must dispel Macht's magic to restore Denken's hometown, which has been turned into lemons.
Macht's magic circle consists of vertices numbered from and is in the form of a tree with root . Each edge is a directed edge pointing from a child to its parent. The -th edge is a directed edge pointing from vertex to vertex with a capacity of .
For Frieren to successfully dispel the magic, she must find the minimum capacity among all edges. That is, she must find the minimum value of . However, Frieren only knows , , and . She does not know . Therefore, she must obtain information through mana manipulation.
Frieren can manipulate mana up to times, and a single manipulation is performed as follows.
- Select some edges and increase their capacity to .
- Determine the amount of mana to flow into each vertex. That is, define an array of length , and flow amount of mana into vertex .
Right after the manipulation, , the amount of mana flowing through vertex is calculated recursively as follows.
Here, means the edge directed from to , and is the capacity of that edge.
After the manipulation is over, Frieren can only know : the amount of mana flowing through the root vertex . In addition, each mana manipulation is performed independently of each other. In other words, the magic circle is immediately restored to its original state after the manipulation is over.
Help Frieren dispel Macht's magic by finding the minimum capacity among all edges.
Implementation Details
Before implementing the functions, you must include the "macht.h" header file using #include "macht.h".
You must implement the following function.
void unravel(std::vector U, std::vector V)
- : arrays of length representing the edges of the magic circle
- This function is called exactly once per test case.
- Inside this function, you can manipulate mana using the
triggerfunction below.
long long trigger(std::vector R, std::vector F);
- : an array of length representing whether each edge is reinforced ()
- If , the -th edge is reinforced to have the capacity of .
- : an array of length representing the amount of mana to flow into each vertex ()
- This function returns the amount of mana flowing through the root vertex .
- If the above conditions are violated, it returns .
- This function can be called at most times.
- Finally, to submit the answer, you must call the function below.
void answer(int min_W);
- : the value of the minimum capacity (i.e., ) ()
- It must be called exactly once.
Constraints
- .
- ().
- .
Subtasks
Samples
Consider the following function call.
unravel(3, [0, 0], [1, 2])
The following is one of the possible sequences of function calls.
Called function | Returned value |
| |
| |
|
From the result of the second trigger call, it can be seen that the weight of the -st edge (the edge connecting vertex and ) is exactly .
Also, by combining this information with the result of the first trigger call, it can be seen that the weight of the -th edge (the edge connecting vertex and ) must be or more. Otherwise, the value of the mana flowing to the root would have been less than .
Therefore, it can be confirmed that the minimum capacity among all edges is .
Sample Grader
The input format for the sample grader is as follows.
The sample grader outputs the following.
- : the number of times the
triggerfunction was called - : the value submitted using the
answerfunction
Note that the sample grader may differ from the grader used in actual judging.