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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

// copied and modified from:
// http://www.topcoder.com/tc?d1=tutorials&d2=lowestCommonAncestor&module=Static#Another easy solution in O(N logN, O(logN)
#define MAXN 200009
#define LOGMAXN 20

int T[MAXN];
int P[MAXN][LOGMAXN];
int L[MAXN];

vector<PII> flows[MAXN];
VI children[MAXN];
int amount[MAXN];
int order[MAXN];

int N, M, K;

void dfs(int node, int level = 0) {
  vector<PII> dfsStack;
  dfsStack.PB(MP(node, level));
  while (!dfsStack.empty()) {
    node = dfsStack.back().first;
    level = dfsStack.back().second;
    dfsStack.pop_back();
    L[node]=level;

    FOREACH(it, children[node]) {
      dfsStack.PB(MP(*it, level+1));
    }
  }
}

void process3() {
  int i, j;

  //we initialize every element in P with -1
  for (i = 0; i < N; i++)
    for (j = 0; 1 << j < N; j++)
      P[i][j] = -1;

  //the first ancestor of every node i is T[i]
  for (i = 0; i < N; i++)
    P[i][0] = T[i];

  //bottom up dynamic programing
  for (j = 1; 1 << j < N; j++)
    for (i = 0; i < N; i++)
      if (P[i][j - 1] != -1)
        P[i][j] = P[P[i][j - 1]][j - 1];
}

int query(int p, int q) {
  int log, i;

  //if p is situated on a higher level than q then we swap them
  if (L[p] < L[q])
    swap(p,q);

  //we compute the value of [log(L[p)]
  for (log = 1; 1 << log <= L[p]; log++);
  log--;

  if (L[p] > L[q]) {
    //we find the ancestor of node p situated on the same level
    //with q using the values in P
    int qOneLevelBelow = L[q]+1;
    for (i = log; i >= 0; i--)
      if (L[p] - (1 << i) >= qOneLevelBelow)
        p = P[p][i];
    if (T[p] == q) {
      return order[p];
    } else {
      p = T[p];
    }
  }

  //we compute LCA(p, q) using the values in P
  for (i = log; i >= 0; i--)
    if (P[p][i] != -1 && P[p][i] != P[q][i]) {
      p = P[p][i];
      q = P[q][i];
    }

  return max(order[p], order[q]);
}

int main() {
  scanf("%d%d%d", &N, &M, &K);

  REP(i,N) {
    scanf("%d", &amount[i]);
    T[i]=-1;
  }

  REP(i,M) {
    int from, to;
    scanf("%d%d", &from, &to);
    from--;
    to--;
    children[to].PB(from);
    T[from]=to;
    order[from]=i;
  }


  int fakeRoot = N;
  T[fakeRoot]=-1;
  int fakeOrder = M;
  order[fakeRoot] = fakeOrder++;
  amount[fakeRoot] = 0;
  REP(i,N) {
    if(T[i] != -1) continue;
    children[fakeRoot].PB(i);
    T[i]=fakeRoot;
    order[i]=fakeOrder++;
  }
  ++N;

  dfs(fakeRoot);

  process3();
  
  REP(i,K) {
    int c, d;
    scanf("%d%d", &c, &d);
    c--;
    d--;
    flows[query(c,d)].PB(MP(c,d));
  }

  LL res = 0;
  REP(i,M) {
    FOREACH(pit, flows[i]) {
      int c = pit->first;
      int d = pit->second;
      int mincd = min(amount[c], amount[d]);
      res += 2*mincd;
      amount[c] -= mincd;
      amount[d] -= mincd;
    }
  }
  printf("%lld\n", res);
  return 0;
}