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
#include <iostream>
#include <map>
#include <set>

using namespace std;

long long polacz(set<int> *s, set<int> *t, int *g, int *ra, int *rb)
{
    long long osady = 0;
    long long o;
    for (set<int>::iterator it = t->begin(); it != t->end(); ++it)
    {
        if (s->find(*it) == s->end())
        {
            s->insert(*it);
        }
        else
        {
            s->erase(*it);
            o = min(g[ra[*it]], g[rb[*it]]);
            g[ra[*it]] -= o;
            g[rb[*it]] -= o;
            osady += o*2;
        }
    }
    delete t;
    return osady;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

    int n, m, k;
    int *g;
    int *ka, *kb;
    int i, j;
    int x, y;
    set<int> **r;
    int *ra, *rb;

    cin >> n >> m >> k;
    g = new int[n];
    ka = new int[m];
    kb = new int[m];
    r = new set<int>*[n];
    ra = new int[k];
    rb = new int[k];
    for (i = 0; i < n; ++i)
    {
        cin >>  g[i];
    }
    for (i = 0; i < m; ++i)
    {
        cin >> x >> y;
        ka[i] = x - 1;
        kb[i] = y - 1;
    }
    for (i = 0; i < n; ++i)
    {
        r[i] = new set<int>;
    }
    for (i = 0; i < k; ++i)
    {
        cin >> x >> y;
        ra[i] = x - 1;
        rb[i] = y - 1;
        r[x - 1]->insert(i);
        r[y - 1]->insert(i);
    }

    long long osad = 0;
    set<int> *s, *t;
    map<int, set<int>*> reakcje;
    map<int, set<int>*>::iterator ia, ib;
    for (i = 0; i < m; ++i)
    {
        x = ka[i];
        y = kb[i];
        ia = reakcje.find(x);
        ib = reakcje.find(y);
        if (ia == reakcje.end() && ib == reakcje.end())
        {
            if (r[y]->size() < r[x]->size())
            {
                s = r[x];
                t = r[y];
            }
            else
            {
                s = r[y];
                t = r[x];
            }
            reakcje[y] = s;
        }
        else if (ia == reakcje.end())
        {
            if (ib->second->size() < r[x]->size())
            {
                s = r[x];
                t = ib->second;
                ib->second = s;
            }
            else
            {
                s = ib->second;
                t = r[x];
            }
        }
        else if (ib == reakcje.end())
        {
            if (r[y]->size() < ia->second->size())
            {
                s = ia->second;
                t = r[y];
            }
            else
            {
                s = r[y];
                t = ia->second;
            }
            reakcje[y] = s;
            reakcje.erase(ia);
        }
        else
        {
            if (ib->second->size() < ia->second->size())
            {
                s = ia->second;
                t = ib->second;
            }
            else
            {
                s = ib->second;
                t = ia->second;
            }
            ib->second = s;
            reakcje.erase(ia);
        }
        osad += polacz(s, t, g, ra, rb);
    }

    cout << osad << '\n';
}