#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long #define ii pair #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcountll #define ll long long #define mp make_pair #define x first #define y second #define Time (double)clock()/CLOCKS_PER_SEC #define munq(a) sort(all(a)); a.resize(unique(all(a))-a.begin()) #define sz(a) ((int)a.size()) #ifdef LOCAL #define debug(x) do { cout << #x << ": " << x << endl; } while(0) #define debug2(x, y) do { std::cerr << #x << ", " << #y << ": " << x << ", " << y << endl; } while (0) #define debug3(x, y, z) do {std::cerr << #x << ", " << #y << ", " << #z << ": " << x << ", " << y << ", " << z << endl; } while(0) #else #define debug(x) #define debug2(x, y) #define debug3(x, y, z) #endif #define FORI(i,a,b) for (int i = (a); i < (b); ++i) #define FOR(i,a) FORI(i,0,a) #define ROFI(i,a,b) for (int i = (b)-1; i >= (a); --i) #define ROF(i,a) ROFI(i,0,a) #define rep(a) FOR(_,a) #define each(a,x) for (auto& a: x) #define FORN(i, n) FORI(i, 1, n + 1) using vi = vector; template std::istream& operator >>(std::istream& input, std::pair & data) { input >> data.x >> data.y; return input; } template std::istream& operator >>(std::istream& input, std::vector& data) { for (T& x : data) input >> x; return input; } template std::ostream& operator <<(std::ostream& output, const pair & data) { output << "(" << data.x << "," << data.y << ")"; return output; } template std::ostream& operator <<(std::ostream& output, const std::vector& data) { for (const T& x : data) output << x << " "; return output; } ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down ll math_mod(ll a, ll b) { return a - b * div_down(a, b); } #define tcT template using V = vector; tcT> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } // set a = min(a,b) tcT> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } tcT> vector presum(vector &a) { vector p(a.size() + 1); FOR (i, a.size()) { p[i + 1] = p[i] + a[i]; } return p; } tcT> vector sufsum(vector &a) { vector p(a.size() + 1); for (int i = (int)a.size() - 1; i >= 0; --i) { p[i] = p[i + 1] + a[i]; } return p; } ll gcd(ll a, ll b) { while (b) { tie(a, b) = mp(b, a % b); } return a; } int Bit(int mask, int bit) { return (mask >> bit) & 1; } signed main() { #ifdef LOCAL #else #define endl '\n' ios_base::sync_with_stdio(0); cin.tie(0); #endif int k,n,m; cin >> k >> n >> m; vi a(k); cin >> a; each(e, a) { e--; } vi b(n); cin >> b; n += 2; V g(n); int s = n - 2, t = n - 1; struct edge { int next, capacity, cost, flow = 0; edge() = default; edge(int next, int capacity, int cost) : next(next), capacity(capacity), cost(cost) {} int rem() const { return capacity - flow; } int operator+=(int f) { return flow += f; } int operator-=(int f) { return flow -= f; } }; vector e; auto addEdge = [&](int from, int next, int capacity, int cost) { //debug2(from, next);debug2(capacity, cost); g[from].push_back(e.size()); e.emplace_back(next, capacity, cost); g[next].push_back(e.size()); e.emplace_back(from, 0, -cost); }; /* Если граф ориентированный, то addEdge вызываем один раз. Если неориентированный, то два, вот так: addEdge(u, v, capacity, cost); addEdge(v, u, capacity, cost); */ vector phi(n, 0); auto fordBellman = [&](int s, int t) { phi.assign(n, 0); for (int iter = 0; iter < n; ++iter) { bool changed = false; for (int u = 0; u < n; ++u) { for (auto index : g[u]) { auto edge = e[index]; if (edge.rem() > 0 && phi[edge.next] > phi[u] + edge.cost) { phi[edge.next] = phi[u] + edge.cost; changed = true; } } } if (!changed) break; } }; fordBellman(s, t); vector dist; vector from; vector cnt; auto dijkstra = [&](int s, int t) { dist.assign(n, 1e18); from.assign(n, -1); cnt.assign(n, false); dist[s] = 0; for (int i = 1; i < n; ++i) { int cur = find(cnt.begin(), cnt.end(), false) - cnt.begin(); for (int j = 0; j < n; ++j) { if (!cnt[j] && dist[j] < dist[cur]) cur = j; } cnt[cur] = true; for (int index : g[cur]) { auto &edge = e[index]; if (edge.rem() == 0) continue; ll weight = edge.cost + phi[cur] - phi[edge.next]; if (dist[edge.next] > dist[cur] + weight) { dist[edge.next] = dist[cur] + weight; from[edge.next] = cur; } } } if (dist[t] == (ll) 1e18) return -1LL; ll cost = 0; for (int p = t; p != s; p = from[p]) { for (auto index : g[from[p]]) { auto &edge = e[index]; ll weight = edge.cost + phi[from[p]] - phi[edge.next]; if (edge.rem() > 0 && edge.next == p && dist[edge.next] == dist[from[p]] + weight) { edge += 1; e[index ^ 1] -= 1; cost += edge.cost; break; } } } for (int i = 0; i < n; ++i) { phi[i] += dist[i]; } return cost; }; for (int i = 0; i < k; ++i) { addEdge(s, a[i], 1, 0); } for (int i = 0; i < n - 2; ++i) { addEdge(i, t, b[i], 0); } debug("ok"); for (int i = 0; i < m; ++i) { int u, v, cost; cin >> u >> v >> cost; u--; v--; addEdge(u, v, k, cost); addEdge(v, u, k, cost); } debug("ok"); ll cost = 0; for (int flow = 0; flow < k; ++flow) { ll a = dijkstra(s, t); if (a == -1) { cout << "-1\n"; return 0; } cost += a; } cout << cost << endl; }