#P5687. [CSP-S 2019 江西] 网格图

[CSP-S 2019 江西] 网格图

Description

Given an n×mn\times m grid graph, rows are numbered from 1n1\sim n and columns are numbered from 1m1\sim m. Each point can be represented by its row number rr and column number cc as (r,c)(r, c).

There is an edge with weight aia_i between points (i,j)(i,j) and (i,j+1)(i,j+1), where 1in,1j<m1\le i\le n, 1\le j<m.

There is an edge with weight bjb_j between points (i,j)(i, j) and (i+1,j)(i+1,j), where 1i<n,1jm1\le i< n, 1\le j \le m.

Please find the minimum spanning tree of this grid graph.

Input Format

The first line contains two positive integers n,mn, m, representing the number of rows and columns.

The second line contains nn positive integers, representing aia_i.

The third line contains mm positive integers, representing bjb_j.

Output Format

Output a single integer in one line, representing the answer.

3 3
2 4 3
1 3 2
16

Hint

Explanation of Sample Input/Output 1

The edges in the minimum spanning tree include: all edges in the first row, and all edges in the first, second, and third columns.

Constraints

For 20%20\% of the testdata, n,m3n, m\le 3, ai,bj10a_i, b_j \le 10.

For 40%40\% of the testdata, n,m20n, m\le 20, ai,bj100a_i, b_j\le 100.

For 64%64\% of the testdata, n,m300n, m\le 300, ai,bj1000a_i, b_j\le 1000.

For 100%100\% of the testdata, 3n,m3×1053\le n, m \le 3\times 10^5, 1ai,bj1051 \le a_i, b_j\le 10^5.

Translated by ChatGPT 5