#include using namespace std; using ll = long long; using pll = pair; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; struct union_find{ vector par; vector siz; union_find(int n) : par(n), siz(n, 1){ for(int i=0; i struct edge{ int from; int to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} void reverse(){swap(from, to);} }; template struct edges : std::vector>{ void sort(){ std::sort( (*this).begin(), (*this).end(), [](const edge& a, const edge& b){ return a.cost < b.cost; } ); } }; template struct graph : std::vector>{ private: int n = 0; int m = 0; edges es; bool dir; public: graph(int n, bool dir) : n(n), dir(dir){ (*this).resize(n); } void add_edge(int from, int to, T cost=1){ if(dir){ es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m++)); }else{ if(from > to) swap(from, to); es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m)); (*this)[to].push_back(edge(to, from, cost, m++)); } } int get_vnum(){ return n; } int get_enum(){ return m; } bool get_dir(){ return dir; } edge get_edge(int i){ return es[i]; } edges get_edge_set(){ return es; } }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using weighted_graph = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; template struct dinic{ int n; residual_graph graph; dinic(residual_graph &graph_){ n = (int)graph_.size(); graph.resize(n); for(int from=0; from e : graph_[from]){ graph[from].push_back(redge(e.to, e.cap, e.cost, (int)graph[e.to].size())); graph[e.to].push_back(redge(from, 0, e.cost, (int)graph[from].size()-1)); } } } T max_flow(int s, int t){ residual_graph rgraph(n); vector level(n); vector iter(n); for(int from=0; from e : graph[from]) rgraph[from].push_back(e); function bfs = [&](){ for(int v=0; v Q; level[s] = 0; Q.push(s); while(!Q.empty()){ int v = Q.front(); Q.pop(); for(redge e : rgraph[v]){ if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; Q.push(e.to); } } } }; function dfs = [&](int v, T f){ if(v == t) return f; for(int &i=iter[v]; i<(int)rgraph[v].size(); i++){ redge &e = rgraph[v][i]; if(e.cap > 0 && level[v] < level[e.to]){ T d = dfs(e.to, min(f, e.cap)); if(d > 0){ e.cap -= d; rgraph[e.to][e.rev].cap += d; return d; } } } return (T)0; }; T flow = 0; for(;;){ bfs(); if(level[t] < 0) return flow; for(int v=0; v 0) flow += f; } } }; void solve(){ ll ans = 0LL; int N; cin >> N; vector P(N); for(int i=0; i> P[i]; if(0 < P[i]) ans += P[i]; } int M; cin >> M; vector U(M), V(M); for(int i=0; i> U[i] >> V[i]; U[i]--; V[i]--; } int K; cin >> K; vector A(K), B(K); vector S(K); for(int i=0; i> A[i] >> B[i] >> S[i]; A[i]--; B[i]--; ans += S[i]; } residual_graph G(N+K+2); int s = N+K, t = N+K+1; for(int i=0; i(t, P[i])); else G[s].pb(redge(i, -P[i])); } for(int i=0; i(V[i], LINF)); for(int i=0; i(N+i, 0LL)); G[A[i]].pb(redge(N+i, LINF)); G[B[i]].pb(redge(N+i, LINF)); G[N+i].pb(redge(t, S[i])); } dinic dc(G); ans -= dc.max_flow(s, t); cout << ans << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }