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
#include<cstdio>
#include<vector>
#include<cassert>
#include<algorithm>
#define MAXF (200005)
#define MAXR (500005)
using namespace std;

class FindAndUnion{
	private:
		int *rank, *parent;
		int _size;
	public:
		/* Create new familly with `size` sets */
		FindAndUnion(int size);
		~FindAndUnion();

		/* Get reprezentant of set `v` */
		int find(int v);

		/* Join sets `u` and `v` */
		void join(int u, int v);

		/* Test if `u` and `v` belong to the same set */
		bool the_same(int u, int v);

		int size();
};

FindAndUnion::FindAndUnion(int size):_size(size){
	this->rank = new int[size];
	this->parent = new int[size];
	for(int i=0;i<size;i++){
		this->rank[i] = 0;
		this->parent[i] = i;
	}
}

FindAndUnion::~FindAndUnion(){
	delete[] this->rank;
	delete[] this->parent;
}

int FindAndUnion::find(int u){
	int v = u;
	while(this->parent[v] != v)
		v = this->parent[v];

	while(this->parent[u] != u){
		int t = this->parent[u];
		this->parent[u] = v;
		u = t;
	}
	return v;
}

void FindAndUnion::join(int u, int v){
	u = this->find(u);
	v = this->find(v);

	if(this->rank[u] > this->rank[v])
		this->parent[v] = u;
	else{
		this->parent[u] = v;
		if(this->rank[u] == this->rank[v])
			this->rank[v]++;
	}
}

bool FindAndUnion::the_same(int u, int v){
	return this->find(u) == this->find(v);
}

int FindAndUnion::size(){
	return this->_size;
}

//-----------------------------------------

struct Tree{
    int left, right;
    int parent;
    int ancestor;
    int color;
    int id;
};

int G[MAXF];
Tree T[2*MAXF];
int R[MAXF];//reprezentant aktualnego stanu fiolki
vector<int> P[MAXF];//P[i] - zbiór `j` reagujących z i
int Reacts[MAXR][2];//reakcje

vector<int> S[MAXF];//Zbiór reakcji ktore nalezy przeprowadzic w n+i


int n,m,k,rC,tC;

#define WHITE (0)
#define BLACK (1)


//O(nlg*n)
void TarjanOLCA(int v, FindAndUnion *fau){
    T[v].ancestor = v;
    
    if(T[v].left!=-1){
        TarjanOLCA(T[v].left, fau);
        fau->join(v, T[v].left);
        T[fau->find(v)].ancestor = v;
    }
    
    if(T[v].right!=-1){
        TarjanOLCA(T[v].right, fau);
        fau->join(v, T[v].right);
        T[fau->find(v)].ancestor = v;
    }
    
    T[v].color = BLACK;
    
    if(v<n){
        for(int i=0;i<P[v].size();i++){
            int u = Reacts[P[v][i]][0]+Reacts[P[v][i]][1]-v;
            if(T[u].color == BLACK)
                S[T[fau->find(u)].ancestor-n].push_back(P[v][i]);
        }
    }
}

int main(){
    
    scanf("%d%d%d",&n,&m,&k);
    tC = n;
    for(int i=0;i<n;i++){
        scanf("%d",&G[i]);
        T[i].left = T[i].right = T[i].parent = -1;
        R[i] = i;
    }
    
    
    for(int i=0;i<m;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        T[tC].left = R[a-1]; T[tC].right = R[b-1];
        T[R[a-1]].parent = T[R[b-1]].parent = tC;
        T[tC].parent = -1;
        R[a-1] = R[b-1] = tC++;
    }
    
    int nid = 0;
    
    for(int i=tC-1;i>=0;i--){
        if(T[i].parent == -1)
            T[i].id = nid++;
        else T[i].id = T[T[i].parent].id;
    }
    
    FindAndUnion fau(tC);
    for(int i=0;i<tC;i++){
        T[i].color = WHITE;
        T[i].ancestor = -1;
    }
    
    rC = 0;
    for(int i=0;i<k;i++){
        int a,b;
        scanf("%d%d",&a,&b);a--;b--;
        if(T[a].id == T[b].id){//kiedys zostanie przeprowadzona reakcja w ktorej przereaguja
            Reacts[rC][0] = a;
            Reacts[rC][1] = b;
            P[a].push_back(rC);
            P[b].push_back(rC);
            rC++;
        }
    }
    
    for(int i=0;i<tC;i++)
        if(T[i].parent == -1)
            TarjanOLCA(i, &fau);
    
    long long osad = 0;
    for(int i=n;i<tC;i++)if(S[i-n].size()){
        sort(S[i-n].begin(),S[i-n].end());
        
        for(int j=0;j<S[i-n].size();j++){
            int u = Reacts[S[i-n][j]][0], v = Reacts[S[i-n][j]][1];

            int o = min(G[u], G[v]);
            G[u]-=o;G[v]-=o;
            osad+=(long long)o;
        }    
    }
    
    printf("%lld\n",2*osad);
    return 0;
}