#include using namespace std; typedef pair pii; typedef long long ll; const int N = 2000086, MOD = 998244353, INF = 0x3f3f3f3f; ll res; int n, m, cnt, w[N]; int e[N], ne[N], h[N], idx, v[N], S, T; int d[N], cur[N]; void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; e[idx] = a, ne[idx] = h[b], w[idx] = 0, h[b] = idx++; } inline bool bfs() { queue q; memset(d, -1, sizeof(int) * (cnt + 10)); memcpy(cur, h, sizeof(int) * (cnt + 10)); d[S] = 0; q.push(S); while (q.size()) { auto u = q.front(); q.pop(); for (int i = h[u]; ~i; i = ne[i]) { int j = e[i]; if (w[i] && d[j] == -1) { d[j] = d[u] + 1; if (j == T) return true; q.push(j); } } } return false; } inline ll dfs(int r, int limit) { if (r == T) return limit; ll res = 0; for (int i = cur[r]; ~i && res < limit; i = ne[i]) { cur[r] = i; int j = e[i]; if (d[j] == d[r] + 1 && w[i]) { ll t = dfs(j, min(limit - res, (ll)w[i])); if (!t) d[j] = -1; w[i] -= t, w[i ^ 1] += t, res += t; } } return res; } inline ll dinic() { ll res = 0, t; while (bfs()) while (t = dfs(S, INF)) res += t; return res; } int main() { memset(h, -1, sizeof h); cin >> n; ll sum = 0; S = 0, T = n + 1; for (int i = 1; i < n + 1; i++) { scanf("%d", v + i); if (v[i] < 0) add(i, T, -v[i]); else add(0, i, v[i]), sum += v[i]; } cnt = n + 1; cin >> m; for (int i = 1; i < m + 1; i++) { int a, b; scanf("%d%d", &a, &b); add(b, a, INF); } cin >> m; while (m--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); sum += c; ++cnt; add(0, cnt, c); add(cnt, a, INF); add(cnt, b, INF); } printf("%lld\n", sum - dinic()); return 0; }