/* #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") //*/ #include // #include #include using namespace std; // using namespace atcoder; // #define _GLIBCXX_DEBUG #define DEBUG(x) cerr << #x << ": " << x << endl; #define DEBUG_VEC(v) \ cerr << #v << ":"; \ for (int iiiiiiii = 0; iiiiiiii < v.size(); iiiiiiii++) \ cerr << " " << v[iiiiiiii]; \ cerr << endl; #define DEBUG_MAT(v) \ cerr << #v << endl; \ for (int iv = 0; iv < v.size(); iv++) { \ for (int jv = 0; jv < v[iv].size(); jv++) { \ cerr << v[iv][jv] << " "; \ } \ cerr << endl; \ } typedef long long ll; // #define int ll #define vi vector #define vl vector #define vii vector> #define vll vector> #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream &operator<<(ostream &os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rrep1(i, n) for (int i = (int)(n); i > 0; i--) #define REP(i, a, b) for (int i = a; i < b; i++) #define in(x, a, b) (a <= x && x < b) #define all(c) c.begin(), c.end() void YES(bool t = true) { cout << (t ? "YES" : "NO") << endl; } void Yes(bool t = true) { cout << (t ? "Yes" : "No") << endl; } void yes(bool t = true) { cout << (t ? "yes" : "no") << endl; } void NO(bool t = true) { cout << (t ? "NO" : "YES") << endl; } void No(bool t = true) { cout << (t ? "No" : "Yes") << endl; } void no(bool t = true) { cout << (t ? "no" : "yes") << endl; } template bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; int popcount(ll t) { return __builtin_popcountll(t); } // int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; // int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 }; vi dx = {0, 0, -1, 1}, dy = {-1, 1, 0, 0}; vi dx2 = {1, 1, 0, -1, -1, -1, 0, 1}, dy2 = {0, 1, 1, 1, 0, -1, -1, -1}; struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(25); cerr << fixed << setprecision(25); } } setup_io; // const ll MOD = 1000000007; const ll MOD = 998244353; // #define mp make_pair // #define endl '\n' #include #include #include #ifdef _MSC_VER #include #endif #include #ifdef _MSC_VER #include #endif namespace atcoder { namespace internal { constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } struct barrett { unsigned int _m; unsigned long long im; explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} unsigned int umod() const { return _m; } unsigned int mul(unsigned int a, unsigned int b) const { unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; constexpr long long bases[3] = {2, 7, 61}; for (long long a : bases) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template constexpr bool is_prime = is_prime_constexpr(n); constexpr std::pair inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return {s, m0}; } constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template constexpr int primitive_root = primitive_root_constexpr(m); unsigned long long floor_sum_unsigned(unsigned long long n, unsigned long long m, unsigned long long a, unsigned long long b) { unsigned long long ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } unsigned long long y_max = a * n + b; if (y_max < m) break; n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } } // namespace internal } // namespace atcoder #include #include #include namespace atcoder { namespace internal { #ifndef _MSC_VER template using is_signed_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using is_integral = typename std::conditional::value || is_signed_int128::value || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed_int = typename std::conditional<(is_integral::value && std::is_signed::value) || is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional<(is_integral::value && std::is_unsigned::value) || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional< is_signed_int128::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; #else template using is_integral = typename std::is_integral; template using is_signed_int = typename std::conditional::value && std::is_signed::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional::value && std::is_unsigned::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional::value, std::make_unsigned, std::common_type>::type; #endif template using is_signed_int_t = std::enable_if_t::value>; template using is_unsigned_int_t = std::enable_if_t::value>; template using to_unsigned_t = typename to_unsigned::type; } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template using is_modint = std::is_base_of; template using is_modint_t = std::enable_if_t::value>; } // namespace internal template * = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template * = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template * = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint &operator*=(const mint &rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime; }; template struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template * = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template * = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator*=(const mint &rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template internal::barrett dynamic_modint::bt(998244353); using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template using is_static_modint = std::is_base_of; template using is_static_modint_t = std::enable_if_t::value>; template struct is_dynamic_modint : public std::false_type {}; template struct is_dynamic_modint> : public std::true_type {}; template using is_dynamic_modint_t = std::enable_if_t::value>; } // namespace internal } // namespace atcoder using namespace atcoder; using mint = modint998244353; constexpr int N = 2011; vector> G(N); vl dijkstra(int s, int n, vector>> G) { priority_queue, greater> pq; int i; vl d(n, INF); d[s] = 0; pq.push(make_pair(0, s)); while (!pq.empty()) { pll f = pq.top(); pq.pop(); int u = f.second; if (d[u] < f.first) { continue; } for (i = 0; i < G[u].size(); i++) { int v = G[u][i].second; if (d[v] > d[u] + G[u][i].first) { d[v] = d[u] + G[u][i].first; pq.push(make_pair(d[v], v)); } } } return d; } // Minimum cost flow WITH NO NEGATIVE CYCLE (just negative cost edge is allowed) // Verified: // - SRM 770 Div1 Medium https://community.topcoder.com/stat?c=problem_statement&pm=15702 // - CodeChef LTIME98 Ancient Magic https://www.codechef.com/problems/ANCT template ::max() / 2> struct MinCostFlow { // https://hitonanode.github.io/cplib-cpp/combinatorial_opt/mincostflow_nonegativeloop.hpp struct _edge { int to, rev; Cap cap; Cost cost; template friend Ostream &operator<<(Ostream &os, const _edge &e) { return os << '(' << e.to << ',' << e.rev << ',' << e.cap << ',' << e.cost << ')'; } }; bool _is_dual_infeasible; int V; std::vector> g; std::vector dist; std::vector prevv, preve; std::vector dual; // dual[V]: potential std::vector> pos; bool _initialize_dual_dag() { std::vector deg_in(V); for (int i = 0; i < V; i++) { for (const auto &e : g[i]) deg_in[e.to] += (e.cap > 0); } std::vector st; st.reserve(V); for (int i = 0; i < V; i++) { if (!deg_in[i]) st.push_back(i); } for (int n = 0; n < V; n++) { if (int(st.size()) == n) return false; // Not DAG int now = st[n]; for (const auto &e : g[now]) { if (!e.cap) continue; deg_in[e.to]--; if (deg_in[e.to] == 0) st.push_back(e.to); if (dual[e.to] >= dual[now] + e.cost) dual[e.to] = dual[now] + e.cost; } } return true; } bool _initialize_dual_spfa() { // Find feasible dual's when negative cost edges exist dual.assign(V, 0); std::queue q; std::vector in_queue(V); std::vector nvis(V); for (int i = 0; i < V; i++) q.push(i), in_queue[i] = true; while (q.size()) { int now = q.front(); q.pop(), in_queue[now] = false; if (nvis[now] > V) return false; // Negative cycle exists nvis[now]++; for (const auto &e : g[now]) { if (!e.cap) continue; if (dual[e.to] > dual[now] + e.cost) { dual[e.to] = dual[now] + e.cost; if (!in_queue[e.to]) in_queue[e.to] = true, q.push(e.to); } } } return true; } bool initialize_dual() { return !_is_dual_infeasible or _initialize_dual_dag() or _initialize_dual_spfa(); } void _dijkstra(int s) { // O(ElogV) prevv.assign(V, -1); preve.assign(V, -1); dist.assign(V, INF_COST); dist[s] = 0; using P = std::pair; std::priority_queue, std::greater

> q; q.emplace(0, s); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < (int)g[v].size(); i++) { _edge &e = g[v][i]; Cost c = dist[v] + e.cost + dual[v] - dual[e.to]; if (e.cap > 0 and dist[e.to] > c) { dist[e.to] = c, prevv[e.to] = v, preve[e.to] = i; q.emplace(dist[e.to], e.to); } } } } MinCostFlow(int V = 0) : _is_dual_infeasible(false), V(V), g(V), dual(V, 0) { static_assert(INF_COST > 0, "INF_COST must be positive"); } int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from and from < V); assert(0 <= to and to < V); assert(cap >= 0); if (cost < 0) _is_dual_infeasible = true; pos.emplace_back(from, g[from].size()); g[from].push_back({to, (int)g[to].size() + (from == to), cap, cost}); g[to].push_back({from, (int)g[from].size() - 1, (Cap)0, -cost}); return int(pos.size()) - 1; } // Flush flow f from s to t. Graph must not have negative cycle. std::pair flow(int s, int t, const Cap &flow_limit) { if (!initialize_dual()) throw; // Fail to find feasible dual Cost cost = 0; Cap flow_rem = flow_limit; while (flow_rem > 0) { _dijkstra(s); if (dist[t] == INF_COST) break; for (int v = 0; v < V; v++) dual[v] = std::min(dual[v] + dist[v], INF_COST); Cap d = flow_rem; for (int v = t; v != s; v = prevv[v]) d = std::min(d, g[prevv[v]][preve[v]].cap); flow_rem -= d; cost += d * (dual[t] - dual[s]); for (int v = t; v != s; v = prevv[v]) { _edge &e = g[prevv[v]][preve[v]]; e.cap -= d; g[v][e.rev].cap += d; } } return std::make_pair(flow_limit - flow_rem, cost); } struct edge { int from, to; Cap cap, flow; Cost cost; template friend Ostream &operator<<(Ostream &os, const edge &e) { return os << '(' << e.from << "->" << e.to << ',' << e.flow << '/' << e.cap << ",$" << e.cost << ')'; } }; edge get_edge(int edge_id) const { int m = int(pos.size()); assert(0 <= edge_id and edge_id < m); auto _e = g[pos[edge_id].first][pos[edge_id].second]; auto _re = g[_e.to][_e.rev]; return {pos[edge_id].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost}; } std::vector edges() const { std::vector ret(pos.size()); for (int i = 0; i < int(pos.size()); i++) ret[i] = get_edge(i); return ret; } template friend Ostream &operator<<(Ostream &os, const MinCostFlow &mcf) { os << "[MinCostFlow]V=" << mcf.V << ":"; for (int i = 0; i < mcf.V; i++) { for (auto &e : mcf.g[i]) os << "\n" << i << "->" << e.to << ":cap" << e.cap << ",$" << e.cost; } return os; } }; signed main() { int k, n, m; cin >> k >> n >> m; vi num(n); rep(i, k) { int x; cin >> x; x--; num[x]++; } vi b(n); rep(i, n) { cin >> b[i]; } // MinCostFlow mcf(n + n + 2); atcoder::mcf_graph mcf(n + 2); rep(i, m) { ll u, v, d; cin >> u >> v >> d; u--; v--; G[u].emplace_back(d, v); G[v].emplace_back(d, u); mcf.add_edge(u, v, INF, d); mcf.add_edge(v, u, INF, d); } // vll d(n); // rep(i, n) { // d[i] = dijkstra(i, n, G); // } int S = n, T = n + 1; rep(i, n) { mcf.add_edge(S, i, num[i], 0); mcf.add_edge(i, T, b[i], 0); } // rep(i, n) { // rep(j, n) { // mcf.add_edge(i, n + j, INF, d[i][j]); // } // } auto [cap, cost] = mcf.flow(S, T, k); assert(cap == k); cout << cost << endl; }