Cheapest cables to connect every site
There are n sites numbered 0 to n - 1 and a list links of cables you could lay, each [a, b, cost]. Pick cables so that every site can reach every other one, directly or through other sites, at the smallest total cost. Return that total, or -1 if even all the cables together leave some site cut off.
Costs are non-negative integers. A pair may appear several times with different costs, and a cable may join a site to itself. n ≥ 1.
Besides the tests, one large input (1,600 sites, 4,799 cables) with a ~200 ms limit (scaled to your device's speed): brute force won't pass.
Examples
cheapest_network(4, [[0,1,3],[1,2,1],[2,3,4],[0,3,2],[0,2,5]])→6cheapest_network(3, [[0,1,2]])→-1cheapest_network(1, [])→0