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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include<algorithm>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define ZERO 0.000001
#define MAX ((1<<31)-1)
#define qprintf debug && printf
#define sort(a,b) if(a>b)swap(a,b);
#define ull long long int
using namespace std;
//int debug=1;
//#define dcout debug && cout
template<class T> 
class H
{
	public:
	int n;
	int height;
	T**tree;
	int*lengths;
	T (*agregator)(T, T);
	H(int n, T*base, T (*agregator)(T, T)):n(n),agregator(agregator){
		height = calculateHeight(n);
		lengths = calculateLengths(n,height);
		tree = new T*[height];
		tree[0]=base;
		FORI(i,1,height){
			tree[i]=new T[lengths[i]];
			FOR(j,lengths[i]){
				////dcout<<"take ("<<i-1<<","<<2*j<<") and ("<<i-1<<","<<min(2*j+1,lengths[i-1])<<")"<<endl;
				tree[i][j]=agregator(tree[i-1][2*j],tree[i-1][safeRange(i-1,2*j+1)]);
			}
		}
	}
	T getAgregated(int from, int to){
		//if(from!=3 || to!=4)debug=0;else debug=1;
		T mx=agregator(tree[0][from],tree[0][to]);
		int currentLevel=0;
		////dcout<<"level:"<<currentLevel<<",from:"<<from<<",to:"<<to<<",mx:"<<mx<<","<<endl;
		while(to!=from && from+1!=to){
			if((from&1)==0){
				mx = agregator(mx, tree[currentLevel][safeRange(currentLevel,from+1)] );
			}
			from/=2;
			if( to&1 ){
				mx = agregator(mx, tree[currentLevel][to-1] );
			}
			to/=2;
			++currentLevel;
			////dcout<<"level:"<<currentLevel<<",from:"<<from<<",to:"<<to<<",mx:"<<mx<<","<<endl;
		}
		return mx;
	}
	int safeRange(int level, int index){
		return min(index, lengths[level]-1);
	}
	int calculateHeight(int n){
		int i=1,higher;
		//dcout<<"FOR n="<<n;
		while(n>1){
			++i;
			n = (n-1)/2+1;
		}
		//dcout<<" computed height "<<i<<endl;
		return i;
	}
	int*calculateLengths(int n, int height){
		int*r=new int[height];
		r[0]=n;
		FORI(i,1,height){
			r[i]=(r[i-1]-1)/2+1;
		}
		return r;
	}
	~H(){
		FORI(i,1,height)delete [](tree[i]);
		delete []tree;
		delete []lengths;
	}
};
int maxint(int a,int b){
	return max(a,b);
}
class V;
class A
{
	public:
	int depth;
	V*ancestor;
};
A minint(A a,A b){
	return a.depth<b.depth ? a :b;
}
class R
{
	public: int a,b;R(int a,int b):a(a),b(b){}
};
class V
{
	public:
		int volume;
	V():left(NULL),right(NULL),parent(this),reactions(NULL){order=-1;};//{v=0;order=-299;}int v;

	V*left;
	V*right;
	V*parent;
	int depth;
	int order;
	void goDownAndFindThelimit(A*ranges,int*rangeIndex, int depth,V* rightAncestor){//if(v){cout<<"i chuj"<<endl;}else v=1;
		this->depth=depth;
		if(left==NULL){
			//if(*rangeIndex>199000)cout<<*rangeIndex<<" ";
			if(rightAncestor==NULL){
				//cout<<"I am the last one"<<endl;
				ranges[*rangeIndex].depth=-1;
				ranges[*rangeIndex].ancestor=NULL;
			}
			else{
				ranges[*rangeIndex].depth=rightAncestor->depth;
				ranges[*rangeIndex].ancestor=rightAncestor;
			}
			order=*rangeIndex;
			++*rangeIndex;
		}else{
			left->goDownAndFindThelimit(ranges,rangeIndex,depth+1,this);
			right->goDownAndFindThelimit(ranges,rangeIndex,depth+1,rightAncestor);
		}
	}
	list<R>* reactions;
};

ostream& operator<<(ostream& os, const H<A>& r)
{
	FOR(i,r.height){
		FOR(k,(1<<i)-1)os<<" ";
		FOR(j,r.lengths[i]){
			os<<r.tree[i][j].depth;
			FOR(k,(1<<(i+1))-1)os<<" ";
		}
		os<<endl;
	}
	return os;
}
int main(){
	ios_base::sync_with_stdio(0);
	int n,m,k;
	cin>>n>>m>>k;
	V*vials=new V[n];
	V*transfers=new V[m];

	FOR(i,n)cin>>vials[i].volume;
	//utwórz drzewo przelań o(przelań)
	FOR(i,m){
		int from,to;
		cin>>from>>to;
		--from;--to;
		transfers[i].parent = transfers+i;
		transfers[i].left = vials[from].parent;
		transfers[i].right = vials[to].parent;
		vials[to].parent->parent = transfers+i;
		vials[from].parent->parent=transfers+i;
		vials[to].parent = transfers+i;
		vials[from].parent=transfers+i;
	}

	//znajdż rooty fiolek o(n* log* n)
	FOR(i,n){
		V*root=vials+i;
		while(root!=root->parent){
			//if(root==NULL){cout<<"null"<<endl;return 0;}
			root=root->parent;
		}
		//mm[root]=1;
		V*current=vials+i;
		while(current->parent != root){
			//if(current==NULL){cout<<"null"<<endl;return 0;}
			V*tmp=current;
			current->parent=root;
			current=tmp->parent;
		}
	}
	//policz drzewa
	//map<V*,int> mm;
	//FOR(i,n)mm[vials[i].parent]=1;
	//cout<<mm.size()<<" "<<n-m<<endl;return 0;
	
	//przejdź po drzewach (o(n))
	A *ranges=new A[n];
	H<A> *lowestCommonAncestors;
	{
		int rangeIndex=0;
		FOR(i,m){
			if(transfers[i].parent==transfers+i){
				//znajdź wspólnych przodzków, o(n)
				//int before=rangeIndex;
				//cout<<"tree at "<<i<<" "<<rangeIndex<<endl;
				transfers[i].goDownAndFindThelimit(ranges,&rangeIndex,0,NULL);
				//int after = rangeIndex;
				//H<A> tmp(after-before,ranges+before,minint);
				//A minimum = tmp.getAgregated(before,after-2);
				//cout<<tmp<<endl;
				//if(minimum.depth<0){cout<<"NIE at "<<i<<" "<<rangeIndex<<endl;return 0;}
				//cout<<rangeIndex<<" ";
			}
		}
		//rozepnij to na drzewie przedziałowym o (n log n)
		lowestCommonAncestors=new H<A>(n,ranges,minint);
	}//return 0;

	//przejedź po reakcjach (o(n))
	FOR(i,k){
		int a,b;
		cin>>a>>b;
		--a;--b;
		//sprawdź które zaszły 
		if(vials[a].parent==vials[b].parent){
			//if(vials[a].order<0 || vials[b].order<0)cout<<"wrong order"<<endl;
			int aOrder = vials[a].order;
			int bOrder = vials[b].order;
			A res  = lowestCommonAncestors->getAgregated(min(aOrder,bOrder),max(aOrder,bOrder)-1);//to do range index order
			//if(res.depth<0)cout<<"cross tree query!!!"<<aOrder<<" "<<bOrder<<endl;
			V*commonParent = res.ancestor;

			//podepnij je do wspólnego przodka o(1)
			if(commonParent->reactions==NULL)
				commonParent->reactions=new list<R>();
			commonParent->reactions->push_back(R(a,b));
		}
	}

	ull dust=0;
	//przejdź po przelaniach
	FOR(i,m){
		if(transfers[i].reactions==NULL)continue;
		list<R>::iterator it = transfers[i].reactions->begin();
		list<R>::iterator ite = transfers[i].reactions->end();
		//przeprowadzając reakcję o(n)
		for(;it!=ite;++it){
			int newDust = min(vials[it->a].volume,vials[it->b].volume);
			vials[it->a].volume -= newDust;
			vials[it->b].volume -= newDust;
			//dcout<<"Reaguje "<<it->a<<" "<<it->b<<" produkując "<<newDust<<" osadu"<<endl;
			dust+=newDust*2;
		}
	}
	
	cout<<dust<<endl;
	return 0;
}