#P1014. Timothy's bottle —— Timothy 的药剂
Timothy's bottle —— Timothy 的药剂
Statement
Timothy plans to collect types of magic potions, numbered , each of which has two forms: a red version and a blue version.
The way to obtain the potion is as follows:
- Direct purchase: Purchasing one bottle of the th type of red version potion costs gold coins;
- Blending and Synthesis: If you already possess the red versions of the th and th potions, you can consume one bottle of each to blend and obtain the blue version of the th potion. The blending process itself does not cost additional gold coins (as long as both raw materials are present).
Timothy doesn't care about the color, and only requires that he ultimately has at least one form (red or blue) of each of the potions. Please calculate the minimum total number of gold coins he needs to pay.
Input
Input an integer in the first line, representing the number of types of potions.
Input integers $a_1, a_2, a_3, \cdots, a_n (1 \leqslant a_i \leqslant 10^4)$ in the second line, representing the prices of directly purchasing one bottle of the th type of red potion in sequence.
Next, there are lines, and the th line contains two integers representing the IDs of the two red potions required to synthesize the th type of blue potion.
Output
Output an integer representing the minimum number of gold coins required to obtain different potions.
Samples
5
2 4 10 1 3
2 3
4 5
1 2
2 5
1 4
16
An optimal solution:
- Purchase the red potions directly, costing ;
- Use red to prepare the rd blue potion, costing ;
The final cost is , satisfying the requirement of having different potions from .
Related
In following contests: