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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <set>
#include <vector>
#include <ext/pool_allocator.h>
//#include <sys/types.h>
//#include <unistd.h>

typedef long long LL;

struct Sets
{
    Sets(int count) : rank_(count, 0), parent_(count)
    {
        for (int i=0; i<parent_.size(); ++i)
            parent_[i] = i;
    }
    void join(int x, int y)
    {
        int xr = find(x);
        int yr = find(y);
        if (rank_[xr] > rank_[yr])
            parent_[yr] = xr;
        else if (rank_[xr] < rank_[yr])
            parent_[xr] = parent_[yr];
        else if (xr != yr) {
            parent_[yr] = parent_[xr];
            ++ rank_[xr];
        }
    }
    int find(int x) const
    {
        if (x==parent_[x])
            return x;
        return parent_[x] = find(parent_[x]);
    }
private:
    std::vector<int> rank_;
    mutable std::vector<int> parent_;
};

struct Reaction
{
    bool operator< (const Reaction& other) const
    {
        return id < other.id;
    }

    int id;
    int s1, s2;
};

int readint()
{
    int k = getchar();
    for ( ; k<'0' || k>'9'; )
        k = getchar();
    int ret = k - '0';
    k = getchar();
    for ( ; '0'<=k && k<='9'; ) {
        ret = ret * 10 + k - '0';
        k = getchar();
    }
    return ret;
}

int main()
{
    int n = readint();
    int m = readint();
    int k = readint();

    std::vector<int> substances(n+1);
    Sets sets(n+1);
    for (int i=1; i<=n; ++i)
        substances[i] = readint();

    std::vector<std::pair<int, int> > join(m);
    for (int i=0; i<m; ++i) {
        join[i].first = readint();
        join[i].second = readint();
    }

    typedef std::set<Reaction, std::less<Reaction>, __gnu_cxx::__pool_alloc<Reaction> > Set;
    std::vector<Set> reactions(n+1);
    for (int i=0; i<k; ++i) {
        Reaction r;
        r.id = i;
        r.s1 = readint();
        r.s2 = readint();
        reactions[r.s1].insert(r);
        reactions[r.s2].insert(r);
    }

    LL left = 0;

    for (int i=0; i<m; ++i)
    {
        int parent_from = sets.find(join[i].first);
        int parent_to = sets.find(join[i].second);

        sets.join(parent_from, parent_to);

        if (reactions[parent_from].size() > reactions[parent_to].size())
            std::swap(parent_from, parent_to);

        // iter parent_from reactions, it is smaller
        for (Set::iterator it=reactions[parent_from].begin(); it!=reactions[parent_from].end(); ++it)
        {
            if (sets.find(it->s1) != sets.find(it->s2))
                reactions[parent_to].insert(*it);
            else 
            {
                // try to react
                int drugs = std::min(substances[it->s1], substances[it->s2]);
                substances[it->s1] -= drugs;
                substances[it->s2] -= drugs;
                left += drugs;
            }
        }

        int parent = sets.find(parent_to);
        std::swap(reactions[parent], reactions[parent_to]);

        if (parent != parent_from)
            reactions[parent_from].clear();
        if (parent != parent_to)
            reactions[parent_to].clear();
    }

    printf("%lld\n", left*2);

    /*std::ostringstream os;
    os << "ps --no-headers up " << getpid();
    std::string command = os.str();
    system(command.c_str());*/

    return 0;
}