1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 2E5;
const int K = 5E5;
const int G = 1E9;

int g[N + 1], a[N + 1];

inline int readnum()
{
   int a = 0, c;
   while((c = getchar() - '0') < 0);
   for(; c >= 0; c = getchar() - '0') a = 10*a + c;
   return a;
}

inline int update(const int k)
{
   return a[k] ? (a[k] = update(a[k])) : k;
}

int main()
{
   const int n = readnum();
   const int m = readnum();
   const int k = readnum();

   for(int i = 1; i <= n; i++)
   {
      g[i] = readnum();
   }

   for(int i = 0; i < m; i++)
   {
      const int j = readnum();
      a[j] = readnum();
   }

   for(int i = 1; i <= n; i++)
   {
      update(i);
   }

   for(int i = 1; i <= n; i++)
   {
      if (!a[i])
      {
         a[i] = i;
      }
   }

   long long ws = 0;

   for(int i = 0; i < k; i++)
   {
      const int c = readnum();
      const int d = readnum();

      if (a[c] == a[d])
      {
         const int w = min(g[c], g[d]);
         g[c] -= w;
         g[d] -= w;
         ws += 2*w;
      }
   }

   printf("%lld", ws);
}