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
#include <cstdio>
#include <utility>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

#if (defined WIN32 || defined WINDOWS || defined _WIN32 || defined _WINDOWS)
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif

typedef long long LL;

int read_int()
{
    register char c = 0;
	int result = 0;
    while (c < 33) c=getchar_unlocked();
    while (c>32) {result=result*10 + (c-'0'); c=getchar_unlocked();}
	return result;
}

void write_int(int x)
{
	if (x >= 10) write_int(x / 10);
	putchar_unlocked('0' + (x % 10));
}

void write_newline()
{
	putchar_unlocked('\n');
}

void write_space()
{
	putchar_unlocked(' ');
}

template<class T> T abs(T a, T b)
{
	return a < b ? b-a : a-b;
}

int main() {
	int n = read_int();
	int m = read_int();
	int k = read_int();
	
	vector<int> weights;
	vector<set<int> > vials(n);
	weights.reserve(n);
	for(size_t i = 0; i < n; ++i)
	{
		int g = read_int();
		weights.push_back(g);
		set<int>& z = vials[i];
		z.insert(i);
	}
	
	vector<pair<int, int> > actions;
	actions.reserve(m);
	for(size_t i = 0; i < m; ++i)
	{
		int a = read_int() - 1;
		int b = read_int() - 1;
		actions.push_back(make_pair(a,b));
	}
	
	vector<map<int, int> > reactionsMap(n);
	vector<pair<int, int> > reactions;
	reactions.reserve(k);
	for(size_t i = 0; i < k; ++i)
	{
		int c = read_int() - 1;
		int d = read_int() - 1;
		int x = min(c,d);
		int y = max(c,d);
		reactions.push_back(make_pair(x, y));
		map<int, int>& z = reactionsMap[x];
		z.insert(make_pair(y, i));
	}
	
	LL sediment = 0;
	for(vector<pair<int, int> >::iterator itAction = actions.begin(); itAction != actions.end(); ++itAction)
	{
		// 1. join vials
		int from = itAction->first;
		int to = itAction->second;
		set<int>& srcVial = vials[from];
		set<int>& vial = vials[to];
		vial.insert(srcVial.begin(), srcVial.end());
		// srcVial.clear();
		
		// 2. get reaction list
		set<int> possibleReactions;
		for(set<int>::iterator it = vial.begin(); it != vial.end(); ++it)
		{
			// get possible reactions;
			map<int, int>& z = reactionsMap[*it];
			
			// get inner join
			map<int, int>::iterator it2_1 = z.begin();
			set<int>::iterator it2_2 = vial.begin();
			while (true)
			{
				if (it2_1 == z.end()) break;
				if (it2_2 == vial.end()) break;
				if (it2_1->first < *it2_2)
				{
					it2_1 = z.lower_bound(*it2_2);
				} 
				else if (it2_1->first > *it2_2)
				{
					++it2_2 = vial.lower_bound(it2_1->first);
				}
				else
				{
					possibleReactions.insert(it2_1->second);
					++it2_1;
					++it2_2;
				}
			}
		}
		
		// 3. do each reaction
		for(set<int>::iterator it = possibleReactions.begin(); it != possibleReactions.end(); ++it)
		{
			pair<int, int>& reaction = reactions[*it];
			int val1 = weights[reaction.first];
			int val2 = weights[reaction.second];
			if (val1 == val2)
			{
				vial.erase(reaction.first);
				vial.erase(reaction.second);
				weights[reaction.first] = 0;
				weights[reaction.second] = 0;
				sediment += (LL)val1;
				sediment += (LL)val1;
			}
			else if (val1 < val2)
			{
				vial.erase(reaction.first);
				weights[reaction.second] = val2 - val1;
				weights[reaction.first] = 0;
				sediment += (LL)val1;
				sediment += (LL)val1;
			}
			else
			{
				vial.erase(reaction.second);
				weights[reaction.first] = val1 - val2;
				weights[reaction.second] = 0;
				sediment += (LL)val2;
				sediment += (LL)val2;
			}
		}
	}

	printf("%lld\n", sediment);
	return 0;
}