#P5687. Minimum Spanning Tree on a Grid Graph

    ID: 18915 Type: Default 1000ms 256MiB

Minimum Spanning Tree on a Grid Graph

Minimum Spanning Tree on a Grid Graph

Given a grid graph of size n×mn \times m with rows numbered from 11 to nn and columns numbered from 11 to mm. Each vertex is denoted by its coordinates (r,c)(r, c). There is an edge between (i,j)(i, j) and (i,j+1)(i, j+1) with weight aia_i for 1in1 \le i \le n and 1j<m1 \le j < m, and an edge between (i,j)(i, j) and (i+1,j)(i+1, j) with weight bjb_j for 1i<n1 \le i < n and 1jm1 \le j \le m. Your task is to compute the total weight of the minimum spanning tree (MST) for this graph.

inputFormat

The input consists of three lines. The first line contains two integers nn and mm. The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n. The third line contains mm integers b1,b2,,bmb_1, b_2, \ldots, b_m.

outputFormat

Output a single integer representing the total weight of the minimum spanning tree.

sample

2 3
1 3
2 1 4
8