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
73
74
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>

#define ST first
#define ND second

using namespace std;

const int MX = 2e5+10;

typedef long long int LL;

int n, m, k, b[MX], alias[MX];
pair<int, int> s[MX];
pair<int, int> e[MX*3];
vector<int> g[MX];
set<int> F[MX];
LL W;

int other(int u, pair<int, int> E) {
	return E.ST == u ? E.ND : E.ST;
}

void merge(int A, int B) {
	vector<pair<int, int> > Q;
	vector<int> nodes;
	while (!F[A].empty()) {
		int u = *F[A].begin();
		F[A].erase(F[A].begin());
		for (int i = 0; i < g[u].size(); i++) {
			int v = other(u, e[g[u][i]]);
			if (F[B].find(v) != F[B].end()) Q.push_back(make_pair(g[u][i], u));
		}
		nodes.push_back(u);
	}
	sort(Q.begin(), Q.end());
	for (int i = 0; i < int(Q.size()); i++) {
		int u = Q[i].ND;
		int v = other(u, e[Q[i].ST]);
		if (b[u] > 0 && b[v] > 0) {
			if (b[u] >= b[v]) {
				W = W + b[v];
				b[u] -= b[v];
				b[v] = 0;
				F[B].erase(F[B].find(v));
			}
			else {
				W = W + b[u];
				b[v] -= b[u];
				b[u] = 0;
			}
		}
	}
	for (int i = 0; i < int(nodes.size()); i++) if (b[nodes[i]] != 0) F[B].insert(nodes[i]);
}

int main() {
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; i++) scanf("%d", &b[i]), F[i].insert(i), alias[i] = i;
	for (int i = 1; i <= m; i++) scanf("%d%d", &s[i].ST, &s[i].ND);
	for (int i = 1; i <= k; i++) {
		scanf("%d%d", &e[i].ST, &e[i].ND);
		g[e[i].ST].push_back(i);
		g[e[i].ND].push_back(i);
	}
	for (int i = 1; i <= m; i++) {
		if (int(F[alias[s[i].ST]].size()) > int(F[alias[s[i].ND]].size())) swap(alias[s[i].ST], alias[s[i].ND]);
		merge(alias[s[i].ST], alias[s[i].ND]);
	}
	printf("%lld\n", W+W);
	return 0;
}