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
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>
#define VVII    VC<VII>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

VII fu;

void fuInit(int n){
    fu = VII(n,MP(-1,0));
}

int _fuFind(int v){
    return ((fu[v].X == -1)?v:(fu[v].X=_fuFind(fu[v].X))); 
}

inline int fuFind(int v){
    return ((fu[v].X == -1)?v:(fu[v].X=_fuFind(fu[v].X))); 
}

inline void fuUnion(int r1, int r2){
    if (fu[r1].Y > fu[r2].Y)
        fu[r2].X = r1;
    else if (fu[r1].Y < fu[r2].Y)
        fu[r1].X = r2;
    else{
        fu[r1].X = r2;
        fu[r2].Y++;
    }   
}

int n, l, r;
VI amount;
VII seq;
VII react;

void readData(){
    scanf("%d%d%d",&n,&l,&r);
    amount.resize(n);
    REP(i,n) scanf("%d",&(amount[i]));
    seq.resize(l);
    REP(i,l) {
        scanf("%d%d",&(seq[i].X),&(seq[i].Y));
        seq[i].X--; seq[i].Y--;
    }
    react.resize(r);
    REP(i,r){
        scanf("%d%d",&(react[i].X),&(react[i].Y));
        react[i].X--;
        react[i].Y--;
    }
}

VII T;
int forestSize;
int root;

void makeForest(){
    fuInit(n);
    VI cur(n); REP(i,n) cur[i] = i;
    T.resize(n,MP(-1,-1));
    T.reserve(2*n-1);
    for(const auto &op : seq){
        fuUnion(fuFind(op.X),fuFind(op.Y));
        T.PB(MP(cur[op.X],cur[op.Y]));
        cur[op.X] = -1;
        cur[op.Y] = SIZE(T)-1;
    } 
    //for(int i=n;i<SIZE(T);i++)
    //    cerr << i << " " << T[i].X << " " << T[i].Y << endl;
    
    VI roots;
    REP(i,n) if (cur[i] != -1) roots.PB(cur[i]);
    //cerr << n << " " << SIZE(roots) << endl; 

    forestSize = SIZE(T);
    if (SIZE(roots) == 1)
        root = roots[0];
    else{
        int prev = roots[0];
        for(int i=1; i < SIZE(roots); i++){
            T.PB(MP(prev,roots[i]));
            prev = SIZE(T)-1;
        }
        root = prev;
    }
}

VVI lca; // indices shifted left by n, since no reactions at leaves
VI ancestor;
VI visited;
VVII lcaPairs;

VI stack;

void doLCA(){
    while(!stack.empty()){
        int u = stack.back();
        ancestor[u] = u;
        if (T[u].X != -1){
            if (!visited[T[u].X]){
                stack.PB(T[u].X);
                continue;
            }
            else if (visited[T[u].X] && !visited[T[u].Y]){
                fuUnion(fuFind(T[u].X),fuFind(u));
                ancestor[fuFind(u)] = u;
                stack.PB(T[u].Y);
                continue;
            } else{
                fuUnion(fuFind(T[u].Y),fuFind(u));
                ancestor[fuFind(u)] = u;
            }
        }
        visited[u] = 1;
        stack.pop_back();
        if (u < n)
            for(auto e : lcaPairs[u])
                if (visited[e.X]){
                    int res = ancestor[fuFind(e.X)];
                    if (res < forestSize)
                        lca[res-n].PB(e.Y);
                }
    }
}

void offlineLCA(){
    lca.resize(forestSize-n);
    ancestor.resize(SIZE(T));
    lcaPairs.resize(n);
    REP(i,SIZE(react)){
        lcaPairs[react[i].X].PB(MP(react[i].Y,i));
        lcaPairs[react[i].Y].PB(MP(react[i].X,i));
    }
    visited.resize(SIZE(T),0);
    fuInit(SIZE(T));
    stack.PB(root);
    doLCA();
}

LL total = 0;

void DFS(int v){
    if (T[v].X != -1){
        DFS(T[v].X);
        DFS(T[v].Y);
    }
    if (v >= n && v < forestSize){
        sort(ALL(lca[v-n]));
        for(auto i : lca[v-n]){
            int rx = react[i].X, ry = react[i].Y;
            int proc = min(amount[rx],amount[ry]);
            amount[rx] -= proc;
            amount[ry] -= proc;
            total += 2*proc;
        }
    }
}

int main(int argc, char *argv[]){
    readData();
    makeForest();
    offlineLCA();
    DFS(root);
    printf("%lld\n",total);
    return 0;
}