#ifndef LOCAL #include #else #include #endif // ―――――――――――――――――――――――――――――――― // abbreviation // ―――――――――――――――――――――――――――――――― using namespace std; using ll = long long; template using V = vector; template using VV = vector>; using vi = vector; using vl = vector; using vd = V; using vs = V; using vc = V; using vvi = vector>; using vvl = vector>; using vvc = vector>; using si = set; using mi = multiset; template using minpq = priority_queue, greater>; // ―――――――――――――――――――――――――――――――― // P // ―――――――――――――――――――――――――――――――― template struct P : pair { template P(Args... args) : pair(args...) { } using pair::first; using pair::second; P& operator+=(const P& r) { first += r.first; second += r.second; return *this; } P& operator-=(const P& r) { first -= r.first; second -= r.second; return *this; } P& operator*=(const P& r) { first *= r.first; second *= r.second; return *this; } template P& operator*=(const S& r) { first *= r, second *= r; return *this; } P operator+(const P& r) const { return P(*this) += r; } P operator-(const P& r) const { return P(*this) -= r; } P operator*(const P& r) const { return P(*this) *= r; } template P operator*(const S& r) const { return P(*this) *= r; } P operator-() const { return P{-first, -second}; } }; using pl = P; using pi = P; using vp = V; // ―――――――――――――――――――――――――――――――― // preprocessor // ―――――――――――――――――――――――――――――――― #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define call(v) (v).cbegin(), (v).cend() #define each(i, v) for (auto i : (v)) #define each2(x, y, v) for (auto [x, y] : (v)) #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repr(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--) #define repit(i, v) for (auto i = v.begin(); i < v.end(); i++) #define reprit(i, v) for (auto i = v.rbegin(); i < v.rend(); i++) #define reg(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++) #define regr(i, l, r) for (ll i = (ll)(r) - 1; i >= (ll)(l); i--) #define regit(i, l, r) for (auto i = (l); i != (r); i++) #define regrit(i, l, r) for (auto i = make_reverse_iterator((r)); i != make_reverse_iterator((l)); i++) #define REP(i, n) for (ll i = 0; i <= (ll)(n); i++) #define REPR(i, n) for (ll i = (ll)(n); i >= 0; i--) #define REG(i, l, r) for (ll i = (ll)(l); i <= (ll)(r); i++) #define REGR(i, l, r) for (ll i = (ll)(r); i >= (ll)(l); i--) #define Yes cout << "Yes" << el #define No cout << "No" << el #define YES cout << "YES" << el #define NO cout << "NO" << el #define fls cout << flush #define eps (1e-10) #define Equals(a, b) (fabs((a) - (b)) < eps) #define MAX(x) *max_element(all(x)) #define MIN(x) *min_element(all(x)) #define BIT(n) (1LL << (n)) #define between(l, k, r) (l <= k && k < r) #define ints(...) \ int __VA_ARGS__; \ input(__VA_ARGS__) #define pb push_back // #define fi first // #define se second // ―――――――――――――――――――――――――――――――― // constexpr // ―――――――――――――――――――――――――――――――― constexpr int INF = 0x3f3f3f3f; constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr string_view ascii_lowercase = "abcdefghijklmnopqrstuvwxyz"; constexpr string_view ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; constexpr char el = '\n'; constexpr char spa = ' '; // ―――――――――――――――――――――――――――――――― // yn, YN // ―――――――――――――――――――――――――――――――― bool yn(const bool b) { if (b) { Yes; } else { No; } return b; } bool YN(const bool b) { if (b) { YES; } else { NO; } return b; } // ―――――――――――――――――――――――――――――――― // chmax, chmin // ―――――――――――――――――――――――――――――――― template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } // ―――――――――――――――――――――――――――――――― // input // ―――――――――――――――――――――――――――――――― template void input(T& n) { cin >> n; } template void input(V& v, const int n) { T value; for (int i = 0; i < n; ++i) { input(value); v.push_back(value); } } template void input(VV& v, const int h, const int w) { V vec; for (int i = 0; i < h; i++) { vec.clear(); input(vec, w); v.push_back(vec); } } template void input(P& p) { cin >> p.first >> p.second; } template void input(T& first, Args&... args) { input(first); if constexpr (sizeof...(args) > 0) { input(args...); } } template void input(set& s, const int n) { for (int i = 0; i < n; ++i) { T value; input(value); s.insert(value); } } template void input(multiset& s, const int n) { for (int i = 0; i < n; ++i) { T value; input(value); s.insert(value); } } template void input(unordered_set& s, const int n) { for (int i = 0; i < n; ++i) { T value; input(value); s.insert(value); } } template void input(map& m, const int n) { for (int i = 0; i < n; ++i) { K k; T v; input(k, v); m.emplace(k, v); } } template void input(unordered_map& m, const int n) { for (int i = 0; i < n; ++i) { K k; T v; input(k, v); m.emplace(k, v); } } // ―――――――――――――――――――――――――――――――― // print // ―――――――――――――――――――――――――――――――― template void print(const T& value, bool endline = true) { cout << value; if (endline) { cout << el; } } template void print(const V& vec, bool endline = true) { for (size_t i = 0; i < vec.size(); ++i) { print(vec[i], false); if (i != vec.size() - 1) { cout << spa; } } if (endline) { cout << el; } } template void print(const P& p, bool endline = true) { cout << p.first << spa << p.second; if (endline) { cout << el; } } template void print_tuple(const tuple& tpl) { if constexpr (Index < sizeof...(T)) { print(get(tpl), false); if constexpr (Index < sizeof...(T) - 1) cout << spa; print_tuple(tpl); } } template void print(const tuple& tpl, bool endline = true) { print_tuple(tpl); if (endline) cout << el; } template void print(const T& first, Args&... args) { print(first, false); cout << spa; if constexpr (sizeof...(args) > 0) { print(args...); } } template ::iterator_category, input_iterator_tag> || is_same_v::iterator_category, output_iterator_tag> || is_same_v::iterator_category, forward_iterator_tag> || is_same_v::iterator_category, bidirectional_iterator_tag> || is_same_v::iterator_category, random_access_iterator_tag>>> void print(Iterator begin, Iterator end, bool endline = true) { for (Iterator it = begin; it != end; ++it) { print(*it, false); if (next(it) != end) { cout << spa; } } if (endline) { cout << el; } } // ―――――――――――――――――――――――――――――――― // debug // ―――――――――――――――――――――――――――――――― template void debug(const T& value, bool endline = true) { if constexpr (is_same_v) { if (value >= INF) { cerr << "inf"; } else if (value <= -INF) { cerr << "-inf"; } else { cerr << value; } } else if constexpr (is_same_v) { if (value >= LINF) { cerr << "inf"; } else if (value <= -LINF) { cerr << "-inf"; } else { cerr << value; } } else { cerr << value; } if (endline) { cerr << el; } } template void debug(const V& vec, bool endline = true) { for (size_t i = 0; i < vec.size(); ++i) { debug(vec[i], false); if (i != vec.size() - 1) { cerr << spa; } } if (endline) { cerr << el; } } template void debug(const VV& vec, bool endline = true) { for (size_t i = 0; i < vec.size(); ++i) { debug(vec[i], false); if (i != vec.size() - 1) { cerr << el; } } if (endline) { cerr << el; } } template void debug(const P& p, bool endline = true) { cerr << p.first << spa << p.second; if (endline) { cerr << el; } } template void debug_tuple(const tuple& tpl) { if constexpr (Index < sizeof...(T)) { debug(get(tpl), false); if constexpr (Index < sizeof...(T) - 1) cerr << spa; debug_tuple(tpl); } } template void debug(const tuple& tpl, bool endline = true) { debug_tuple(tpl); if (endline) cerr << el; } template void debug(const T& first, Args&... args) { debug(first, false); cerr << spa; if constexpr (sizeof...(args) > 0) { debug(args...); } } template auto sum(Iter begin, Iter end) { using T = typename iterator_traits::value_type; return accumulate(begin, end, T{}); } template ::iterator_category, input_iterator_tag> || is_same_v::iterator_category, output_iterator_tag> || is_same_v::iterator_category, forward_iterator_tag> || is_same_v::iterator_category, bidirectional_iterator_tag> || is_same_v::iterator_category, random_access_iterator_tag>>> void debug(Iterator begin, Iterator end, bool endline = true) { for (Iterator it = begin; it != end; ++it) { debug(*it, false); if (next(it) != end) { cerr << spa; } } if (endline) { cerr << el; } } // ―――――――――――――――――――――――――――――――― // mod_pow // ―――――――――――――――――――――――――――――――― template T mod_pow(T x, T n, const T& p) { T ret = 1; while (n > 0) { if (n & 1) (ret *= x) %= p; (x *= x) %= p; n >>= 1; } return ret; } namespace atcoder { namespace internal { template struct simple_queue { std::vector payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const T& t) { payload.push_back(t); } T& front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; } // namespace internal template struct mf_graph { public: mf_graph() : _n(0) { } explicit mf_graph(int n) : _n(n), g(n) { } int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap}); g[to].push_back(_edge{from, from_id, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector edges() { int m = int(pos.size()); std::vector result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto& _e = g[pos[i].first][pos[i].second]; auto& _re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector level(_n), iter(_n); internal::simple_queue que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } }; auto dfs = [&](auto self, int v, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int& i = iter[v]; i < int(g[v].size()); i++) { _edge& e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) return res; } level[v] = _n; return res; }; Cap flow = 0; while (flow < flow_limit) { bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } return flow; } std::vector min_cut(int s) { std::vector visited(_n); internal::simple_queue que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } private: int _n; struct _edge { int to, rev; Cap cap; }; std::vector> pos; std::vector> g; }; } // namespace atcoder using namespace atcoder; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(10) << boolalpha; cerr << fixed << setprecision(10) << boolalpha; ints(N); vl P; input(P, N); ints(M); vl U, V; rep(_, M) { input(U, 1); input(V, 1); } ints(K); ll ans = 0; each(p, P) if (p >= 0) ans += p; mf_graph G(N + K + 2); int S = N + K; int T = S + 1; rep(i, N) { ll p = P[i]; if (p >= 0) G.add_edge(i, T, p); else G.add_edge(S, i, -p); } rep(i, M) { ll u = U[i] - 1; ll v = V[i] - 1; G.add_edge(u, v, LINF); } int a, b; ll s; rep(i, K) { input(a, b, s); --a; --b; ans += s; G.add_edge(a, N + i, LINF); G.add_edge(b, N + i, LINF); G.add_edge(N + i, T, s); } ll F = G.flow(S, T); ans -= F; print(ans); // debug(F); // each(e, G.edges()) debug(e.from, e.to, e.cap, e.flow); return 0; }