#include "bits/stdc++.h" using namespace std; #define all(x) begin(x),end(x) template ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } template::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #define debug(a) cerr << "(" << #a << ": " << a << ")\n"; typedef long long ll; typedef vector vi; typedef vector vvi; typedef pair pi; const int mxN = 1e4+1; const ll oo = 3e18; struct mincostflow { // Source is Node 0; // Sink is node n-1 struct edge { ll f,c; int to; ll cost; }; typedef vector ve; vector> adj; vector edges; int n; mincostflow(int _n) { n=_n; adj.resize(n); dist.resize(n); parent.resize(n); pflow.resize(n); } void addEdge(int a, int b, ll w, ll cost, bool directed = true) { adj[a].push_back((int)edges.size()); adj[b].push_back((int)edges.size()+1); edges.push_back({0,w,b,cost}); edges.push_back({0,directed?0:w,a,-cost}); } bitset inqueue; vector dist, pflow; vi parent; void SPFA() { fill(all(dist),oo); queue q; q.push(0); parent[0] = -1; dist[0] = 0; inqueue[0]= true; pflow[0] = oo; while(!q.empty()) { int at = q.front(); q.pop(); inqueue[at] = false; for(int ei: adj[at]) { auto& e = edges[ei]; if(e.c-e.f<1) continue; ll constraint = min(e.c-e.f,pflow[at]); if(dist[at]+e.cost < dist[e.to]) { parent[e.to] = ei; pflow[e.to] = constraint; dist[e.to] = dist[at]+e.cost; if(!inqueue[e.to]) { inqueue[e.to] = true; q.push(e.to); } } else if(dist[at]+e.cost==dist[e.to]) { if(pflow[e.to]< constraint) { parent[e.to] = ei; pflow[e.to] = constraint; } } } } } void findPath(ll extra) { int at = n-1; while(at!=0) { int ei = parent[at]; auto& e = edges[ei]; e.f+=extra; auto& o = edges[ei^1]; o.f-=extra; at = o.to; } } pair solve(int k) { ll flow = 0, cost = 0; while(true) { SPFA(); if(dist[n-1]==oo) { break; } int extraflow = pflow[n-1]; ll shortestpath = dist[n-1]; // debug(shortestpath); // debug(extraflow); if(flow+extraflow>=k) { cost+=shortestpath*(k-flow); flow = k; break; } flow+=extraflow; cost+=shortestpath*extraflow; findPath(pflow[n-1]); } return {flow,cost}; } }; int main() { int k,n,m; cin >> k >> n >> m; mincostflow mcf(n+2); for(int i=0;i> a; mcf.addEdge(0,a,1,0); } for(int i=1;i<=n;++i) { int b; cin >> b; if(b) { mcf.addEdge(i,n+1,b,0); } } for(int i=0;i> u >> v >> w; mcf.addEdge(u,v,1e9,w); mcf.addEdge(v,u,1e9,w); } auto [flow,res] = mcf.solve(1e9); cout << res << '\n'; }