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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>

using namespace std;

typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PII;

#define FOR(x, b, e) for (int x = b; x <= (e); ++x)
#define FORD(x, b, e) for (int x = b; x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair

#define INF 1000000001

struct Phial;

struct Reaction {
    int i, k;
    Phial *x, *y;

    Reaction(int _i = 0, int _k = 0, Phial* _x = 0, Phial* _y = 0)
        : i(_i), k(_k), x(_x), y(_y)
    { }

    bool operator<(const Reaction& r) const {
        if (i == r.i) return k > r.k;
        return i > r.i;
    }
};

struct Phial {
    int   rank;
    Phial *par, *plca;

    int g, k;
    priority_queue<Reaction> qu;

    Phial() : rank(0), par(0), g(0), k(-1), qu() {
        par = plca = this;
    }
};

Phial* FindSet(Phial* p)
{
    if (p != p->par) p->par = FindSet(p->par);
    return p->par;
}

void Link(Phial* x, Phial* y)
{
    if (x == y) return;

    if (x->rank > y->rank)
        y->par = x;
    else
        x->par = y;

    if (x->rank == y->rank) ++y->rank;
}

void Union(Phial* x, Phial* y)
{
    x->plca = y;
    Link(FindSet(x), FindSet(y));
}

vector<Phial*> Path(Phial* x)
{
    vector<Phial*> v;
    for (; x != x->plca; x = x->plca)
        v.PB(x);
    v.PB(x);
    return v;
}

// naive :(
int LCA(Phial* x, Phial* y)
{
    vector<Phial*> px = Path(x);
    vector<Phial*> py = Path(y);

    int xs = SIZE(px) - 1;
    int ys = SIZE(py) - 1;
    while (xs >= 0 && ys >= 0 && px[xs] == py[ys]) --xs, --ys;
    int k = -1;
    REP(i, xs + 1) k = max(k, px[i]->k);
    REP(j, ys + 1) k = max(k, py[j]->k);
    return k;
}

int main()
{
    int n, m, k, g, a, b, c, d;
    scanf("%d %d %d\n", &n, &m, &k);

    vector<Phial> P(n);
    REP(i, n) {
        scanf("%d", &g);
        P[i].g = g;
    }

    REP(i, m) {
        scanf("%d %d\n", &a, &b);
        Phial* x = &P[--a];
        Phial* y = &P[--b];
        x->k = i;

        Union(x, y);
    }

    REP(i, k) {
        scanf("%d %d\n", &c, &d);
        Phial* x = &P[--c];
        Phial* y = &P[--d];
        Phial *p = FindSet(x);
        if (p == FindSet(y))
            p->qu.push(Reaction(max(x->k, y->k), i, x, y));
    }

    g = 0;
    FOREACH(it, P) {
        while (!it->qu.empty()) {
            Reaction r = it->qu.top();
            it->qu.pop();

            int l = min(r.x->g, r.y->g);
            g += 2 * l;
            r.x->g -= l;
            r.y->g -= l;
        }
    }
    printf("%d\n", g);

    return 0;
}