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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;

/**
 *
 * @author Marcin Lewandowski
 */
public class fio {
    private static HashMap<Reaction,Reaction>reactions;
    private static long sediment = 0;
    private static InputStream in = System.in;
    private static final Comparator<Reaction> REACTIONS_COMPARATOR = new Comparator<Reaction>() {
        @Override
        public int compare(Reaction o1, Reaction o2) {
            return o1.prio-o2.prio;
        }
    };

    public static void main(String[] args) throws IOException {
        int n = readInt();
        int m = readInt();
        int k = readInt();
        Vial[] viales = new Vial[n+1];
        int[] ints = readInts(n);
        for(int i = 1 ; i<n+1 ; i++){
            Substance s = new Substance(i, ints[i-1]);
            viales[i] = new Vial(s);
        }

        int[][] steps = new int[m][2];
        ints=readInts(2*m);
        for(int i = 0 ; i < m ; i++){
//            steps[i][0] = readInt();
//            steps[i][1] = readInt();
            steps[i][0] = ints[2*i];
            steps[i][1] = ints[2*i+1];
        }
        reactions = new HashMap<Reaction,Reaction>(k);
        ints=readInts(2*k);
        for(int i = 0 ; i < k ; i++){
            int ingreedient1 = ints[2*i];//readInt();
            int ingreedient2 = ints[2*i+1];//readInt();
            Reaction reaction = new Reaction(ingreedient1, ingreedient2, i);
            reactions.put(reaction,reaction);
            Reaction reaction2 = new Reaction(ingreedient2, ingreedient1, i);
            reactions.put(reaction2,reaction);
        }
        for(int i =0; i< m; i++){
            mixVials(viales[steps[i][0]],viales[steps[i][1]]);
        }
        System.out.println(sediment);
    }
    
    private static void mixVials(Vial from, Vial to){
        ArrayList<Reaction> reactionsToPerform = new ArrayList<Reaction>();
        Reaction possibleReaction = new Reaction(0, 0, 0);
        for(Substance s1 : from.substances.values()){
            for(Substance s2: to.substances.values()){
                possibleReaction.ingreedient1=s1.number;
                possibleReaction.ingreedient2=s2.number;
                Reaction toAdd = reactions.get(possibleReaction);
                if(toAdd!=null){
                    reactionsToPerform.add(toAdd);
                }
            }
        }
        Collections.sort(reactionsToPerform, REACTIONS_COMPARATOR);
        for(Reaction reaction : reactionsToPerform){
            Substance s1 = from.substances.get(reaction.ingreedient1);
            Substance s2 = to.substances.get(reaction.ingreedient2);
            if(s1==null){
                s1 = to.substances.get(reaction.ingreedient1);
                s2 = from.substances.get(reaction.ingreedient2);
            }
            int min = Math.min(s1.quantity, s2.quantity);
            if(min!=0){
                s1.quantity-=min;
                s2.quantity-=min;
                sediment+=2*min;
            }
        }
        if(!reactionsToPerform.isEmpty()){
            HashSet<Integer> toRemove = new HashSet<Integer>();
            for(Entry<Integer,Substance> entry : to.substances.entrySet()){
                if(entry.getValue().quantity==0){
                    toRemove.add(entry.getKey());
                }
            }
            for(Integer key : toRemove){
                to.substances.remove(key);
            }
        }
        for(Entry<Integer,Substance> entry : from.substances.entrySet()){
            if(entry.getValue().quantity>0){
                to.substances.put(entry.getKey(), entry.getValue());
            }
        }

    }

    private static int readInt() throws IOException {
        int result = 0;
        boolean isDigit = false;
        int c = 0;
        while ((c = in.read()) != -1) {
            if (c >= '0' && c <= '9') {
                isDigit = true;
                result = result * 10 + c - '0';
            } else if (isDigit) {
                break;
            }
        }
        return result;
    }
    private static int[] readInts(int size) throws IOException {
        int digit = 0;
        int[] result = new int[size];
        int counter=0;
        boolean isDigit = false;
        int c = 0;
        while ((c = in.read()) != -1) {
            if (c >= '0' && c <= '9') {
                isDigit = true;
                digit = digit * 10 + c - '0';
            } else if (isDigit) {
                result[counter]=digit;
                counter++;
                isDigit = false;
                digit=0;
                if(counter==size){
                    break;
                }
            }
        }
        return result;
    }
    
    private static class Substance{
        int number;
        int quantity;

        public Substance(int number, int quantity) {
            this.number = number;
            this.quantity = quantity;
        }
        
    }
    
    private static class Vial{
        
        public Vial(Substance initialSubstance){
            substances = new HashMap<Integer, Substance>();
            substances.put(initialSubstance.number, initialSubstance);
        }
        private HashMap<Integer,Substance> substances;
    }
    
    
    private static class Reaction{
        int ingreedient1;
        int ingreedient2;
        int prio;

        public Reaction(int ingreedient1, int ingreedient2, int prio) {
            this.ingreedient1 = ingreedient1;
            this.ingreedient2 = ingreedient2;
            this.prio = prio;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Reaction other = (Reaction) obj;
            if (this.ingreedient1 != other.ingreedient1) {
                return false;
            }
            if (this.ingreedient2 != other.ingreedient2) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 3;
            hash = 29 * hash + this.ingreedient1;
            hash = 29 * hash + this.ingreedient2;
            return hash;
        }
    }
}