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
#include <cstdio>
#include <set>
#include <map>
#include <cassert>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;

const int MAX_N = 200000+44;
const int MAX_K = 500000+44;

int m, n, k;
long long G[MAX_N];
int forwards[MAX_N];
set<int> backwards[MAX_N];
vector<pair<int, int>> reactions;
vector<pair<int, int>> connections;

vector<pair<int, int>> ordered_reactions[MAX_N];


int visited[MAX_N];
int representants[MAX_N];
int levels[MAX_N];

void dfs(int id, int gid, vector<pair<int, int>>& eulers_path, int level)
{
	visited[id] = gid;
	levels[id] = level;
	if(representants[id]==0)
		representants[id] = eulers_path.size();
	eulers_path.push_back(make_pair(id, level));
	for(int pid : backwards[id])
	{
		dfs(pid,gid,  eulers_path, level+1);
		eulers_path.push_back(make_pair(id, level));
	}
}

long long result = 0;

void dfs_react(int id)
{
	for(int pid : backwards[id])
	{
		dfs_react(pid);
	}
	
	for(auto& p : ordered_reactions[id])
	{
		int amnt = min(G[p.first], G[p.second]);
		
		result += 2*amnt;
		G[p.first] -= amnt;
		G[p.second] -= amnt;
	}
}

int main()
{
	scanf("%d %d %d", &n, &m, &k);
	
	for(int i=1;i<=n;i++) scanf("%lld", &G[i]);
	
	int a,b;
	for(int i=0;i<m;i++)
	{
		scanf("%d %d", &a, &b);
		forwards[a]=b;
		backwards[b].insert(a);
		connections.push_back(make_pair(a, b));
	}
	
	int c,d;
	for(int i=0;i<k;i++)
	{
		scanf("%d %d", &c, &d);
		reactions.push_back(make_pair(c, d));
	}
	
	vector<pair<int, int>> eulers_path;
	map<int, vector<int>> eulers_labels;
	vector<int> final_substances;
	int gid=0;
	for(auto it=connections.rbegin(); it!=connections.rend(); it++)
	{
		if(visited[it->second]) continue;		
		final_substances.push_back(it->second);		
		dfs(it->second, ++gid, eulers_path, 0);
	}
	{
		
		
		//                <lca, prev>
		vector<vector<pair<int, int>>> rmq_sparsed;
		for(int i=0; 1<<i <= eulers_path.size(); i++)
		{
			int len = 1<<i;
			rmq_sparsed.push_back(vector<pair<int, int>>());
			for(int l=0; l+len-1 < eulers_path.size(); l++)
			{
				if(i==0)
					rmq_sparsed[i].push_back(
						make_pair(eulers_path[l].first, 0)
					);
				else if(i==1)
				{
					if(levels[rmq_sparsed[i-1][l].first] < levels[rmq_sparsed[i-1][l+(1<<(i-1))].first])
						rmq_sparsed[i].push_back(make_pair(rmq_sparsed[i-1][l].first, rmq_sparsed[i-1][l+(1<<(i-1))].first));
					else
						rmq_sparsed[i].push_back(make_pair(rmq_sparsed[i-1][l+(1<<(i-1))].first, rmq_sparsed[i-1][l].first));
				}
				else
				{
					if(levels[rmq_sparsed[i-1][l].first] < levels[rmq_sparsed[i-1][l+(1<<(i-1))].first])
						rmq_sparsed[i].push_back(rmq_sparsed[i-1][l]);
					else if(levels[rmq_sparsed[i-1][l].first] == levels[rmq_sparsed[i-1][l+(1<<(i-1))].first])
					{
						if(representants[rmq_sparsed[i-1][l].second] > representants[rmq_sparsed[i-1][l+(1<<(i-1))].second])
							rmq_sparsed[i].push_back(rmq_sparsed[i-1][l]);
						else
							rmq_sparsed[i].push_back(rmq_sparsed[i-1][l+(1<<(i-1))]);
					}
					else
						rmq_sparsed[i].push_back(rmq_sparsed[i-1][l+(1<<(i-1))]);
				}
			}
		}
		
		
		for(auto &p : reactions)
		{
			if(visited[p.first]!=visited[p.second] || visited[p.first]==0)
			{
				continue;
			}
			
			int r1 = representants[p.first];
			int r2 = representants[p.second];
			if(r1>r2) swap(r1, r2);
			
			int i = (int)log2(r2-r1+1);
			
			int j1 = r1;
			int j2 = r2+1 - (1<<i);
			int min1 = rmq_sparsed[i][j1].first;
			int min2 = rmq_sparsed[i][j2].first;
			
			int at; int prev;
			if(
			(levels[min1]==levels[min2] && representants[rmq_sparsed[i][j1].second]>representants[rmq_sparsed[i][j2].second])
			|| levels[min1]<levels[min2])
			{
				at = rmq_sparsed[i][j1].first;
				prev = rmq_sparsed[i][j1].second;
			}
			else
			{
				at = rmq_sparsed[i][j2].first;
				prev = rmq_sparsed[i][j2].second;
			}
			
			//assert(visited[at]==visited[prev]);
			//assert(visited[at]==visited[p.first]);
			ordered_reactions[prev].push_back(p);
		}
	}
	
	for(int start : final_substances)
	{
		dfs_react(start);
	}
	printf("%lld\n", result);
}