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
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <queue>
#include <list>
#include <string>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PI;
#define MP make_pair
#define PB push_back
#define SD second
#define FT first

int k,n,m,q;

const int N = 100003;
const int INF = 20000000;

int masa[N];
PI steps[N];
vector<PI> hate;
vector<PI> sas[N];

VI zawartosc[N];

LL przelej(int a, int b) {
	//O(E(a)*b+E(b)*a)
	unordered_set<int> bb(zawartosc[b].begin(),zawartosc[b].end());
	
	vector<pair<int,PI> > wytracaj;
	for(int i=0;i<zawartosc[a].size();i++) {
		int sub = zawartosc[a][i];
		for(int i2=0;i2<sas[sub].size();i2++) {
			PI hater = sas[sub][i2];
			if(bb.count(hater.SD)) {
				wytracaj.PB(MP(hater.FT,MP(sub,hater.SD)));
			}
		}
	}
	sort(wytracaj.begin(),wytracaj.end());
	LL res = 0;
	for(int i=0;i<wytracaj.size();i++) {
		PI miedzy = wytracaj[i].SD;
		LL ile = min(masa[miedzy.FT],masa[miedzy.SD]);
		res +=ile * 2;
		masa[miedzy.FT]-=ile;
		masa[miedzy.SD]-=ile;
	}
	
	for(int i=0;i<zawartosc[a].size();i++) {
		int sub = zawartosc[a][i];
		if(masa[sub] > 0) {
			zawartosc[b].PB(sub);
		}
	}
	return res;
}

int main() {
	scanf("%d%d%d",&n,&m,&k);
	
	for(int i=0;i<n;i++) {
		scanf("%d",&masa[i]);
		zawartosc[i].PB(i);
	}
	
	for(int i=0;i<m;i++) {
		int a,b;
		scanf("%d%d",&a,&b);a--;b--;
		steps[i] = MP(a,b);
	}
	
	for(int i=0;i<k;i++) {
		int a,b;
		scanf("%d%d",&a,&b);a--;b--;
		sas[a].PB(MP(i,b));
		sas[b].PB(MP(i,a));
		hate.PB(MP(a,b));
	}
	
	LL res = 0;
	for(int i=0;i<m;i++) {
		res += przelej(steps[i].FT,steps[i].SD);
		//printf("%lld\n",res);
	}
	
	printf("%lld\n",res);
		
	return 0;
}