#P5578. [PA 2014] Fiolki

[PA 2014] Fiolki

Description

The chemist Gili wants to make a magical potion to save the world.

Gili has nn different liquid substances and nn bottles (both numbered from 11 to nn). Initially, bottle ii contains gig_i grams of substance ii.

Gili needs to perform several steps to make the potion. In step ii, she pours all the liquid from bottle aia_i into bottle bib_i. After that, bottle aia_i will no longer be used. You may assume the bottles have unlimited capacity.

Gili knows that some pairs of substances will react together and produce precipitate. The reaction is: 11 gram of substance cic_i and 11 gram of substance did_i produce 22 grams of precipitate, and the reaction continues until one reactant is exhausted. The precipitate produced will not react with any substance.

When there is more than one pair of substances that can react in the same bottle, Gili knows the order in which they react. After each pouring, Gili waits until all reactions finish before performing the next step.

Gili wants to know the total amount of precipitate produced during the whole process.

Input Format

The first line contains three integers n,m,kn,m,k, representing the number of bottles (i.e. the number of substances), the number of steps, and the number of possible reactions.

The second line contains nn integers g1,g2,,gng_1,g_2,\dots,g_n, representing the initial mass of the substance in each bottle.

The next mm lines each contain two integers ai,bia_i,b_i, representing step ii. It is guaranteed that aia_i will not appear in any later step.

The next kk lines each contain a pair of substances ci,dic_i,d_i that can react, given in the priority order of reactions. The same reaction will not appear more than once.

Output Format

Output the total mass of precipitate produced.

3 2 1
2 3 4
1 2
3 2
2 3
6

Hint

For 100%100\% of the testdata, 0m<n2×1050 \le m < n \le 2 \times 10^5, 0k5×1050 \le k \le 5 \times 10^5, 1gi1091 \le g_i \le 10^9, 1ai,bi,ci,din1 \le a_i,b_i,c_i,d_i \le n, aibia_i \ne b_i, cidic_i \ne d_i.

Translated by ChatGPT 5