Public Judge

pjudge

时间限制: 2 s 内存限制: 512 MB 总分: 100 可 Hack ✓

#21625. [PR #3] Minimum Spanning Tree

统计

There is an $n \times m$ grid graph, where the grid point at row $x$ and column $y$ is denoted as $(x, y)$. Given four monotonically non-decreasing arrays $A, B, C, D$, the edge weights of the grid graph are determined by these four arrays as follows:

  • The weight of the edge between $(x, y)$ and $(x+1, y)$ is $A_x + B_y$.
  • The weight of the edge between $(x, y)$ and $(x, y+1)$ is $C_x + D_y$.

You need to find the size (total weight) of the Minimum Spanning Tree (MST) of this grid graph.

Input

The first line contains two integers $n$ and $m$, representing the size of the grid graph.

The second line contains $n-1$ integers $A_i$, representing array $A$.

The third line contains $m$ integers $B_i$, representing array $B$.

The fourth line contains $n$ integers $C_i$, representing array $C$.

The fifth line contains $m-1$ integers $D_i$, representing array $D$.

Output

Output a single integer representing the size of the Minimum Spanning Tree.

Examples

Input 1

2 3
1
1 3 6
1 4
1 2

Output 1

17

Note 1

The Minimum Spanning Tree contains the following five edges:

  • $(1,1) - (2,1): 2$
  • $(1,1) - (1,2): 2$
  • $(1,2) - (1,3): 3$
  • $(1,2) - (2,2): 4$
  • $(2,2) - (2,3): 6$

Input 2

See the provided files.

Constraints

For all test cases, it is guaranteed that $2 \leq n, m \leq 10^5$ and $1 \leq A_i, B_i, C_i, D_i \leq 10^6$.

It is guaranteed that $A_i \leq A_{i+1}$, $B_i \leq B_{i+1}$, $C_i \leq C_{i+1}$, and $D_i \leq D_{i+1}$.

Test Case ID $n, m \leq$ Special Property
$1 \sim 6$ $10^3$ None
$7 \sim 12$ $10^5$ There exist $x, y$ such that $A_i=x, B_i=y$, i.e., all elements in arrays $A$ and $B$ are identical
$13 \sim 20$ None

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.