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
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#define MAXN 200001
using namespace std;
std::vector<int> fi;
std::vector<int> redirect;
std::vector<int> registers[MAXN];

std::vector<pair<int,int> > seq;
std::vector<pair<int,int> > reactions;

typedef long long int lli;

static lli addfs(int n1,int n2) {
    lli result=0;

    if (registers[n1].size() != 0 ) {
        std::vector<int> inter;
        std::vector<int> merged;
        std::vector<int>::iterator it;
        std::set_intersection( registers[n1].begin(), registers[n1].end(),
                               registers[n2].begin(), registers[n2].end(),
                               std::back_inserter( inter ) ); 
 
        merged.resize(registers[n1].size() + registers[n2].size());
        it = std::set_symmetric_difference(registers[n1].begin(), registers[n1].end(),
                   registers[n2].begin(), registers[n2].end(),
                   merged.begin()); 

        merged.resize(it - merged.begin());
        registers[n2] = merged;

        for (int i=0;i<inter.size();i++) {
            int f1 = reactions[inter[i]].first;
            int f2 = reactions[inter[i]].second;
            int val = fi[f1]<fi[f2]?fi[f1]:fi[f2]; 
            fi[f1]  -= val;
            fi[f2]  -= val;
            result += val*2;
//            registers[n2].erase(inter[i]);
        }
    }

    return result;
}

int main(int argc,char *argv[]) {
    int n,m,k;
    lli result = 0;
    scanf("%d %d %d",&n,&m,&k);
 
    fi.resize(n+1);
    for (int i=1;i<=n;i++) {
       int val;
       scanf("%d",&val);
       fi[i] = val;
    }

    seq.resize(m);
    for (int i=0;i<m;i++) {
       int f1,f2;
       scanf("%d %d",&f1,&f2);
       seq[i] = pair<int,int>(f1,f2);
    }

    for (int i=0;i<k;i++) {
       int s1,s2;
       scanf("%d %d",&s1,&s2);
       registers[s1].push_back(i);
       registers[s2].push_back(i);
       reactions.push_back(pair<int,int>(s1,s2));
    } 

    for (int i=0;i<m;i++) {
	result += addfs(seq[i].first,seq[i].second);
    }

    printf("%lld\n",result);
}