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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cstdio>
#include <algorithm>
#include <vector>

static const int MAX_N = 200005;
static const bool verbose = false;
int n, m, k;

int fio[MAX_N];
std::vector<std::pair<int, int> > reactions[MAX_N];
std::pair<int, int> sequence[MAX_N];
std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > > reactions_order;

long long result;

struct Node {
	int father;
	int children[2];
	int root;
	bool visited;
};
Node nodes[MAX_N * 2];
int nodes_count;

int find_root(int x) {
	if (nodes[x].root == x)
		return x;
	return nodes[x].root = find_root(nodes[x].root);
}

void make_union_on_element_root(int u, int v, int element) {
	nodes[find_root(u)].root = element;
	nodes[find_root(v)].root = element;
}

int buildTree()
{
	// n = liczba fiolek
	for (int i = 0; i < n; ++i) {
		nodes[i].children[0] = -1;
		nodes[i].children[1] = -1;
		//nodes[i].father = i;
		nodes[i].root = i;
	}
	nodes_count = n;
	
	//wezly powstaja w kolejnosci operacji mieszania!
	for (int i = 0; i < m; ++i) {
		int s1 = sequence[i].first;
		int s2 = sequence[i].second;
		int r1 = find_root(s1);
		int r2 = find_root(s2);
		nodes[nodes_count].children[0] = r1;
		nodes[nodes_count].children[1] = r2;
		nodes[nodes_count].root = nodes_count;
		make_union_on_element_root(r1, r2, nodes_count);
		++nodes_count;
	}
}

int find(int x) {
	if (nodes[x].father == x)
		return x;
	return nodes[x].father = find(nodes[x].father);
}

void make_union_on_element(int u, int v, int element) {
	nodes[find(u)].father = element;
	nodes[find(v)].father = element;
}

void tarian(int current) {
	nodes[current].father = current;
	for (int i = 0; i < 2; ++i) {
		int v = nodes[current].children[i];
		if (v >= 0) {
			tarian(v);
			make_union_on_element(current, v, current);
		}
	}

	nodes[current].visited = true;
	int root = find_root(current);
	if (current < n) { // tu sa pytania
		for (int i = 0; i < reactions[current].size(); ++i) {
			int v = reactions[current][i].first;
			int order = reactions[current][i].second;
			if (nodes[v].visited &&  find_root(v) == root) {
				int anc = nodes[find(v)].father;
				
				if (verbose)
					printf("Father of %d %d is %d\n", current, v, anc);
				reactions_order.push_back(std::make_pair(std::make_pair(anc, order),
														 std::make_pair(current, v)));
			}
		}
	}
}

void tarian() {
	if (verbose)
		printf("Nodes count %d\n", nodes_count);
	nodes[nodes_count].father = nodes_count;
	for (int i = 0; i < nodes_count; ++i)
		if (nodes[i].root == i)
			tarian(nodes[i].root);
}

void simulate() {
	std::stable_sort(reactions_order.begin(), reactions_order.end());
	for (int i = 0; i < reactions_order.size(); ++i) {
		if (verbose) {
			for(int j = 0; j < n; ++j)
				printf("%d ", fio[j]);
			printf("\n");
		}
		int s1 = reactions_order[i].second.first;
		int s2 = reactions_order[i].second.second;
		if (verbose)
			printf("Mieszam %d %d\n", s1, s2);
		int dirt = std::min(fio[s1], fio[s2]);
		fio[s1] -= dirt;
		fio[s2] -= dirt;
		result += 2 * dirt;
	}
	
	if (verbose) {
		for(int j = 0; j < n; ++j)
			printf("%d ", fio[j]);
		printf("\n");
	}
}

int main() {
	scanf("%d%d%d", &n, &m, &k);
	
	for (int i = 0; i < n; ++i)
		scanf("%d", fio + i);
	
	for (int i = 0; i < m; ++i)
	{
		scanf("%d%d", &sequence[i].first, &sequence[i].second);
		--sequence[i].first;
		--sequence[i].second;
	}
	
	int t1, t2;
	for (int i = 0; i < k; ++i) {
		scanf("%d%d", &t1, &t2);
		reactions[t1-1].push_back(std::make_pair(t2-1, i));
		reactions[t2-1].push_back(std::make_pair(t1-1, i));
	}
	
	buildTree();
	tarian();
	
	simulate();
	
	printf("%lld\n", result);
	
	return 0;
}