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

using namespace std;

const int MAXN = 200000;
const int MAXM = 200000;
const int MAXK = 500000;

list<int> flasks[MAXN + 1];
int flasksSize[MAXN + 1];
int substances[MAXN + 1];
int steps[MAXM][2];
int reactions[MAXK][2];
list< pair<int, int> > reactionsList[MAXN + 1];
priority_queue< int, vector<int>, greater<int> > reactionsQueue;

int nodeP[MAXN + 1];
int nodeRank[MAXN + 1];

void makeSet(int x) {
    nodeP[x] = x;
    nodeRank[x] = 0;
}

int findSet(int x) {
    if (x != nodeP[x]) nodeP[x] = findSet(nodeP[x]);
    return nodeP[x];
}

void linkSet(int x, int y) {
    if (nodeRank[x] > nodeRank[y]) nodeP[y] = x;
    else {
        nodeP[x] = y;
        if (nodeRank[x] == nodeRank[y]) nodeRank[y] += 1;
    }
}

void unionSet(int x, int y) {
    linkSet(findSet(x), findSet(y));
}

int main() {
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    int g;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &g);
        flasks[i].push_back(i);
        flasksSize[i] = 1;
        substances[i] = g;
        makeSet(i);
    }
    int a, b;
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &a, &b);
        steps[i][0] = a;
        steps[i][1] = b;
    }
    int c, d;
    for (int i = 0; i < k; i++) {
        scanf("%d%d", &c, &d);
        reactions[i][0] = c;
        reactions[i][1] = d;
        pair<int, int> ci(c, i);
        pair<int, int> di(d, i);
        reactionsList[c].push_back(di);
        reactionsList[d].push_back(ci);
    }

    long long res = 0;
    int aSize, bSize;
    int smallerFlask, biggerFlask;
    int smallerFlaskSet, biggerFlaskSet;
    int substance, reactionSubstance, reactionOrder;
    int reaction, result;
    list<int>::iterator flasksIt;
    list< pair<int, int> >::iterator reactionsListIt;

    for (int i = 0; i < m; i++) {
        a = steps[i][0];
        b = steps[i][1];

        // find reactions
        aSize = flasksSize[a];
        bSize = flasksSize[b];
        //printf("mixing: %d (%d) -> %d (%d)\n", a, aSize,  b, bSize);
        if (aSize < bSize) {
            smallerFlask = a;
            biggerFlask = b;
        }
        else {
            smallerFlask = b;
            biggerFlask = a;
        }
        smallerFlaskSet = findSet(*(flasks[smallerFlask].begin()));
        biggerFlaskSet = findSet(*(flasks[biggerFlask].begin()));
        for (flasksIt = flasks[smallerFlask].begin(); flasksIt != flasks[smallerFlask].end(); ++flasksIt) {
            substance = *flasksIt;
            if (substances[substance]) {
                for (reactionsListIt = reactionsList[substance].begin(); reactionsListIt != reactionsList[substance].end(); ++reactionsListIt) {
                    reactionSubstance = (*reactionsListIt).first;
                    reactionOrder = (*reactionsListIt).second;
                    if (substances[reactionSubstance]) {
                        if (findSet(reactionSubstance) == findSet(biggerFlaskSet)) {
                            //printf("    found reaction %d: %d -> %d\n", reactionOrder, substance, reactionSubstance);
                            reactionsQueue.push(reactionOrder);
                        }
                    }
                }
            }
        }

        // do reactions in order
        while (!reactionsQueue.empty()) {
            reaction = reactionsQueue.top();
            reactionsQueue.pop();
            c = reactions[reaction][0];
            d = reactions[reaction][1];
            //printf("    doing reaction %d: %d -> %d\n", reaction, c, d);
            result = min(substances[c], substances[d]);
            res += 2 * result;
            substances[c] -= result;
            substances[d] -= result;
        }

        // splice flasks
        flasks[b].splice(flasks[b].end(), flasks[a]);
        flasksSize[b] += flasksSize[a];
        flasksSize[a] = 0;
        unionSet(smallerFlaskSet, biggerFlaskSet);
    }

    printf("%lld\n", res);
    return 0;
}