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
/*************************************************************************
 *   Zadanie:           Fiolki                                           *
 *   Zlozonosc czasowa: O(n + (m+k) log^2 m)                             *
 *   Wynik:             --/10                                            *
 *************************************************************************/

#include <iostream>
#include <vector>
#include <algorithm>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define SIZE(c) (int) (c).size()
#define FOREACH(i,c) FOR(i,0,SIZE(c)-1)
#define MIN(x,y) ( ((x) < (y))? (x) : (y) )
#define MAX(x,y) ( ((x) > (y))? (x) : (y) )
#define PB push_back
#define MP make_pair
#define ST first
#define ND second
#define INF 1000000

using namespace std;

typedef pair < int , int > PII;

/*************************************************************************/

struct FAU
{
    vector < int > p; //parent
    vector < int > r; //rank
    vector < PII > history;
    vector < int > history_r;

    FAU(void) {}

    void Resize(int n)
    {
        while(SIZE(p) < n)
        {
            p.PB(-1);
            r.PB(0);
        }
    }

    int Find(int x)
    {
        if (p[x] == -1) return x;

        return Find(p[x]);
    }

    bool Together(int x, int y)
    {
        return (Find(x) == Find(y));
    }

    void Union(int x, int y)
    {
        x = Find(x);
        y = Find(y);

        if (x == y)
        {
            history.PB( MP(-1,-1) );
            history_r.PB(-1); //pusty wpis

            return ;
        }

        if (r[x] > r[y])
        {
            history.PB( MP(y,p[y]) );
            p[y] = x;
        }
        else
        {
            history.PB( MP(x,p[x]) );
            p[x] = y;
        }

        if (r[x] == r[y])
        {
            ++r[y];
            history_r.PB(y);
        }
        else
            history_r.PB(-1);

        return ;
    }

    void Restore(int z) //cofa 'z' ostatnich operacji Union
    {
        FOR(i,1,z)
        {
            PII b = history.back();
            history.pop_back();

            int y = history_r.back();
            history_r.pop_back();

            if (y != -1) --r[y];
            if (b.ST != -1) p[b.ST] = b.ND;
        }
    }
};

/*************************************************************************/

FAU F;
vector < PII > Ch; //changes

struct react
{
    int x;
    int y;

    int id; //priorytet
    int t; //czas zajscia

    react(int nx = 0, int ny = 0, int nid = 0, int nt = INF)
        { x = nx; y = ny; id = nid; t = nt; }
};

bool comp(react a, react b)
{
    return ((a.t == b.t)? (a.id < b.id) : (a.t < b.t));
}

vector < react > R; //reactions

/*************************************************************************/

void Solve(int l, int r, vector < int > &V)
{
    /*  w V reakcje co do ktorych wiadomo ze zaszly w przedziale [l,r]
        w F stan FAU po reakcjach 1,...,l-1
        funkcja rozstrzyga kiedy zaszla ktora z reakcji z V             */

    if (l == r)
    {
        FOREACH(i,V) R[V[i]].t = l;
        return ;
    }

    vector < int > Vleft, Vright;
    //Vleft - [l,mid], Vright - [mid+1,r]

    int mid = (l+r)/2;

    FOR(i,l,mid) F.Union(Ch[i].ST,Ch[i].ND);

    FOREACH(i,V)
    {
        if (F.Together(R[V[i]].x,R[V[i]].y))
            Vleft.PB(V[i]);
        else
            Vright.PB(V[i]);
    }

    Solve(mid+1,r,Vright);

    F.Restore(mid-l+1);

    Solve(l,mid,Vleft);

    return ;
}

/*************************************************************************/

int main()
{
    ios_base::sync_with_stdio(0);

    int n, m, k;
    cin >> n >> m >> k;

    if ((m == 0) || (k == 0))
    {
        cout << 0;
        return 0;
    }

    vector < int > S(n); //substance
    Ch.resize(m);
    F.Resize(n);
    R.resize(k);

    FOREACH(i,S) cin >> S[i];

    FOREACH(i,Ch)
    {
        cin >> Ch[i].ST >> Ch[i].ND;
        --Ch[i].ST; --Ch[i].ND;
    }

    FOREACH(i,R)
    {
        cin >> R[i].x >> R[i].y;
        R[i].id = i;

        --R[i].x; --R[i].y;
    }

    FOREACH(i,Ch) F.Union(Ch[i].ST,Ch[i].ND); //wykonuje wszystkie zmiany

    FOREACH(i,R) if (!F.Together(R[i].x,R[i].y))
    { //wyrzuca reakcje ktore nigdy nie zajda
        swap(R[i],R[SIZE(R)-1]);
        R.pop_back();
        --i;
    }

    F.Restore(m); //cofa

    vector < int > ALL;
    FOREACH(i,R) ALL.PB(i);

    Solve(0,m-1,ALL); //spelniono zalozenia dla Solve

    sort(R.begin(), R.end(), comp);

    long long int ans = 0;

    FOREACH(i,R)
    {
        int d = MIN(S[R[i].x],S[R[i].y]);

        S[R[i].x] -= d;
        S[R[i].y] -= d;

        ans += (2*d);
    }

    cout << ans;

    return 0;
}

/*************************************************************************/