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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
using namespace std;
#define LOG(args...) 
//#define LOG(args...) fprintf(stderr, args)
#define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++)
#define MAXN 210000
#define NLIM 8000
typedef pair<int,int> para;
int n,m,k;

struct node {
	vector<node*> P;
	vector<node*> L;
	vector<para> Q;
	int id;
	int rootId;
	int depth;
} t[MAXN*2];

void attachNode(node* parent, node* child) {
	parent->L.push_back(child);
	// if(child->P.empty() == false) {
	// 	LOG("reattachment! %d to %d", child->id, parent->id);
	// }
	// child->P.push_back(parent);
}

node * tp[MAXN];

void calcSubtree(node* p, vector<node*>& S) {
	int d = (p->depth = S.size());

	//LOG("down %x i:%d,d:%d,rootId:%d\n", p, p->id, p->depth,p->rootId); 
	int lvl = 0;
	
	if(d) while(!(((1<<lvl)-1) & d)) {
		int j = 1<<lvl;
		p->P.push_back(S[d-j]);
		lvl++;
	}
	
	S.push_back(p);
	p->rootId = S[0]->id;
	FOR(i, p->L.size()){
		calcSubtree(p->L[i], S);
	}
	S.pop_back();
	//LOG("back id %d:%x\n", p->id, p);
}

void calcTree(node* root) {
	vector<node*> S;
	calcSubtree(root, S);
}


node * goUp(node* p, int target) {
	LOG("up! %x id:%d, d:%d, target: %d\n",p, p->id, p->depth, target);
	int diff = p->depth - target;
	
	int lvl = 0;
	while((1<<(lvl+1)) <= diff) lvl++;
	
	// LOG("up! lvl:%d\n", lvl);	
	while(p->depth > target) {
		int j = min(lvl, (int)p->P.size()-1);
	
		// LOG("up! j:%d\n", j);	
		p = p->P[j];
		// LOG("upped! %x id:%d, d:%d, target: %d\n",p, p->id, p->depth, target);
		diff = p->depth - target;
		while((1<<lvl) > diff) lvl--;
	}	
	return p;
}

node * getCommonSubtree(node* a, node* b) {
	
	// LOG("a:%x[i:%d,d:%d] b:%x[i:%d, d:%d]\n", a, a->id, a->depth, b, b->id, b->depth);
	int lvl = 0;
	while(a->P[lvl] != b->P[lvl]) {
		while(lvl < a->P.size()-1 && a->P[lvl+1] != b->P[lvl+1]) lvl++;
		a = a->P[lvl];
		b = b->P[lvl];
		
		LOG("up a:%x[i:%d,d:%d] b:%x[i:%d, d:%d]\n", a, a->id, a->depth, b, b->id, b->depth);
	}
	
	while(a!=b && a->P[lvl] == b->P[lvl]) {
		while(lvl > 0 && a->P[lvl] == b->P[lvl]) lvl--;
		a = a->P[lvl];
		b = b->P[lvl];
		
		LOG("down a:%x[i:%d,d:%d] b:%x[i:%d, d:%d]\n", a, a->id, a->depth, b, b->id, b->depth);
	}
	
	if(a != b) {
		LOG("not found?!\n");
	}
	
	return a;
}


int g[MAXN];

long long react(node * p) {
	long long res = 0;
	FOR(i, p->L.size()) {
		res += react(p->L[i]);
	}
	
	FOR(i, p->Q.size()) {
		int a = p->Q[i].first;
		int b = p->Q[i].second;
		
		long long react = min(g[a],g[b]);
		g[a]-=react;
		g[b]-=react;
		res+=2*react;
	}
	
	return res;
}


int main() {
	scanf("%d%d%d",  &n, &m, &k);
	FOR(i,n) scanf("%d", g+i);
	FOR(i, n) {
		tp[i] = &t[i];
		t[i].id = i;
	}
	LOG("I have the grams\n");
	FOR(i, m) {
		int a, b; scanf("%d%d",&a,&b);
		b--; a--;
		node * a1 = tp[a];
		node * b1 = tp[b];
		node * p = &t[n+i];
		p->id = b;
		tp[a] = 0;
		tp[b] = p;
		LOG("work %d+%d\n", a1->id, b1->id);
		attachNode(p, a1);
		attachNode(p, b1);
	}
	LOG("I have the graph\n");
	
	FOR(i,n) if(tp[i]) {
		calcTree(tp[i]);
		
		LOG("%d) Tree: %x [i:%d/d:%d]\n",i,tp[i], tp[i]->id, tp[i]->depth );
	}
	
	LOG("I have the tree calculated\n");
	
	FOR(i, k) {
		int a1,b1; scanf("%d%d", &a1, &b1);
		a1--; b1--;
		node * a = &t[a1];
		node * b = &t[b1];
		
		LOG("(%d,%d) -> %x[i:%d/d:%d/r:%d], %x[i:%d/d:%d/r:%d]\n", a1,b1, a, a->id, a->depth, a->rootId, b, b->id, b->depth, b->rootId);
		if(a->rootId != b->rootId) continue;
		
		if(a->depth < b->depth) b = goUp(b, a->depth);
		if(b->depth < a->depth) a = goUp(a, b->depth);
		
		LOG("after (%d,%d) -> %x[i:%d/d:%d/r:%d], %x[i:%d/d:%d/r:%d]\n", a1,b1, a, a->id, a->depth, a->rootId, b, b->id, b->depth, b->rootId);
		
		node * c = getCommonSubtree(a,b);
		LOG("Found: %x\n", c);
		c->Q.push_back(para(a1,b1));
	}

	LOG("I have the reactions localized\n");
	long long res = 0;
	
	FOR(i, n) if(tp[i]){
		res+=react(tp[i]);
	}
	printf("%lld\n", res);
	
}