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
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>

#define SC(n) scanf("%d", &n)
#define SC2(n, m) scanf("%d %d", &n, &m)
#define SCC(c) scanf(" %c", &c)
#define SCS(b) scanf("%s", b)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define FORE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i)
#define PR(n) printf("%d ", (int) (n)) 
#define PRN(n) printf("%d\n", (int) (n)) 
#define elif else if
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define all(v) v.begin(), v.end()
#define itr iterator
#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef vector<pi> vpi;
typedef pair<pii, pi> piii;
typedef vector<vpi> vvpi;
typedef map<int, vpi> mvpi;

const int NMAX = 500*1000 + 7, TMAX = (1<<22)+5;

int n, m, k, a, b, kol;
char is[NMAX];
int g[NMAX], h[NMAX], pos[NMAX];
vi edge[NMAX];
piii rea[NMAX];
int mag;
pi t[TMAX], per[TMAX];
ll out;

void init() {
	mag = 1;
	while(mag < kol)
		mag *= 2;
	
	REP(i, kol)
		t[mag + i] = per[i];
	FORE(i, mag + kol, 2*mag-1)
		t[i] = mp(n + 10, 0);
	FORD(i, mag - 1, 1)
		t[i] = min(t[2*i], t[2*i+1]);
}

pi findMin(int l, int r) {
	prd("findMin %d %d -> ", l, r);
	l += mag;
	r += mag;
	pi ans = mp(n + 10, 0);
	
	while(l < r) {
	    if(l % 2 == 1) {
	      ans = min(ans, t[l]);
	      l = l/2 + 1;
	    }
	    else
	      l /= 2;
	    
	    if(r % 2 == 0) {
	      ans = min(ans, t[r]);
	      r = r/2 - 1;
	    }
	    else
	      r /= 2;
	  }
  	if(l == r)
  		ans = min(ans, t[l]);
  	prd("(%d %d)\n", ans.xx, ans.yy);
	return ans;
}

void dfs(int v, int hh) {
	pos[v] = kol;
	per[kol++] = mp(hh, n + 11);
	
	int k0 = n + 10;
	FORIT(it, edge[v], vi) {
		dfs(*it, hh+1);
		per[kol++] = mp(hh, k0--);
	}
}

int main() {
	SC2(n, m);
	SC(k);
	REP(i, n)
		SC(g[i+1]);
	REP(i, m) {
		SC2(a, b);
		edge[b].pb(a); // kolejnosc krawedzi ok
		is[a] = 1;
	}
	FORE(i, 1, n) if(!is[i])
		edge[0].pb(i);
	REP(i, k) {
		SC2(a, b);
		rea[i] = mp(mp(mp(0, 0), -i), mp(a, b));
	}
	
	dfs(0, 1);
	init();
	
	dbg REP(i, kol)
		prd("%d ", per[i]);
	prd("\n");
	dbg REP(i, n+1)
		prd("%d -> pos %d h %d\n", i, pos[i], h[i]);
	
	REP(i, k) {
		int x1 = rea[i].yy.xx;
		int x2 = rea[i].yy.yy;
		int y1 = pos[x1], y2 = pos[x2];
		rea[i].xx.xx = findMin(min(y1,y2), max(y1,y2));
	}
	sort(rea, rea + k);
	reverse(rea, rea + k);
	
	REP(i, k) {
		if (rea[i].xx.xx.xx == 1)
			break;
		int x1 = rea[i].yy.xx;
		int x2 = rea[i].yy.yy;
		int gg = min(g[x1], g[x2]);
		g[x1] -= gg;
		g[x2] -= gg;
		dbg printf("react %d(%d) %d(%d) -- %d, %d, %d\n", x1, g[x1], x2, g[x2], rea[i].xx.xx.xx, rea[i].xx.xx.yy, rea[i].xx.yy);
		out += gg * 2;
	}
	printf("%lld", out);
	//cout << out;
	return 0;
}