Power Grid
View as PDF
Submit solution
Points:
100
Time limit:
2.0s
PyPy 3
5.0s
Python 3
5.0s
Memory limit:
500M
Problem type
Shichikuji must supply electricity to cities. City
sits at coordinates
.
A city has electricity if it has a power station, or if it is connected (directly or through other cities) to a city that does.
- Building a power station in city
costs
.
- Connecting cities
and
with a wire costs
.
Wires run along cardinal directions, so their length is the Manhattan distance between the two cities. Find the minimum cost to give every city electricity.
Input
The first line contains an integer .
The next lines each contain two integers
and
.
The next line contains integers
.
The last line contains integers
.
Output
Output a single integer: the minimum cost.
Constraints
Example 1
Input
3
2 3
1 1
3 2
3 2 3
3 2 3
Output
8
Explanation
Building a station in every city costs , which is optimal.
Example 2
Input
3
2 1
1 2
3 3
23 2 23
3 2 3
Output
27
Explanation
Build a station in city (cost
), connect
--
(cost
), and
connect
--
(cost
), for a total of
.
Comments