Problem:
Problem E: The Two Men of the Japanese Alps

Two experienced climbers are planning a first-ever attempt: they start at two points of the equal altitudes on a mountain range, move back and forth on a single route keeping their altitudes equal, and finally meet with each other at a point on the route. A wise man told them that if a route has no point lower than the start points (of the equal altitudes) there is at least a way to achieve the attempt. This is the reason why the two climbers dare to start planning this fancy attempt.

The two climbers already obtained altimeters (devices that indicate altitude) and communication devices that are needed for keeping their altitudes equal. They also picked up a candidate route for the attempt: the route consists of consequent line segments without branches; the two starting points are at the two ends of the route; there is no point lower than the two starting points of the equal altitudes. An illustration of the route is given in Figure E.1 (this figure corresponds to the first dataset of the sample input).

Figure E.1: An illustration of a route

The attempt should be possible for the route as the wise man said. The two climbers, however, could not find a pair of move sequences to achieve the attempt, because they cannot keep their altitudes equal without a complex combination of both forward and backward moves. For example, for the route illustrated above: a climber starting at p1 (say A) moves to s, and the other climber (say B) moves from p6 to p5; then A moves back to t while B moves to p4; finally A arrives at p3 and at the same time B also arrives at p3. Things can be much more complicated and thus they asked you to write a program to find a pair of move sequences for them.

There may exist more than one possible pair of move sequences, and thus you are requested to find the pair of move sequences with the shortest length sum. Here, we measure the length along the route surface, i.e., an uphill path from (0, 0) to (3, 4) has the length of 5.

Input

The input is a sequence of datasets.

The first line of each dataset has an integer indicating the number of points N (2 ≤ N ≤ 100) on the route. Each of the following N lines has the coordinates (xi, yi) (i = 1, 2, ... , N) of the points: the two start points are (x1, y1) and (xN, yN); the line segments of the route connect (xi, yi) and (xi+1, yi+1) for i = 1, 2, ... , N - 1. Here, xi is the horizontal distance along the route from the start point x1, and yi is the altitude relative to the start point y1. All the coordinates are non-negative integers smaller than 1000, and inequality xi < xi+1 holds for i = 1, 2, .. , N - 1, and 0 = y1 = yN ≤ yi for i = 2, 3, ... , N - 1.

The end of the input is indicated by a line containing a zero.

Output

For each dataset, output the minimum sum of lengths (along the route) of move sequences until the two climbers meet with each other at a point on the route. Each output value may not have an error greater than 0.01.

Sample Input

6
0 0
3 4
9 12
17 6
21 9
33 0
5
0 0
10 0
20 0
30 0
40 0
10
0 0
1 2
3 0
6 3
9 0
11 2
13 0
15 2
16 2
18 0
7
0 0
150 997
300 1
450 999
600 2
750 998
900 0
0

Output for the Sample Input

52.5
40.0
30.3356209304689
10078.072814085803

Solution in C (judge verdict: Accepted)

// AOJ 1309: The Two Men of the Japanese Alps
// 2019.2.27 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

// 整数入力を受け付ける関数 in()
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif
int in()
{
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

// 数値のハッシュ関数
#define HASHSIZ 499979   // 素数にすべきだと言われる
int hash[HASHSIZ+5], *hashend = hash + HASHSIZ;   // 初期値はゼロ

int insert(int n)       // リターン値：追加する整数が既存なら 0、追加成功なら 1
{
	int *p = hash + n % HASHSIZ;
	while (*p) {
		if (*p == n) return 0;
		if (++p == hashend) p = hash;
	}
	*p = n;
	return 1;
}

// 優先度付きキュー
typedef struct { double len; int l, r; } QUE;
QUE que[2048];
int qsize;             // 初期値はゼロ

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

// min_heapify() は外部から見えない
static void min_heapify(int i)
{
	int l, r, min;
	QUE qt;
	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].len < que[i].len) min = l; else min = i;
	if (r < qsize && que[r].len < que[min].len) min = r;
	if (min != i) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		min_heapify(min);
	}
}

// キューのトップから要素を取り除く
// キュートップの要素を取得するには que[0].ken, que[0].l, que[0].r に直接アクセス
void deq()
{
	que[0] = que[--qsize];
	min_heapify(0);
}

// キューに要素を追加する
void enq(int l, int r, double len)
{
	int i, min;
	QUE qt;
	i = qsize++;
	que[i].len = len, que[i].l = l, que[i].r = r;
	while (i > 0 && que[min = PARENT(i)].len > que[i].len) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		i = min;
	}
}

// 平面幾何ライブラリー
typedef struct { double x, y; } PP;
typedef struct { PP s, e; } SEG, LINE;

#define INF			1e8
#define EPS			1e-8
#define EQ(a,b)		(fabs((a)-(b))<EPS)
#define PPeQ(a,b)	(EQ(a.x,b.x)&&EQ(a.y,b.y))

PP vset(double x, double y) { PP r; r.x = x, r.y = y; return r; }
PP vadd(PP p1, PP p2) { PP r; r.x = p1.x + p2.x, r.y = p1.y + p2.y; return r; }
PP vsub(PP p1, PP p2) { PP r; r.x = p1.x - p2.x, r.y = p1.y - p2.y; return r; }
PP vsmul(PP p, double k) { PP r; r.x = p.x * k, r.y = p.y * k; return r; }

double dist(PP p1, PP p2) { return hypot(p1.x - p2.x, p1.y - p2.y); } // 二点間距離
double cross(PP a, PP b) { return a.x * b.y - a.y * b.x; }
double dot(PP a, PP b) { return a.x * b.x + a.y * b.y; }
double norm(PP a) { return a.x * a.x + a.y * a.y; }
PP dir(SEG s) { return vsub(s.e, s.s); }

int ccw(PP p0, PP p1, PP p2) {
	PP a, b;	double t;
	a = vsub(p1, p0), b = vsub(p2, p0), t = cross(a, b);
	if (t > EPS) return 1;	if (t < -EPS) return -1;
	if (dot(a, b) < -EPS) return 2;	if (norm(a) < norm(b)) return -2;
	return 0;
}

// セグメントライン２本が内部で交差するか、についての判定関数
int isIntersectSS(SEG s1, SEG s2)
{
	return ccw(s1.s, s1.e, s2.s) * ccw(s1.s, s1.e, s2.e) < 0 &&
		ccw(s2.s, s2.e, s1.s) * ccw(s2.s, s2.e, s1.e) < 0;
}

// セグメントライン２本の交差する点（座標）
PP crossPointSS(SEG s1, SEG s2)
{
	double n, d;
	n = cross(vsub(s2.s, s1.s), dir(s2));
	d = cross(dir(s1), dir(s2));
	return vadd(s1.s, vsmul(dir(s1), n / d));
}

int N;
PP pp[5000];

// 最短距離を求める
double dijkstra()
{
	int i, j, l, r, ll, rr;
	double len;

	memset(hash, 0, sizeof(hash));
	qsize = 0;
	enq(0, N - 1, 0.0);
	while (qsize) {
		len = que[0].len, l = que[0].l, r = que[0].r; deq();
		if (l == r) return len;
		if (!insert(l | (r << 14))) continue;

		for (i = -1; i <= 1; i++) {
			ll = l + i;
			if (ll < 0) continue;
			for (j = -1; j <= 1; j++) if (i | j) {
				rr = r + j;
				if (rr >= N) continue;
				if (EQ(pp[ll].y, pp[rr].y)) {
					enq(ll, rr, len + dist(pp[l], pp[ll]) + dist(pp[r], pp[rr]));
				}
			}
		}
	}
	return -1.0;
}

// qsort()用比較関数
int cmp(const void *a, const void *b)
{
	if (((PP *)a)->x <= ((PP *)b)->x) return -1;
	return 1;
}

int main()
{
	int i, j, sz;
	SEG s1, s2;

	while (N = in()) {
		for (i = 0; i < N; i++) {
			pp[i].x = in(), pp[i].y = in();
		}
		sz = N;
		for (i = 0; i < N; i++) {
			if (EQ(pp[i].y, 0)) continue;
			s1.s = vset(-1.0, pp[i].y), s1.e = vset(1001.0, pp[i].y);
			for (j = 0; j < N - 1; j++) {
				s2.s = vset(pp[j].x, pp[j].y), s2.e = vset(pp[j + 1].x, pp[j + 1].y);
				if (isIntersectSS(s1, s2))	pp[sz++] = crossPointSS(s1, s2);
			}
		}
		qsort(pp, sz, sizeof(PP), cmp);

		for (i = 0, j = 1; j < sz; j++) {
			while (j < sz && PPeQ(pp[j], pp[i])) j++;
			if (j < sz) if (++i != j) pp[i] = pp[j];
		}
		N = i + 1;

		printf("%lf\n", dijkstra());
	}
	return 0;
}