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
#include <stdio.h>
#include <stdlib.h>

typedef struct _Tfio {
        int id;
        long long g;
        struct _Tfio *next;
        struct _Tfio *prev;
} Tfio;

typedef struct _Tdup {
        int a,b;
        struct _Tdup *prev;
        struct _Tdup *next;
} Tdup;

int cmpInt(const void *a, const void *b) {
        return *(int *)a - *(int*)b;
}

int *inTree;
Tfio **fioCache;

long long reduce(Tdup **mapH, int treeId) {
        long long ret = 0, m;
        Tfio *a, *b;
        Tdup *map;
        map = *mapH;

        while (map) {
                if (inTree[map->a] == treeId && inTree[map->b] == treeId) {
                        a = fioCache[map->a];
                        b = fioCache[map->b];
                        m = a->g < b->g ? a->g : b->g;
                        a->g -= m;
                        b->g -= m;
                        ret += m;
                        if (map == *mapH) {
                                *mapH = map->next;
                        } else {
                                map->prev->next = map->next;
                        }
                }
                map = map->next;
        }

        return ret;
}

int main() {
        int n,m,k,i;
        long long int ret;

        Tfio **f, **fT;
        Tdup *seqH, *mapH, *seqT, *mapT, *tmp;
        seqH = mapH = seqT = mapT = NULL;

        scanf("%d%d%d", &n, &m, &k);

        f = malloc(n*sizeof(*f));
        fT = malloc(n*sizeof(*fT));
        inTree = malloc(n*sizeof(*inTree));
        fioCache = malloc(n*sizeof(*fioCache));
        for (i=0; i<n; ++i) {
                f[i] = malloc(sizeof(*f[i]));
                f[i]->id = i;
                f[i]->next = f[i]->prev = NULL;
                scanf("%lld", &(f[i]->g));
                inTree[i] = i;
                fioCache[i] = f[i];
                fT[i] = f[i];
        }

        for (i=0; i<m; ++i) {
                tmp = malloc(sizeof(*tmp));
                scanf("%d%d", &(tmp->a), &(tmp->b));
                tmp->a--;
                tmp->b--;
                tmp->next = NULL;
                if (seqH == NULL) {
                        seqH = seqT = tmp;
                        tmp->prev = NULL;
                } else {
                        tmp->prev = seqT;
                        seqT->next = tmp;
                        seqT = tmp;
                }
        }

        for (i=0; i<k; ++i) {
                tmp = malloc(sizeof(*tmp));
                scanf("%d%d", &(tmp->a), &(tmp->b));
                tmp->a--;
                tmp->b--;
                tmp->next = NULL;
                if (mapH == NULL) {
                        mapH = mapT = tmp;
                        tmp->prev = NULL;
                } else {
                        tmp->prev = mapT;
                        mapT->next = tmp;
                        mapT = tmp;
                }
        }

        ret = 0;
        i = 0;
        while (seqH) {
//              if (i % 1000 == 0) printf("%d/%d\n", i, m);
                i++;
                fT[seqH->b]->next = f[seqH->a];
                f[seqH->a]->prev = fT[seqH->b];
                fT[seqH->b] = fT[seqH->a];
                while (f[seqH->a]) {
                        inTree[f[seqH->a]->id] = seqH->b;
                        f[seqH->a] = f[seqH->a]->next;
                }
                ret += reduce(&mapH, seqH->b);
                seqH = seqH->next;
        }
        printf("%lld\n", 2LL * ret);
        free(f);
        free(inTree);
        free(fioCache);
        return 0;
}