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
/*
 *  Copyright (C) 2014  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <vector>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;


//#define DEBUG 666
#ifdef DEBUG
	#define DBG(x) x
#else
	#define DBG(x)
#endif


class Reaction {
public:
	int priority;
	int agent1;
	int agent2;

	Reaction(int p, int a1, int a2): priority(p), agent1(a1), agent2(a2) {}

	bool operator<(const Reaction& other) const {
		return priority < other.priority;
	}
};


int main() {
	int n, m, k;
	int weight, from, to, agent1, agent2;

	vector<int> weights;
	vector<pair<int, int>> steps;
	vector<unordered_map<int, int>> reactions;
	vector<unordered_set<int>> products;

	ios::sync_with_stdio (false);
	cin >> n >> m >> k;

	weights.reserve(n);
	steps.reserve(m);
	reactions.resize(n);
	products.resize(n);

	for (int i = 0; i < n; ++i) {
		cin >> weight;
		weights.push_back(weight);
	}
	for (int i = 0; i < m; ++i) {
		cin >> from >> to;
		steps.push_back(make_pair(--from, --to));
	}
	for (int i = 0; i < k; ++i) {
		cin >> agent1 >> agent2;
		--agent1; --agent2;
		reactions[min(agent1, agent2)][max(agent1, agent2)] = i;
		products[agent1].insert(agent1);
		products[agent2].insert(agent2);
	}

	unsigned long long int precipitate = 0;
	int priority;
	int agent;

	for (int i = 0; i < m; ++i) {
		from = steps[i].first;
		to = steps[i].second;

DBG(
cout << "s " << i << ": " << from << " -> " << to << endl;
)

		// find posisble reactions and their priorities
		deque<Reaction> reactions_order;
		for (auto agent1 : products[from]) {
			for (auto agent_map: reactions[agent1]) {
				if (products[to].count(agent_map.first) == 0) {
					continue;
				}
				auto agent2 = agent_map.first;
DBG(
cout << "a = " << agent1 << "+" << agent2 << endl;
);
				priority = reactions[agent1][agent2];
				reactions_order.push_back(Reaction(priority, agent1, agent2));
			}
		}
		for (auto agent1 : products[to]) {
			for (auto agent_map: reactions[agent1]) {
				if (products[from].count(agent_map.first) == 0) {
					continue;
				}
				auto agent2 = agent_map.first;
DBG(
cout << "a = " << agent1 << "+" << agent2 << endl;
);
				priority = reactions[agent1][agent2];
				reactions_order.push_back(Reaction(priority, agent1, agent2));
			}
		}

		// sort reactions by priority
		sort(begin(reactions_order), end(reactions_order));

		// apply reactions
		for (auto r : reactions_order) {
DBG(
cout << "r = " << r.agent1 << "+" << r.agent2 << endl;
);

			weight = min(weights[r.agent1], weights[r.agent2]);
			if (weight == 0) {
				continue;
			}

			precipitate += 2 * weight;
			weights[r.agent1] -= weight;
			weights[r.agent2] -= weight;
		}

		// move reactions residues from the source phial
		auto iter = begin(products[from]);
		while (iter != end(products[from])) {
			agent = *iter++;
			if (weights[agent] > 0) {
				products[to].insert(agent);
			}
			products[from].erase(agent);
		}
		// remove used agents from the target phial
		iter = begin(products[to]);
		while (iter != end(products[to])) {
			agent = *iter++;
			if (weights[agent] == 0) {
				products[to].erase(agent);
			}
		}

DBG(
for (int j = 0; j < n; ++j) {
	cout << "p[" << j << "] =";
	for (auto agent : products[j]) {
		cout << " " << agent;
	}
	cout << ", ";
}
cout << endl;

cout << "weights = ";
for (int j = 0; j < n; ++j) {
	cout << weights[j] << " ";
}
cout << endl;
);
	}

	cout << precipitate << endl;
	return 0;
}