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
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <time.h>

struct ProbeData
{
	ProbeData(long id, long vol)
		: id(id), vol(vol), isCounted(true), next(-1), nextMerge(-1){}

	long vol;
	bool isCounted;
	long id;
	long next;
	long nextMerge;
	
	std::vector<long> ownMerges;
};

struct Relation
{
	Relation( long p1, long p2, long commonMerge)
		: p1(p1), p2(p2), commonMerge(commonMerge){}

	long p1;
	long p2;

	long commonMerge;
};

long FindCommonMerge(long v1, long v2, std::vector<ProbeData>& probes)
{
	auto probe1 = probes[v1 - 1];
	auto probe2 = probes[v2 - 1];
	long newProbe1, newProbe2;
	bool shouldReturn = false;
	long res;

	while(true)
	{
		if(probe1.ownMerges.empty() || probe2.ownMerges.empty())
		{
			newProbe1 = probe1.id;
			newProbe2 = probe2.id;

			bool check = false;
			if(probe1.ownMerges.empty())
			{
				newProbe1 = probe1.next;
				check = true;
			}

			if(probe2.ownMerges.empty())
			{
				newProbe2 = probe2.next;
				check = true;
			}

			if(check)
			{
				if(newProbe1 == newProbe2)
				{
					res = -1;
					if(newProbe1 != probe1.id)
						res = probe1.nextMerge;

					if(newProbe2 != probe2.id)
						res = std::max( res, probe2.nextMerge );
					
					return res;
				}

				probe1 = probes[newProbe1 - 1];
				probe2 = probes[newProbe2 - 1];
			}

			continue;
		}

		for(auto& it : probe1.ownMerges)
		{
			if(std::binary_search(probe2.ownMerges.begin(), probe2.ownMerges.end(), it))
			{
				return it;
			}
		}
		
		if(probe1.ownMerges.back() > probe2.ownMerges.back())
		{
			if(probes[probe2.next - 1].id == probe1.id)
				return probe2.next;

			probe2 = probes[probe2.next - 1];
		}
		else
		{
			if(probes[probe1.next - 1].id == probe2.id)
				return probe1.next;

			probe1 = probes[probe1.next - 1];
		}
	}
}

int main()
{
	long n, m, k;
	scanf("%ld %ld %ld", &n, &m, &k);

	std::vector<ProbeData> probes;
	probes.reserve(n);
	
	long v1, v2;
	for(long i = 0; i < n; ++i)
	{
		scanf("%ld", &v1);
		probes.push_back(ProbeData(i+1, v1));
	}

	long mergeNum = 0;
	for(long i = 0; i < m; ++i)
	{
		scanf("%ld %ld", &v1, &v2);
		
		probes[v1 - 1].next = v2;
		probes[v1 - 1].nextMerge = mergeNum;
		probes[v2 - 1].ownMerges.push_back(mergeNum);

		++mergeNum;
	}

	std::vector<Relation> relations;
	relations.reserve(k);

	for(long i = 0; i < k; ++i)
	{
		scanf("%ld %ld", &v1, &v2);
		

		relations.push_back(Relation(v1, v2, FindCommonMerge(v1, v2, probes)));
	}

	std::stable_sort(relations.begin(), relations.end(), [](const Relation& r1, const Relation& r2){ return r1.commonMerge < r2.commonMerge; });

	long long totGramms = 0;
	long minTmp;

	for(auto& elem : relations)
	{
		auto& probe1 = probes[elem.p1 - 1];
		auto& probe2 = probes[elem.p2 - 1];

		if(probe1.isCounted && probe2.isCounted)
		{
			minTmp = std::min(probe1.vol, probe2.vol);
			totGramms += minTmp;
			probe1.vol -= minTmp;
			probe2.vol -= minTmp;
			if(probe1.vol == 0)
				probe1.isCounted = false;

			if(probe2.vol == 0)
				probe2.isCounted = false;
		}
	}

	totGramms *= 2;

	printf("%lld\n", totGramms);
}