시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 19 | 16 | 15 | 100.000% |
Consider a grid graph: the vertices are lined up into a grid of $H$ rows by $W$ columns. Let us denote the vertex in the $i$-th row and $j$-th column as $(i, j)$.
To define the weights of the graph edges, we will consider four non-decreasing sequences, $A$, $B$, $C$, and $D$, consisting of $H-1$, $W$, $H$, and $W-1$ positive integers, respectively:
Find the total weight of the edges in the minimal spanning tree of this graph.
The first line of input contains two positive integers $H$ and $W$ ($2 \le H, W \le 10^5$).
The second line contains $H-1$ integers $A_i$: the elements of the sequence $A$.
The third line contains $W$ integers $B_i$: the elements of the sequence $B$.
The fourth line contains $H$ integers $C_i$: the elements of the sequence $C$.
The fifth line contains $W-1$ integers $D_i$: the elements of the sequence $D$.
It is guaranteed that $A_{i-1} \le A_{i}$, $B_{i-1} \le B_{i}$, $C_{i-1} \le C_{i}$, and $D_{i-1} \le D_{i}$ for $i>1$, and additionally, $1 \le A_i, B_i, C_i, D_i \le 10^6$.
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.
2 3 1 1 3 6 1 4 1 2
17
4 3 1 13 15 3 6 11 3 6 6 11 9 17
173