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
#define debug if(0)
// Grzegorz Guspiel
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i,n) for(int i=0;i<int(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second

template<typename T> void maxE(T& a, const T& b) { a = max(a, b); }
template<typename T> void minE(T& a, const T& b) { a = min(a, b); }

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& t) {
    out << "[";
    FOREACH (i, t) out << *i << ", ";
    out << "]";
    return out;
}

template<typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& rhs) {
    out << "(" << rhs.st << "," << rhs.nd << ")";
    return out;
}

const int MAXN = 500 * 1000 + 1000;

struct Query {
    int a, b;
    int priority;
    int resultNode;
    Query(): resultNode(-1) {}
};

struct Operation {
    int a, b, priority;
};
bool operator<(const Operation& a, const Operation& b) {
    return a.priority < b.priority;
}

vector<int> ch[MAXN];
int root;
int vialNode[MAXN];
int amounts[MAXN];
Query queries[MAXN];
vector<Query*> queriesAtNode[MAXN];
vector<Operation> operationsAtNode[MAXN];
int sp[MAXN];
bool vis[MAXN];
int numNodes, numVials, numSteps, numReactions;
long long result;

int fs(int a) {
    if (sp[a] != sp[sp[a]]) sp[a] = fs(sp[a]);
    return sp[a];
}
void us(int a, int b) {
    a = fs(a); b = fs(b);
    sp[a] = b;
}

void tarjan(int u) {
    FOREACH (v, ch[u]) {
        tarjan(*v);
        us(*v, u);
    }
    vis[u] = 1;
    FOREACH (q, queriesAtNode[u]) {
        int he = (*q)->a; if ((*q)->a == u) he = (*q)->b;
        if (!vis[he]) continue;
        debug cout << "p of " << (*q)->a << " " << (*q)->b << " is " << fs(he) << endl;
        (*q)->resultNode = fs(he);
        Operation op; op.a = (*q)->a; op.b = (*q)->b;
        op.priority = (*q)->priority;
        operationsAtNode[fs(he)].pb(op);
    }
}

void tarjanAlgorithm() {
    REP (i, numNodes) sp[i] = i;
    tarjan(root);
}

void dfs(int a) {
    FOREACH (i, ch[a]) dfs(*i);
    sort(ALL(operationsAtNode[a]));
    if (a != root) FOREACH (i, operationsAtNode[a]) {
        int x = min(amounts[i->a], amounts[i->b]);
        amounts[i->a] -= x;
        amounts[i->b] -= x;
        result += x * 2;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> numVials >> numSteps >> numReactions;
    REP (i, numVials) cin >> amounts[i];
    numNodes = numVials;
    REP (i, numVials) vialNode[i] = i;
    REP (i, numSteps) {
        int from; cin >> from; from--;
        int to; cin >> to; to--;
        ch[numNodes].pb(vialNode[from]);
        ch[numNodes].pb(vialNode[to]);
        vialNode[from] = -1;
        vialNode[to] = numNodes;
        numNodes++;
    }
    REP (i, numVials) if (vialNode[i] != -1) {
        ch[numNodes].pb(vialNode[i]);
    }
    root = numNodes++;
    debug cout << "root " << root << endl;
    REP (i, numReactions) {
        int subst1; cin >> subst1; subst1--;
        int subst2; cin >> subst2; subst2--;
        queries[i].a = subst1;
        queries[i].b = subst2;
        queries[i].priority = i;
        queriesAtNode[subst1].pb(queries + i);
        queriesAtNode[subst2].pb(queries + i);
    }

    tarjanAlgorithm();
    dfs(root);
    cout << result << endl;
	return 0;
}