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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

struct Edge
{
    int u, v;
    
    vector <int> reactions;
    
    void load()
    {
        scanf ("%d%d", &u, &v);
    }
};

int m;

int volume[200001], F[200001];
Edge E[199999];

int find(int a)
{
    if (F[a] == a)
        return a;
        
    return F[a] = find(F[a]);
}

struct Reaction
{
    int a, b, begin, end, order;
    
    void load(int i)
    {
        scanf ("%d%d", &a, &b);
        
        order = i;
        begin = -1;
        end = m;
        
        E[m / 2].reactions.push_back(i);
    }
    
    bool occurs()
    {
        return (find(a) == find(b));
    }
    
    int middle()
    {
        return ((begin + end) / 2);
    }
    
    bool operator< (const Reaction &other)  const
    {
        if (end == other.end)
            return order < other.order;
            
        return end < other.end;
    }
};

Reaction K[500000];

int main()
{
	int n, k;
    
    scanf ("%d%d%d", &n, &m, &k);
    
    for (int i=1; i<=n; i++)
        scanf ("%d", &volume[i]);
        
    for (int i=0; i<m; i++)
        E[i].load();
        
    for (int i=0; i<k; i++)
        K[i].load(i);
        
    int unsolvedReactions = k;
    
    while (unsolvedReactions)
    {
        for (int i=1; i<=n; i++)
            F[i] = i;
            
        for (int i=0; i<m; i++)
        {
            F[E[i].u] = E[i].v;
            
            while (!E[i].reactions.empty())
            {
                int reaction = E[i].reactions.back();   //  numer aktualnie spawdzanej reakcji
                
                K[reaction].occurs() ? K[reaction].end = i  : K[reaction].begin = i;
                
                E[i].reactions.pop_back();
            }
        }
        
        for (int i=0; i<k; i++)
        {
            if (K[i].end - K[i].begin > 1)
                E[K[i].middle()].reactions.push_back(i);
                
            else if (K[i].end - K[i].begin == 1)
            {
                K[i].begin++;
                unsolvedReactions--;
            }
        }
    }
    
    sort (K, K + k);
    
    long long res = 0;
    
    for (int i=0; i<k; i++)
    {
        if (K[i].end == m)
            break;
            
        int Min = min(volume[K[i].a], volume[K[i].b]);
        
        volume[K[i].a] -= Min;
        volume[K[i].b] -= Min;
        
        res += (Min * 2);
    }
    
    printf("%lld\n", res);
    
    return 0;
}