Railway Connection
Time limit1sMemory limit1024 MB
Given existing rail links and city flows, find the minimum cost to connect all cities, where an edge costs the product of its endpoint flows.
- Level
Medium5 of 10
- Topics
- Minimum spanning tree, Union-find, Greedy, Sorting
- Solved
- No attempts yet
Problem
The railway infrastructure of Bitlandia is being reorganized. This task has been assigned to Martynas, the head of the Bitlandia Train Company.
First, Martynas estimated the inbound passenger flow for each city . Martynas designs railway lines between cities so that:
- From any city in Bitlandia it is possible to travel by rail to every other city (not necessarily directly).
- Building one railway line between cities and costs biteuros — a larger flow requires more investment (a bigger station, a larger parking lot, and so on).
Some railways in Bitlandia have already been built, but with a reduced budget Martynas wants to build the missing lines as cheaply as possible.
Determine the minimum cost of building the remaining railway lines so that all of Martynas's requirements are satisfied.
Input
The first line contains two space-separated integers and — the number of cities in Bitlandia and the number of railway lines already built.
The second line contains space-separated integers .
Each of the next lines contains two integers and , meaning that a direct railway line already exists between cities and .
Output
Output the minimum cost, in biteuros, of building all the remaining railway lines.
Constraints
- All pairs are distinct and .