Find the MST for Grid
Time limit2sMemory limit512 MB
Given a grid graph whose edge weights come from four non-decreasing sequences, find the total weight of its minimum spanning tree.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
Consider a grid graph: the vertices are lined up into a grid of rows by columns. Let us denote the vertex in the -th row and -th column as .
To define the weights of the graph edges, we will consider four non-decreasing sequences, , , , and , consisting of , , , and positive integers, respectively:
- there is a bidirectional edge connecting vertices and of weight for all and such that and ;
- there is a bidirectional edge connecting vertices and of weight for all and such that and ;
- the graph contains no other edges.
Find the total weight of the edges in the minimal spanning tree of this graph.
Input
The first line of input contains two positive integers and ().
The second line contains integers : the elements of the sequence .
The third line contains integers : the elements of the sequence .
The fourth line contains integers : the elements of the sequence .
The fifth line contains integers : the elements of the sequence .
It is guaranteed that , , , and for , and additionally, .
Output
Print the total weight of the edges in the minimal spanning tree of the given graph. Note that the answer may not fit into a 32-bit integer.