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
// Mateusz Kurek, PA 2014
// 5B FIO

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<string>
#include<list>
#include<deque>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<sstream>
#include<cstring>
#include <string>
#include <unistd.h>

//#include <iostream>

using namespace std;

#define REP(i,v)for(int i=0;i<(v);++i)
#define FOR(i,x,v)for(int i=x;i<=(v);++i)
#define FORD(i,x,v)for(int i=x;i>=(v);--i)
#define VAR(v,n) __typeof(n) v = (n)
#define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define ALL(c) (c).begin(), c.end()
#define PB push_back
#define SZ size
#define MP make_pair
#define FI first
#define SE second
#define CL clear()
#define RS resize
#define INFTY 1000000001
#define EPS 10e-9
#define SIZE(x) ((int)(x).size())

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<bool> VB;
typedef pair<int,int> PII;
typedef long long LL;
typedef vector<string> VS;

void show(PII p)
{printf("(%d %d)\n",p.FI,p.SE);}

void show(VI e)
{REP(i,SIZE(e)) printf("%d ",e[i]); printf("\n");}

void show(vector<PII> e)
{REP(i,SIZE(e)) printf("(%d %d) ",e[i].FI,e[i].SE); printf("\n");}

void show(VS e)
{REP(i,SIZE(e)) printf("%s\n",e[i].c_str());}

void show(VVI e)
{REP(i,SIZE(e)) show(e[i]);}

#define MAXN 200002
#define MAXM 200002
#define MAXK 500002

long long lost = 0;
int subst_left[MAXN], rep[MAXN], n, m, k;
int reactions_1[MAXK], reactions_2[MAXK], steps_1[MAXN], steps_2[MAXN];
set<int> phial_reactions[MAXN], tmp;
set<int>::iterator it, it2;

int Find(const int &a)
{
    if(rep[a] == a) return a;

    int fa = Find(rep[a]);
    rep[a] = fa;
    return fa;
}

void make_reaction(int el1, int el2){

    int m = min(subst_left[el1], subst_left[el2]);
    // printf("Reaguje %d z substancji %d i %d\n", m, el1, el2);
    lost += m*2;
    subst_left[el1] -= m;
    subst_left[el2] -= m;
}

bool Union(const int &from, const int &to)
{
    PII reaction;
    int f_from = from, // Find(from),
        f_to = to, // Find(to),
        react_iter,
        react_apply;

    // printf("przelewam z %d do %d\n", from, to);
    if(f_from == f_to)
        return false;

    // apply reactions
    if(phial_reactions[f_from].size() <= phial_reactions[f_to].size()){
        // apply reaction from f_from to f_to
        react_iter = f_from;
        react_apply = f_to;
    }
    else{
        react_iter = f_to;
        react_apply = f_from;
    }
    // printf("Aplikuje reakcje z %d na %d\n", react_iter, react_apply);

    for(it=phial_reactions[react_iter].begin(); it != phial_reactions[react_iter].end(); ){
        // reaction = reactions[*it];
        if(Find(reactions_1[*it]) == react_apply || Find(reactions_2[*it]) == react_apply){
            // printf("%d lub %d znaleziony w %d\n", reaction.first, reaction.second, react_apply);
            make_reaction(reactions_1[*it], reactions_2[*it]);
            phial_reactions[react_apply].erase(*it);
            phial_reactions[react_iter].erase(it++);
        }
        else{
            ++it;
        }
    }

    // union
    // phial_count[f_to] += phial_count[f_from];
    rep[f_from] = f_to;

    if(phial_reactions[f_from].size() >= phial_reactions[f_to].size()){
        tmp = phial_reactions[f_from];
        phial_reactions[f_from] = phial_reactions[f_to];
        phial_reactions[f_to] = tmp;
    }

    it2 = phial_reactions[f_to].begin();
    for(it=phial_reactions[f_from].begin(); it != phial_reactions[f_from].end(); ++it){
        if(subst_left[reactions_1[*it]] > 0 && subst_left[reactions_2[*it]] > 0){
            it2 = phial_reactions[f_to].insert(it2, *it);
        }
    }
    // phial_reactions[f_to].insert(phial_reactions[f_from].begin(), phial_reactions[f_from].end());
    phial_reactions[f_from].clear();

    // }
    // else{
    //     it2 = phial_reactions[f_from].begin();
    //     for(it=phial_reactions[f_to].begin(); it != phial_reactions[f_to].end(); ++it){
    //         if(subst_left[reactions_1[*it]] > 0 && subst_left[reactions_2[*it]] > 0){
    //             it2 = phial_reactions[f_from].insert(it2, *it);
    //         }
    //     }
    //     phial_reactions[f_to].clear(); // ?
    //     phial_reactions[f_to] = phial_reactions[f_from];
    // }

    // printf("Elementy w zbiorze po połączeniu: \n");
    // FOR(i, 1, n){
    //     if(Find(i) == f_to){
    //         printf("%d ", i);
    //     }
    // }
    // printf("\nReakcje fiolek po połączeniu:\n");
    // for (it=phial_reactions[f_to].begin(); it!=phial_reactions[f_to].end(); ++it){
    //     printf("%d (%d <-> %d) ", *it, reactions[*it].first, reactions[*it].second);
    // }
    // printf("\n");

    // remove reactions with 0 subst
    // for(it=phial_reactions[f_to].begin(); it != phial_reactions[f_to].end(); ){
    //     if(subst_left[reactions_1[*it]] == 0 || subst_left[reactions_2[*it]] == 0){
    //         phial_reactions[f_to].erase(it++);
    //     }
    //     else{
    //         ++it;
    //     }
    // }
    return true;
}

int main(){
    int a, b, c, d;
    scanf("%d %d %d", &n, &m, &k);
    FOR(i, 1, n){
        scanf("%d", &subst_left[i]);
        rep[i] = i;
        // phial_count[i] = 1;
    }
    REP(i, m){
        scanf("%d %d", &a, &b);
        steps_1[i] = a;
        steps_2[i] = b;
    }
    REP(i, k){
        scanf("%d %d", &c, &d);
        reactions_1[i] = c;
        reactions_2[i] = d;
        phial_reactions[c].insert(i);
        phial_reactions[d].insert(i);
    }
    // printf("Ile substancji: \n");
    // FOR(i, 1, n){
    //     printf("%d:%d ", i, subst_left[i]);
    // }
    // printf("\n");

    // printf("Going sleep\n");
    // usleep(10000000);

    REP(i, m){
        Union(steps_1[i], steps_2[i]);
    }

    printf("%lld\n", lost);
    // int s = 0;
    // FOR(i, 1, n){
    //     s += phial_reactions[i].size();
    // }
    // printf("reactions size: %d (total reactions: %d)\n", s, k);
    // printf("Going sleep\n");
    // usleep(10000000);
    return 0;
}