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
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

bool debugFlag = false;

int main() {
	std::ios_base::sync_with_stdio (false);

	int n, m, k;

	cin >> n;
	cin >> m;
	cin >> k;

	vector<long> substancje;
	map<int, multimap<int, int> > fiolki;
	map<int, map<int, int> > react;
	map<int, map<int, bool> > zaw;
	vector<int> from;
	vector<int> to;
	long ile;
	int f, t;
	int a, b;

	long long osad = 0;
	if (debugFlag) cout << "what?" << endl;
	for (int i = 0; i < n; ++i) {
		cin >> ile;
		substancje.push_back(ile);
		zaw[i][i] = true;
		if (debugFlag) cout << "initial " << substancje[i] << endl;
	}
	if (debugFlag) cout << "got here" << endl;
	for (int i = 0; i < m; ++i) {
		cin >> f;
		cin >> t;
		from.push_back(f-1);
		to.push_back(t-1);
	}

	if (debugFlag) cout << "got there" << endl;
	for (int i = 0; i < k; ++i) {
		cin >> a;
		cin >> b;
		a -= 1;
		b -= 1;
		react[a][b] = i;
		react[b][a] = i;
		fiolki[a].insert(pair<int,int>(b, a));
		fiolki[b].insert(pair<int,int>(a, b));
		if (debugFlag) cout << "reaction " << a << " " << b << endl;
/*
		if ((subst[a] == subst[b]) ) { // && (fiolki[subst[a]][a] == true && fiolki[subst[b]][b] == true)) {
			if(substancje[a] < substancje[b])
				ile = substancje[a];
			else
				ile = substancje[b];
			substancje[a] -= ile;
			substancje[b] -= ile;
			osad += ile;
			osad += ile;
		}
*/
	}
	if (debugFlag) cout << "and finally" << endl;
	for (int i = 0; i < m; ++i) {
		f = from[i];
		t = to[i];
		if (debugFlag) cout << "i " << i << "/" << m << " zlewam: " << f << " " << t << endl;
		map<int, pair<int, int> > reactions;
		for (map<int, bool>::iterator itf = zaw[f].begin();  itf != zaw[f].end(); ++itf) {
			if (debugFlag) cout << " analyzing " << itf->first << endl;
			if (fiolki[t].find(itf->first) != fiolki[t].end()) {
				for(multimap<int, int>::iterator r = fiolki[t].lower_bound(itf->first);
					r != fiolki[t].upper_bound(itf->first); ++r) {
					if (debugFlag) cout << " reacting " << r->first << " " << r->second << endl;
					fiolki[f].erase(r->first);
					reactions[react[r->first][r->second]]=pair<int, int>(r->first, r->second);
				}
				fiolki[t].erase(itf->first);
			}
			zaw[t][itf->first] = true;
		}
		for (multimap<int, int>::iterator x = fiolki[f].begin(); x != fiolki[f].end(); ++x) {
			fiolki[t].insert(pair<int, int>(x->first, x->second));
		}
		for (map<int, pair<int, int> >::iterator rea = reactions.begin(); rea != reactions.end(); ++rea) {
			a = rea->second.first;
			b = rea->second.second;
			if (substancje[a] <= 0 || substancje[b] <= 0)
				continue;
			if(substancje[a] < substancje[b])
				ile = substancje[a];
			else
				ile = substancje[b];
			substancje[a] -= ile;
			substancje[b] -= ile;
			osad += ile;
			osad += ile;
			if(substancje[a] <= 0)
				zaw[t].erase(a);
			if(substancje[b] <= 0)
				zaw[t].erase(b);
		}
	}

	cout << osad << endl;
}