#P1014. Timothy's bottle —— Timothy 的药剂

Timothy's bottle —— Timothy 的药剂

Statement

Timothy plans to collect nn types of magic potions, numbered 1n1\sim n, 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 iith type of red version potion costs aia_i gold coins;
  • Blending and Synthesis: If you already possess the red versions of the bib_ith and cic_ith potions, you can consume one bottle of each to blend and obtain the blue version of the iith 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 1n1\sim n potions. Please calculate the minimum total number of gold coins he needs to pay.

Input

Input an integer n(1n105)n(1\leqslant n\leqslant 10^5) in the first line, representing the number of types of potions.

Input nn 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 iith type of red potion in sequence.

Next, there are nn lines, and the iith line contains two integers bi,ci(1bi,cin)b_i,c_i(1\leqslant b_i,c_i\leqslant n) representing the IDs of the two red potions required to synthesize the iith type of blue potion.

Output

Output an integer representing the minimum number of gold coins required to obtain nn 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 1st,2nd,4th,5th1st, 2nd, 4th, 5th red potions directly, costing 2+4+1+3=102+4+1+3=10;
  • Use 11 red 22 to prepare the 33rd blue potion, costing 2+4=62+4=6;

The final cost is 10+6=1610+6=16, satisfying the requirement of having different potions from 151\sim 5.