There are cities and initially roads between them. You are the Civil Engineer (but a programmer from heart!) who is in charge of constructing roads between these cities.
The cities are numbered from to . There are types of road constructions which can be performed among these cities. For each , the -th kind of road construction is to choose an integer such that and construct a new two-way road connecting city and city . Performing the -th kind of road construction once costs taka.
You can do these kinds of road constructions any number of times (possibly zero) in any order. For example, if three kinds of road constructions are available, you can choose to perform the first type of road construction twice, the second zero times, and the third once.
The people of these cities want to travel from one city to another city. You have to determine whether it is possible to make all the cities connected together. If possible, then determine the minimum total cost. Here, “connected” means, there exists at least one path from each city to every other city.
Constraints:
Input is given from Standard Input in the following format:
If it is possible to make all the cities connected, print the minimum total cost needed to do so.
If it is impossible to make all the cities connected, print .
Input | Output |
---|---|
4 2 2 3 3 5 | 11 |
If we first do the road construction of the first kind to connect cities and , then do it again to connect cities and , and finally the road construction of the second kind to connect cities and , all the cities will be connected together. The total cost incurred here is taka, which is the minimum possible. |
Input | Output |
---|---|
6 1 3 4 | -1 |
There is no way to make all the cities connected, so we should print . |