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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <typeinfo>
#include <vector>

#include <unordered_set>
#include <unordered_map>
#include <bitset>

using namespace std;
#define rep(i,b) for(auto i=(0);i<(b);++i)
#define fo(i,a,b) for(auto i=(a);i<=(b);++i)
#define ford(i,a,b) for(auto i=(a);i>=(b);--i)
#define fore(a,b) for(auto &a:(b))
#define v vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define clr(x,a) memset(x,a,sizeof(x))

template <typename T, size_t N> struct ma : array<T,N> { T& operator[](size_t n) {
#ifdef DEBUG
           assert(n < N);
#endif
return (*static_cast<array<T,N>*>(this))[n]; } } ; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef ostringstream oss; typedef istringstream iss;
template<class T> string to_str(const T &a) { oss os; os << a; return os.str(); }
template<> string to_str<ld>(const ld& a) { oss os; os.precision(10); os.setf(ios::fixed); os << a; return os.str(); }
template<class T> T from_str(const string &s) { iss is; T val; is >> val; return val; }

int cond = (ll)0;
#define db(x...) { if (cond > 0) { cond--; rep (xxx, 1) cerr << __LINE__ << " " << #x << " " << x << endl; cerr.flush(); } }
#define dbv(x...) { if (cond > 0) { cerr << __LINE__<<": "; fore (it, x) cerr << it <<" "; cerr << endl; } }
//template <class C, class=typename C::iterator> struct _cprint { using type = void;}; template<> struct _cprint<string> {};
//template <class C, class=typename _cprint<C>::type> ostream& operator<<(ostream &o, const C& v){ for(auto x:v) o<<x<<" "; return o; }
//
//

int st = 0;

struct LCA {
    v<vi> ed, questions;
    vi color,fu, ancestor;
    v<array<int,3>> ret;

    void init(int n) {
        ed.resize(n);  questions.resize(n);
        color.resize(n, 0); fu.resize(n);  ancestor.resize(n);

        xx.resize(n + 1000);
        pparent.resize (n + 1000);
        ii.resize (n + 1000);
    }

    int fufind(int x){ return fu[x]<0?x:fu[x]=fufind(fu[x]); }

    vi xx, pparent, ii;
    void dfs2() {
        int rec = 0;
        xx[0] = 0;
        pparent[0] = -1;
        while (rec >= 0) {
beg:;
            int &x = xx[rec];
            int &parent = pparent[rec];
            if (ii[rec] == 0) {
                color[x]=1; fu[x]=-1;  ancestor[x]=x;
            }
            if (ii[rec] < 0) {
                int i = - (ii[rec] + 1);
                auto it = ed[x][i];
                int a=fufind(x),b=fufind(it);
                if (fu[a]<fu[b]) swap(a,b);
                fu[b]+=fu[a]; fu[a]=b;
                ancestor[b]=x;

                ii[rec] = i + 1;
            }
            for (int &i = ii[rec]; i < ed[x].size(); i++) {
                auto it = ed[x][i];
                if (it!=parent){
                    ii[rec] = -1 - i;
                    rec++;
                    xx[rec] = it;
                    pparent[rec] = x;
                    ii[rec] = 0;
                    goto beg;
//                    dfs(it,x);
            
                }
            }
            fore(it, questions[x]) {
                if (color[it]==2){
                    int lca= ancestor[fufind(it)];
                    int a = x, b = it;
                    if (!(a < b)) swap(a, b);
                    ret.pb({{a, b, lca}});
                    //printf("Najglebszym przodkiem wierzcholkow %d i %d jest wierzcholek %d.\n",x,it,lca);
                }
            }
            color[x]=2;
            rec--;
        }
    }

    void dfs(int x=0,int parent=-1){
        color[x]=1; fu[x]=-1;  ancestor[x]=x;
        for (int i = 0; i < ed[x].size(); i++) {
            auto it = ed[x][i];
            if (it!=parent){
                dfs(it,x);
                int a=fufind(x),b=fufind(it);
                if (fu[a]<fu[b]) swap(a,b);
                fu[b]+=fu[a]; fu[a]=b;
                ancestor[b]=x;
            }
        }

        fore(it, questions[x]) {
            if (color[it]==2){
                int lca= ancestor[fufind(it)];
                int a = x, b = it;
                if (!(a < b)) swap(a, b);
                ret.pb({{a, b, lca}});
                //             printf("Najglebszym przodkiem wierzcholkow %d i %d jest wierzcholek %d.\n",x,it,lca);
            }
        }
        color[x]=2;

    }
    //Begin of the code
    void add_edge(int a,int b){
        ed[a].pb(b);
        ed[b].pb(a);
    }

    void add_question(int a,int b){
        if (a == b) {
            ret.pb({{a, a, a}});
            return;
        }
        questions[a].pb(b);
        questions[b].pb(a);
    }
};

const int BITS = 20;
struct LCA2 {
    int n;
    vector<array<int, BITS>> P;

    LCA2(int _n) : n(_n) {
        P.resize(n);
        rep (i, n) P[i][0] = -1;
    }

    void add_edge(int a, int b) {
        P[b][0] = a;
    }

    void dfs() {
        fo (j, 1, BITS-1)
        rep (i, n) {
            int par = P[i][j-1];
            if (par == -1 || P[par][j-1] == -1) {
                P[i][j] = -1;
                continue;
            }
            P[i][j] = P[par][j-1];
        }
    }

    int max_dep(int a) {
        int ret = 0;
        ford (i, BITS-1, 0) {
            if (P[a][i] != -1) {
                a = P[a][i];
                ret |= 1<<i;
            }
        }
        return ret;
    }

    int go_down(int a, int len) {
        ford (i, 19, 0) 
            if (len & (1<<i))
                a = P[a][i];
        return a;
    }

    int query(int a, int b) {
        int da = max_dep(a);
        int db = max_dep(b);
        if (da > db) a = go_down(a, da - db);
        if (da < db) b = go_down(b, db - da);
        ford (j, BITS-1, 0) {
            if (P[a][j] != P[b][j]) {
                a = P[a][j];
                b = P[b][j];
            }
        }
        //cerr<<" "<<da<<" "<<db<<endl;
        //cerr<<"XX "<<P[a][0]<<" "<<P[b][0]<<endl;;
        assert(P[a][0] == P[b][0]);
        return P[a][0];
    }
};


int main2() {
    LCA lca;
    lca.init(5);
    // a < b then a is ancestor
    lca.add_edge(0, 3);
    lca.add_edge(0, 1);

    lca.add_question(0, 3);
    lca.add_question(0, 1);
    lca.add_question(1, 3);
    lca.add_question(0, 3);

    lca.dfs();
    auto ret = lca.ret;
    sort(all(ret));
    fore (it, ret) {
        cout << it[0] << " " << it[1] << " "<<it[2] << endl; }
    // q[0], q[1], lca(q[0], q[1])
    // any order
    // 3 1 3
    // 0 3 0
    // 0 1 0
    // 0 3 0


    return 0;
}

vi fiol;
typedef array<int,2> d2;
typedef array<int,3> d3;
v<d2> qu;


struct fus {
    int n;
    vi PA;
    fus (int _n) : n(_n), PA(n) {
        rep (i, n) PA[i] = i;
    }

    int parent(int i) {
        if (PA[i] == i) return i; else return PA[i] = parent(PA[i]);
    }
};

v<d2> pary;

//#define DUMMY

void solve() {
    int n, m, k;
#ifndef DUMMY
        scanf("%d%d%d", &n, &m, &k);
#else
    n = 200000;
    m = n - 1;
    k = 500000;
#endif
    db(n<<" "<<m<<" "<<k);


    fiol.resize(n);
    qu.resize(m);
    rep (i, n) {
#ifndef DUMMY
        scanf("%d", &fiol[i]);
#else
        fiol[i] = i + 324;
#endif
    }
    rep (i, m) {
        int &a = qu[i][0], &b = qu[i][1];
#ifndef DUMMY
        scanf("%d%d", &a, &b);
#else
        a = i + 1; b = i + 2;
        assert(a >= 1 && a <= n);
        assert(b >= 1 && b <= n);
#endif
        if (!(a<b)) swap(a,b);
        a--; b--;

    }
    pary.resize(k);
    rep (i, k) {
        int &a = pary[i][0]; int &b = pary[i][1];
#ifndef DUMMY
        scanf("%d%d", &a, &b);
#else
        a = i % n + 1;
        b = (i + 1) % n + 1;
        assert(a >= 1 && a <= n);
        assert(b >= 1 && b <= n);
#endif
        if (!(a<b)) swap(a,b);
        a--;
        b--;
    }
    LCA2 lca(n + m);
    fus fu(n + m);

    rep (i, m) {
        int &a = qu[i][0], &b = qu[i][1];
        if (!(a < b)) swap(a, b);
        int pa = fu.parent(m+a);
        int pb = fu.parent(m+b);
        assert(pa != pb);

        int npar = (m - 1) - i;

        db(m+a<<" "<<m+b<<" "<<pa<<" "<<pb<<" "<<npar);
        fu.PA[pa] = npar;
        fu.PA[pb] = npar;
        lca.add_edge(npar, pa);
        lca.add_edge(npar, pb);
        assert(fu.parent(npar) == npar);

        db(m+a<<" "<<m+b);
    }
    lca.dfs();
    v<d2> kol;
    rep (i, k) {
        int &a = pary[i][0];
        int &b = pary[i][1];
        if (!(a < b)) swap(a, b);
        int lucid = lca.query(m+a, m+b);
        if (fu.parent(m+a) == fu.parent(m+b)) {
            kol.pb({{-lucid, i}});
        }
        else
            assert(lucid == -1);
    }

    sort(all(kol));
    db("");
    ll res = 0;
    fore (it, kol) {
        int i = it[1];
        int &a = pary[i][0]; int &b = pary[i][1];
        int mi = min(fiol[a], fiol[b]);
        fiol[a] -= mi;
        fiol[b] -= mi;
        res += mi * 2;
    }
    db("");
    cout << res << endl;
}

int main(int argc, char ** argv) {
    ios::sync_with_stdio(false);
    //  freopen("../1.in","r",stdin); 
    //  freopen("../2.in","r",stdin); 
    //  freopen("../3.in","r",stdin); 
    //  freopen("../A-small.in","r",stdin); freopen("../A-small.out","w",stdout);
    //  freopen("../A-small-attempt0.in","r",stdin);freopen("../A-small-attempt0.out","w",stdout);
    //  freopen("../A-small-attempt1.in","r",stdin);freopen("../A-small-attempt1.out","w",stdout);
    //  freopen("../A-small-attempt2.in","r",stdin);freopen("../A-small-attempt2.out","w",stdout);
    //  freopen("../A-large.in","r",stdin); freopen("../A-large.out","w",stdout)

    //main2();
    vs args;
    int nr_test = -1;
    int t = 1;

    fo (i, 1, argc - 1) args.pb(argv[i]);
    fore (it, args) if (it == "q") cond = (int)1e9;
    fore (it, args) if (it == "all") t = 1000;
    fore (it, args) if (atoi(it.c_str()) > 0) nr_test = atoi(it.c_str());

    fo (i, 1, t) {
        solve();
    }
    return 0;
}