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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;


public class fio {
	
	int phialNumber;
	int experimentNumber;
	int reationNumber;
	
	int phialContent[] = new int[200000];
	Experiment experiments[] = new Experiment[200000];
	Reaction reactions[] = new Reaction[500000];
	
	ExperimentNode[] orderedExerimentNodes = new ExperimentNode[200000];
	Node[] treeNodes = new Node[400001];
	int treeNodeIndex = 0;
	PhialNode[] phialNodes = new PhialNode[200000];
	
	RootNode root;
	
	BigInteger result = BigInteger.ZERO;
	
	int LCAHelper[][] = new int[400001][20];
	
	public static void main(String[] args) {
		new fio();
	}
	
	void process() {
		createTree();
		updateUsedPhials();
		updateNodesLevel();
		createLCA();
		assignReactions();
		doPerform();
	}
	
	void createTree() {
		Node[] nodes = new Node[200000];
		
		for (int i = 0; i < experimentNumber; i++) {
			Experiment exp = experiments[i];
			int firstPhial = exp.getPhialOne()-1;
			int secondPhial = exp.getPhialTwo()-1;
			
			Node firstNode = nodes[firstPhial];
			Node secondNode = nodes[secondPhial];
					
			if (firstNode == null) {
				firstNode = new PhialNode(firstPhial);
				addTreeNode(firstNode);
			}
			
			if (secondNode == null) {
				secondNode = new PhialNode(secondPhial);
				addTreeNode(secondNode);
			}
			
			ExperimentNode experimentNode = new ExperimentNode(firstNode, secondNode);
			addTreeNode(experimentNode);
			
			nodes[firstPhial] = null;
			nodes[secondPhial] = experimentNode;
			
			orderedExerimentNodes[i] = experimentNode;
		}
		
		LinkedList<Node> rootChildren = new LinkedList<>();
		for (int i = 0; i < phialNumber; i++) {
			Node node = nodes[i];
			if (node != null) {
				rootChildren.add(node);
			}
		}
		
		root = new RootNode(rootChildren);
		addTreeNode(root);
	}

	void addTreeNode(Node node) { 
		treeNodes[treeNodeIndex] = node;
		node.setIndex(treeNodeIndex);
		treeNodeIndex++;
	}

	void updateUsedPhials() {
		Arrays.fill(phialNodes, null);
		for (int i = 0; i < treeNodeIndex; i++) {
			Node node = treeNodes[i];
			if (node instanceof PhialNode) {
				PhialNode phialNode = (PhialNode)node;
				
				int phialIndex = phialNode.getPhialIndex();
				phialNodes[phialIndex] = phialNode;
			}
		}
	}
	
	void updateNodesLevel() {
		Stack<Node> stack = new Stack<>();
		stack.push(root);
		
		while (!stack.isEmpty()) {
			Node node = (Node) stack.pop();
			
			LinkedList<Node> children = node.getChildren();
			if (children != null) {
				for (Node child : children) {
					stack.push(child);
				}
			}
			
			Node parent = node.getParent();
			if (parent != null) {
				node.setLevel(parent.getLevel()+1);
			}
		}
	}
	
	void createLCA() {
		for (int i = 0; i < treeNodeIndex; i++) {
			for (int j = 0; 1 << j < treeNodeIndex; j++) {
				LCAHelper[i][j] = -1;
			}
		}

		for (int i = 0; i < treeNodeIndex; i++) {
			Node node = treeNodes[i];
			Node parent = node.getParent();

			LCAHelper[i][0] = i;
			if (parent != null) {
				LCAHelper[i][0] = parent.getIndex();
			} else {
				LCAHelper[i][0] = -1;
			}
		}

		for (int j = 1; 1 << j < treeNodeIndex; j++) {
			for (int i = 0; i < treeNodeIndex; i++) {
				if (LCAHelper[i][j - 1] != -1)
					LCAHelper[i][j] = LCAHelper[LCAHelper[i][j - 1]][j - 1];
			}
		}
	}

	Node lca(int phialOne, int phialTwo) {
		PhialNode phialNodeOne = phialNodes[phialOne];
		PhialNode phialNodeTwo = phialNodes[phialTwo];
	
		if (phialNodeOne == null || phialNodeTwo == null) {
			return null;
		}
		
		int p = phialNodeOne.getIndex();
		int q = phialNodeTwo.getIndex();
		int lp = phialNodeOne.getLevel(); 
		int lq = phialNodeTwo.getLevel();
		
		//if p is situated on a higher level than q then we swap them
		  if (lp < lq) {
		      int tmp = p; p = q; q = tmp;
		      tmp = lp; lp = lq; lq = tmp;
		  }
				  
		// we compute the value of [log(L[p)]
		int log = 1;
		for (; 1 << log <= lp; log++)
			;
		log--;
				   
		// we find the ancestor of node p situated on the same level
		// with q using the values in P
		for (int i = log; i >= 0; i--) {
			if (lp - (1 << i) >= lq) {
				p = LCAHelper[p][i];
			}
		}

		if (p == q) {
			return treeNodes[p];
		}
		// we compute LCA(p, q) using the values in P
		for (int i = log; i >= 0; i--) {
			if (LCAHelper[p][i] != -1 && LCAHelper[p][i] != LCAHelper[q][i]) {
				p = LCAHelper[p][i];
				q = LCAHelper[q][i];
			}
		}

		return treeNodes[p].getParent();
	}

	void assignReactions() {
		for (int i = 0; i < reationNumber; i++) {
			Reaction reaction = reactions[i];
			Node node = lca(reaction.getPhialOne()-1, reaction.getPhialTwo()-1);
			
			if (node instanceof ExperimentNode) {
				ExperimentNode experimentNode = (ExperimentNode)node;
				experimentNode.addReaction(reaction);
			}
		}
	}
	
	
	
	void doPerform() {
		for (int i = 0; i < experimentNumber; i++) {
			ExperimentNode experimentNode = orderedExerimentNodes[i];
			LinkedList<Reaction> reactionsToPerform = experimentNode.getReactionsToPerform();
			for (Reaction reaction : reactionsToPerform) {
				int phialOne = reaction.getPhialOne()-1;
				int phialTwo = reaction.getPhialTwo()-1;
				
				int phialOneContent = phialContent[phialOne];
				int phialTwoContent = phialContent[phialTwo];
				
				if (phialOneContent <= 0 || phialTwoContent <= 0) {
					continue;
				}
				
				int rest = Math.abs(phialOneContent - phialTwoContent);
				int common = Math.max(phialOneContent - rest, phialTwoContent - rest);
				
				phialContent[phialOne] -= common;
				phialContent[phialTwo] -= common;
				
				int sediment = common*2;
				
				result = result.add(new BigInteger(""+sediment));
			}
		}
	}
	
	public fio() {
		read();
		process();
		print();
	}
	
	void read() {
		InputStream is = System.in;
//		if (Boolean.getBoolean("test")) {
//			is = this.getClass().getResourceAsStream("fio.in");
//		}
		
		BufferedInputStream bis = new BufferedInputStream(is);

		phialNumber = readInt(bis);
		experimentNumber = readInt(bis);
		reationNumber = readInt(bis);
		
		for (int i = 0; i < phialNumber; i++) {
			phialContent[i] = readInt(bis);
		}
		
		for (int i = 0; i < experimentNumber; i++) {
			int x1 = readInt(bis);
			int x2 = readInt(bis);
			Experiment exp = new Experiment(i, x1, x2);
			experiments[i] = exp;
		}
		
		for (int i = 0; i < reationNumber; i++) {
			int x1 = readInt(bis);
			int x2 = readInt(bis);
			Reaction reaction = new Reaction(i, x1, x2);
			reactions[i] = reaction;
		}
	}
	
	void print() {
		System.out.println(result);
	}
	
	abstract class Node {
		Node parent;

		LinkedList<Node> children;

		int level;
		
		int index;
		
		public LinkedList<Node> getChildren() {
			return children;
		}

		public Node getParent() {
			return parent;
		}

		public void setParent(Node parent) {
			this.parent = parent;
		}

		public void setChildren(LinkedList<Node> children) {
			this.children = children;
		}

		public int getLevel() {
			return level;
		}

		public void setLevel(int level) {
			this.level = level;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}
		
		
	}

	class RootNode extends Node {
		public RootNode(LinkedList<Node> children) {
			setChildren(children);
			for (Node node : children) {
				node.setParent(this);
			}
			setLevel(0);
		}
	}

	class ExperimentNode extends Node {
		//private final Experiment experiment;
		
		private LinkedList<Reaction> reactionsToPerform = new LinkedList<>();

		public ExperimentNode(Node firstNode,
				Node secondNode) {
			super();
			//this.experiment = experiment;
		
			firstNode.setParent(this);
			secondNode.setParent(this);
			
			LinkedList<Node> children = new LinkedList<>();
			children.add(firstNode);
			children.add(secondNode);
			
			setChildren(children);
		}
		
		void addReaction(Reaction reaction) {
			reactionsToPerform.add(reaction);
		}

		public LinkedList<Reaction> getReactionsToPerform() {
			return reactionsToPerform;
		}

		@Override
		public String toString() {
			return "ExperimentNode [parent=" + parent!=null?""+parent.getIndex():null + ", level=" + level
					+ ", index=" + index + "]";
		}
	}

	
	class PhialNode extends Node {
		LinkedList<Reaction> reactionsToPerform;
		
		int phialIndex;

		public PhialNode(int phialIndex) {
			super();
			this.phialIndex = phialIndex;
		}

		public int getPhialIndex() {
			return phialIndex;
		}

		@Override
		public String toString() {
			return "PhialNode [phialIndex=" + phialIndex + ", parent=" + parent!=null?""+parent.getIndex():"null"
					+ ", level=" + level + ", index=" + index + "]";
		}
	}
	
	
	class PhialSet {
		private final int index;
		private final int phialOne;
		private final int phialTwo;
		
		public PhialSet(int index, int phialOne, int phialTwo) {
			super();
			this.index = index;
			this.phialOne = phialOne;
			this.phialTwo = phialTwo;
		}

		public int getIndex() {
			return index;
		}

		public int getPhialOne() {
			return phialOne;
		}

		public int getPhialTwo() {
			return phialTwo;
		}

		@Override
		public String toString() {
			return "PhialSet [index=" + index + ", phialOne=" + phialOne
					+ ", phialTwo=" + phialTwo + "]";
		}
		
	}
	
	class Experiment extends PhialSet {
		public Experiment(int index, int phialOne, int phialTwo) {
			super(index, phialOne, phialTwo);
		}
	}
	
	class Reaction extends PhialSet {
		public Reaction(int index, int phialOne, int phialTwo) {
			super(index, phialOne, phialTwo);
		}
	}
	
	private int readInt(InputStream in) {
		int ret = 0;
		boolean dig = false;
		
		try {
			for (int c = 0; (c = in.read()) != -1;) {
				if (c >= '0' && c <= '9') {
					dig = true;
					ret = ret * 10 + c - '0';
				} else if (dig) {
					break;
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		return ret;
	}
}