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
// Grzegorz Bukowiec
// PA 2014 - Fiolki

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int min(int a, int b)
{
	if (a < b) {
		return a;
	} else {
		return b;
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	int n, m, k, a, b, check1, check2;
	cin >> n >> m >> k;
	int *quantity = new int[n];
	pair<int, int> *comingTo = new pair<int, int>[n];
	for (int i = 0; i < n; ++i) {
		cin >> quantity[i];
		comingTo[i] = make_pair(-1, -1);
	}
	queue<pair<int, int>> *reactions = new queue<pair<int, int>>[m];
	for (int i = 0; i < m; ++i) {
		cin >> a;
		--a;
		cin >> b;
		--b;
		comingTo[a] = make_pair(b, i);
	}

	bool *signing = new bool[m];
	queue<int> toClean;
	for (int i = 0; i < k; ++i) {
		cin >> a;
		--a;
		cin >> b;
		--b;

		while (!toClean.empty()) {
			signing[toClean.front()] = false;
			toClean.pop();
		}

		int foundCommon = -1;
		check1 = a;
		check2 = b;

		while (comingTo[check1].first != -1 || comingTo[check2].first != -1) {
			if (comingTo[check1].first != -1) {
				if (signing[comingTo[check1].second]) {
					foundCommon = comingTo[check1].second;
					break;
				}
				signing[comingTo[check1].second] = true;
				toClean.push(comingTo[check1].second);
				check1 = comingTo[check1].first;
			}
			if (comingTo[check2].first != -1) {
				if (signing[comingTo[check2].second]) {
					foundCommon = comingTo[check2].second;
					break;
				}
				signing[comingTo[check2].second] = true;
				toClean.push(comingTo[check2].second);
				check2 = comingTo[check2].first;
			}
		}

		if (foundCommon != -1) {
			reactions[foundCommon].push(make_pair(a, b));
		}
	}

	long long int result = 0;

	for (int i = 0; i < m; ++i) {
		while (!reactions[i].empty()) {
			int reduce = min(quantity[reactions[i].front().first], quantity[reactions[i].front().second]);
			quantity[reactions[i].front().first] -= reduce;
			quantity[reactions[i].front().second] -= reduce;
			result = result + reduce + reduce;
			reactions[i].pop();
		}
	}

	cout << result;

	return 0;
}