Tighten Up!

Time limit1sMemory limit128 MB

Summary
Given a polygonal string between two holes and a set of pins, compute the length of the taut chain that wraps around the pins when pulled tight.
Level

Hard8 of 10

Topics
Geometry, Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

A flat panel has two holes and some pins nailed onto its front surface. From behind the panel a string comes up through one hole, is laid on the surface as a polygonal chain, and passes back behind the panel through the other hole. Initially the string touches none of the pins.

We tie a stone of equal weight to each of the string's two ends. The stones slowly pull the string tight until no slack is left. Obstructed by some of the pins, the string settles into a new polygonal chain (in some layouts it is obstructed by no pin at all). While being pulled tight the string never catches on itself, so its final shape is a polygonal chain whose interior vertices are pin positions and whose two endpoints are the holes.

The string, the pins and the holes are thin enough that their sizes may be ignored. Write a program that computes the length of the tightened chain that lies on the surface.

Input

The input consists of several datasets and ends with a line containing two zeros separated by a space.

Each dataset has the following format, where l=m+nl = m + n:

m n
x1 y1
...
xl yl

The first line contains two integers mm and nn (2≤m≤1002 \le m \le 100, 0≤n≤1000 \le n \le 100): mm is the number of vertices that describe the initial string (the two holes included) and nn is the number of pins. Each of the following l=m+nl = m + n lines holds two integers xix_i and yiy_i (0≤xi≤10000 \le x_i \le 1000, 0≤yi≤10000 \le y_i \le 1000), the coordinates of a point Pi=(xi,yi)P_i = (x_i, y_i).

  • P1,…,PmP_1, \dots, P_m give the initial string: the holes are P1P_1 and PmP_m, and the string is the polygonal chain P1→P2→⋯→PmP_1 \to P_2 \to \dots \to P_m in this order.
  • Pm+1,…,Pm+nP_{m+1}, \dots, P_{m+n} are the pin positions.

No two points coincide, and no three points are collinear.

Output

For each dataset, print on its own line the length of the tightened string that remains on the surface, rounded to exactly three digits after the decimal point. Print nothing else.

Examples3

  1. Example 1

    Input
    6 16
    5 4
    11 988
    474 975
    459 16
    985 12
    984 982
    242 227
    140 266
    45 410
    92 570
    237 644
    370 567
    406 424
    336 290
    756 220
    634 251
    511 404
    575 554
    726 643
    868 571
    907 403
    845 283
    10 4
    261 196
    943 289
    859 925
    56 822
    112 383
    514 0
    1000 457
    514 1000
    0 485
    233 224
    710 242
    850 654
    485 915
    140 663
    26 5
    0 953
    180 0
    299 501
    37 301
    325 124
    162 507
    84 140
    913 409
    635 157
    645 555
    894 229
    598 223
    783 514
    765 137
    599 445
    695 126
    859 462
    599 312
    838 167
    708 563
    565 258
    945 283
    251 454
    125 111
    28 469
    1000 1000
    185 319
    717 296
    9 315
    372 249
    203 528
    15 15
    200 247
    859 597
    340 134
    967 247
    421 623
    1000 427
    751 1000
    102 737
    448 0
    978 510
    556 907
    0 582
    627 201
    697 963
    616 608
    345 819
    810 809
    437 706
    702 695
    448 474
    605 474
    329 355
    691 350
    816 231
    313 216
    864 360
    772 278
    756 747
    529 639
    513 525
    0 0
    
    Expected output
    2257.052
    3609.922
    2195.837
    3619.772
    
  2. Example 2

    Input
    2 0
    0 0
    300 400
    0 0
    
    Expected output
    500.000
    
  3. Example 3

    Input
    3 1
    0 0
    50 100
    100 0
    50 10
    0 0
    
    Expected output
    101.980