#P2632. Minimum Spanning Tree on Two Lines

    ID: 15899 Type: Default 1000ms 256MiB

Minimum Spanning Tree on Two Lines

Minimum Spanning Tree on Two Lines

Given two horizontal lines with points on each, compute the weight of the minimum spanning tree (MST) connecting all the points. The first line (at y = 0) has n points and the second line (at y = 1) has m points.

The weight of an edge between two points \((x_1, y_1)\) and \((x_2, y_2)\) is given by the Euclidean distance:

\( \sqrt{(x_1-x_2)^2+(y_1-y_2)^2} \).

Your task is to calculate and output the total weight of the MST that connects all \(n+m\) points. Print the answer with 6 digits after the decimal point.

inputFormat

The input consists of three lines:

  • The first line contains two integers n and m — the numbers of points on the first and second lines respectively.
  • The second line contains n integers representing the x-coordinates of the points on the first line (with y-coordinate 0).
  • The third line contains m integers representing the x-coordinates of the points on the second line (with y-coordinate 1).

outputFormat

Output a single floating point number which is the total weight of the MST connecting all points. The answer should be printed with 6 digits after the decimal point.

sample

1 1
0
0
1.000000

</p>