Two Mountaineers

Time limit1sMemory limit128 MB

Summary
Given a polygonal mountain profile with equal endpoints, find the minimum total elevation change for two climbers who swap endpoints while staying at equal height.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Geometry, Implementation
Solved
No attempts yet

Problem

Two mountaineers stand at opposite ends of a mountain range at exactly the same elevation. Each wants to walk along the range and reach the other climber's starting point, and throughout the journey the two must always stay at the same elevation. To keep their elevations equal, a climber may move either forward or backward along the range. As long as the range never drops below the starting elevation, the two climbers can always exchange starting points in this way. Given the shape of a mountain range, compute the minimum possible sum of the lengths of the two climbers' walks.

The mountain range is modeled as a polygonal chain P=(p1,p2,…,pn)P = (p_1, p_2, \dots, p_n) with vertices pi=(xi,yi)p_i = (x_i, y_i). It is monotone in the xx-direction, i.e. x1<x2<⋯<xnx_1 < x_2 < \dots < x_n, so it is the graph of a piecewise-linear function of xx.

A walk of a climber is a sequence of points W=(w1,w2,…,wm)W = (w_1, w_2, \dots, w_m) on PP such that every consecutive part (wj,wj+1)(w_j, w_{j+1}) lies on PP. The length of a single step (wj,wj+1)(w_j, w_{j+1}) is measured only by the change in elevation, ∣y(wj+1)−y(wj)∣|y(w_{j+1}) - y(w_j)| (horizontal movement contributes nothing), and the length of the whole walk is the sum of the lengths of its steps. Climber A starts at p1p_1 and climber B starts at pnp_n; since they begin at the same elevation, y1=yny_1 = y_n.

For example, in the figure above, the left climber's walk is (a,d,c,e,g,f,h,j,i,l,o,p,r,p,o,m,o,p,s)(a, d, c, e, g, f, h, j, i, l, o, p, r, p, o, m, o, p, s), and the resulting sum of the two climbers' walk lengths is 120120, which is the minimum.

Input

The first line contains the number of test cases TT (1≤T≤31 \le T \le 3).

Each test case is given as follows. The first line contains an integer nn (3≤n≤10003 \le n \le 1000), the number of vertices of the polygonal chain PP. Each of the next nn lines contains two integers xix_i and yiy_i (0≤xi,yi≤100000 \le x_i, y_i \le 10000), the coordinates of pip_i, listed in the order p1,p2,…,pnp_1, p_2, \dots, p_n. The xx-coordinates are strictly increasing.

Climber A starts at p1p_1 and climber B starts at pnp_n, so y1=yny_1 = y_n. In every test case the mountain range never drops below the starting elevation, and the vertex elevations yiy_i are all distinct except that the two endpoints share the starting elevation (y1=yny_1 = y_n).

Output

For each test case, print a single line containing the minimum possible sum of the two climbers' walk lengths.

Examples1

  1. Example 1

    Input
    3
    3
    0 0
    5 5
    10 0
    5
    0 0
    2 10
    3 5
    4 15
    5 0
    7
    5 10
    6 15
    7 11
    8 20
    9 12
    11 14
    13 10
    
    Expected output
    20
    100
    120