This page is still under construction.

Parts of this page are still being built. What you see may change.

Crow

Time limit3sMemory limit256 MB

Summary
Sum the shortest path lengths between consecutive query points that stay above the ground and outside a polygonal mountain.
Level

Hard8 of 10

Topics
Geometry, Shortest path, Graph
Solved
No attempts yet

Problem

A crow lives on the two dimensional plane. Today it flew around all day again, looking for food and for anything shiny.

The part of the plane with y<0y < 0 is ground, so the crow cannot go there. There is also one mountain, and the crow cannot go inside it either. The mountain is described by NN vertices P1(x1,y1)P_1(x_1, y_1) through PN(xN,yN)P_N(x_N, y_N). The values xix_i are given in increasing order, every yiy_i satisfies yi≥0y_i \ge 0, and y1=yN=0y_1 = y_N = 0. Joining P1P_1 through PNP_N in order and then joining PNP_N back to P1P_1 gives the boundary of the mountain. The crow may travel along the edges of the mountain, but it cannot enter the interior.

The crow made M−1M-1 moves today. It started at Q1(X1,Y1)Q_1(X_1, Y_1), went to Q2(X2,Y2)Q_2(X_2, Y_2) next, and so on until it visited QM(XM,YM)Q_M(X_M, Y_M) and ended the day. The crow is clever, so each move from QiQ_i to Qi+1Q_{i+1} took a shortest path that passes through neither the interior of the ground nor the interior of the mountain. Write a program that computes the total distance the crow travelled today.

Input

The first line contains NN (3≤N≤10003 \le N \le 1000).

The ii-th of the next NN lines contains xix_i and yiy_i (−104≤xi≤104-10^4 \le x_i \le 10^4, 0≤yi≤1040 \le y_i \le 10^4) separated by one space. xi<xi+1x_i < x_{i+1} holds for every ii with 1≤i<N1 \le i < N, and y1=yN=0y_1 = y_N = 0.

The next line contains MM (2≤M≤10002 \le M \le 1000).

The ii-th of the next MM lines contains XiX_i and YiY_i (−104≤Xi≤104-10^4 \le X_i \le 10^4, 0≤Yi≤1040 \le Y_i \le 10^4) separated by one space. No (Xi,Yi)(X_i, Y_i) lies inside the mountain. It may lie on the boundary.

Output

Print the total distance the crow travelled, rounded to six digits after the decimal point, on one line. Always print all six digits.

Hint

The terrain of the first example is shown below. Green is the mountain and brown is the ground. The crow can move through the light blue area and along the edges of the mountain and the ground.

The next two pictures show the two moves of the crow. The first move has length 525\sqrt{2} and the second has length 5+55 + \sqrt{5}, so the two together are about 14.30713614.307136.

Examples2

  1. Example 1

    Input
    8
    0 0
    1 2
    2 3
    3 2
    4 0
    6 0
    8 4
    10 0
    3
    0 1
    5 0
    9 2
    
    Expected output
    14.307136
    
  2. Example 2

    Input
    3
    0 0
    2 4
    4 0
    3
    -1 0
    5 0
    -1 0
    
    Expected output
    20.000000