#include using namespace std; #define overload3(_NULL, _1, _2, name, ...) name #define rep1(i, n) for (remove_const_t> i = 0; i < (n); i++) #define rep2(i, a, b) for (remove_const_t> i = a; i < (b); i++) #define rep(...) overload3(__VA_ARGS__, rep2, rep1)(__VA_ARGS__) #if __has_include() # include #else # define dbg(...) (void(0)) #endif template void drop(const T &x) { cout << x << "\n"; exit(0); } template bool chmax(T &a, const T &b) { return a < b and (a = b, true); } template bool chmin(T &a, const T &b) { return a > b and (a = b, true); } using i64 = long long; using usize = size_t; using size_type = usize; /** * @brief Floyd Warshall Algorithm / 負辺を許す全点対最短経路問題 * @docs docs/floyd_warshall.md */ template vector> floyd_warshall(vector>> const &graph) { const size_type n = size(graph); constexpr T INF = numeric_limits::max(); vector> dp(n, vector(n, INF)); rep(i, n) { dp[i][i] = 0; for (const auto [to, cost]: graph[i]) dp[i][to] = cost; } rep(k, n) rep(i, n) rep(j, n) if (dp[i][k] < INF and dp[k][j] < INF) chmin(dp[i][j], dp[i][k] + dp[k][j]); rep(i, n) if (dp[i][i] < 0) return {}; return dp; } /** * @brief Union Find * @docs docs/union_find.md */ struct UnionFind { size_type n; vector node; explicit UnionFind(const size_type n): n(n), node(n, -1) {} bool unite(size_type u, size_type v) { u = root(u), v = root(v); if (u == v) return false; if (node[u] > node[v]) swap(u, v); node[u] += node[v]; node[v] = (int) u; return true; } size_type root(size_type v) { return node[v] < 0 ? v : node[v] = (int) root(node[v]); } size_type size(size_type v) { return (size_type) -node[root(v)]; } bool same(size_type u, size_type v) { return root(u) == root(v); } vector> group() { vector ret(n, vector{}); rep(i, n) ret[root(i)].push_back(i); ret.erase(remove_if(begin(ret), end(ret), [&](const auto &ri) { return empty(ri); })); return ret; } }; /** * @brief Kruskal's Algorithm */ template vector> kruskal(const usize n, const usize k, vector> const &edges) { using Edge = tuple; UnionFind uf(n); vector ret{}; for (const auto [cost, u, v]: edges) { if (not uf.same(u, v)) { ret.emplace_back(Edge{ cost, u, v }); uf.unite(u, v); } if (size(ret) + 1 == k) break; } return ret; } int main() { usize n, m, k; cin >> n >> m >> k; vector a(n); for (auto &&ai: a) cin >> ai; vector graph(n, vector>{}); rep(_, m) { usize x, y; i64 z; cin >> x >> y >> z; graph[--x].emplace_back(--y, z); graph[y].emplace_back(x, z); } const auto dist = floyd_warshall(graph); using bit_t = int; constexpr i64 INF = numeric_limits::max(); i64 ans = INF; for (bit_t x = (1 << k) - 1; x < (1 << n);) { i64 cost = 0; rep(i, n) if (x & (1 << i)) cost += a[i]; using edge_t = tuple; vector sub_graph{}; rep(i, n) if (x & (1 << i)) { rep(j, n) if (x & (1 << j) and dist[i][j] != INF) sub_graph.emplace_back(dist[i][j], i, j); } sort(begin(sub_graph), end(sub_graph)); const auto mst = kruskal(n, k, sub_graph); cost += accumulate(begin(mst), end(mst), 0ll, [](i64 acc, auto e) { return acc + get<0>(e); }); chmin(ans, cost); bit_t t = x | (x - 1); x = (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(x) + 1)); } cout << ans << "\n"; } struct IOSetup { IOSetup() noexcept { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup;