Problem D Making Perimeter of the Convex Hull Shortest

The convex hull of a set of three or more planar points is, when not all of them are on one line, the convex polygon with the smallest area that has all the points of the set on its boundary or in its inside. Your task is, given positions of the points of a set, to find how much shorter the perimeter of the convex hull can be made by excluding two points from the set.

The figures below correspond to the three cases given as Sample Input 1 to 3. Encircled points are excluded to make the shortest convex hull depicted as thick dashed lines.

<image> | <image> | <image>
---|---|---
Sample Input 1 | Sample Input 2 | Sample Input 3

Input

The input consists of a single test case in the following format.


$n$
$x_1$ $y_1$
...
$x_n$ $y_n$


Here, $n$ is the number of points in the set satisfying $5 \leq n \leq 10^5$. For each $i$, ($x_i, y_i$) gives the coordinates of the position of the $i$-th point in the set. $x_i$ and $y_i$ are integers between $-10^6$ and $10^6$, inclusive. All the points in the set are distinct, that is, $x_j \ne x_k$ or $y_j \ne y_k$ holds when $j \ne k$. It is guaranteed that no single line goes through $n - 2$ or more points in the set.

Output

Output the difference of the perimeter of the convex hull of the original set and the shortest of the perimeters of the convex hulls of the subsets with two points excluded from the original set. The output should not have an error greater than $10^{-4}$.

Sample Input 1


10
-53 62
-19 58
-11 11
-9 -22
45 -7
37 -39
47 -58
-2 41
-37 10
13 42


Sample Output 1


72.96316928


Sample Input 2


10
-53 62
-19 58
-11 11
-9 -22
45 -7
43 -47
47 -58
-2 41
-37 10
13 42


Sample Output 2


62.62947992


Sample Input 3


10
-53 62
-35 47
-11 11
-9 -22
45 -7
43 -47
47 -58
-2 41
-37 10
13 42


Sample Output 3


61.58166534






Example

Input

10
-53 62
-19 58
-11 11
-9 -22
45 -7
37 -39
47 -58
-2 41
-37 10
13 42


Output

72.96316928
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()

using LL = long long;

const int max_N = (int) 1e5 + 21;

struct P { 
	int x, y;
	
	explicit P(int x = 0, int y = 0) : x(x), y(y) {}
	
	inline P add(const P &p) const { return P(x + p.x, y + p.y); }
	
	inline P sub(const P &p) const { return P(x - p.x, y - p.y); }
	
	inline LL dot(const P &p) const { return 1ll * x * p.x + 1ll * y * p.y; }
	
	inline LL det(const P &p) const { return 1ll * x * p.y - 1ll * y * p.x; }
	
	inline LL abs2() { return dot(*this); }
	
	inline double abs() { return std::sqrt(abs2()); }
	
	bool operator<(const P &p) const { return x != p.x ? x < p.x : y < p.y; }
	
	bool operator==(const P &p) const { return x == p.x && y == p.y; }
} p[max_N], o;

bool origin[max_N], added[max_N]; 

int n, st[max_N], top, Q[max_N], tot; 

double ans;

std::vector<int> vec[3];

void initConvex() {
	std::sort(p + 1, p + 1 + n); 
	top = 0;
	auto check = [&](int i) -> bool {
		return p[i].sub(p[st[top - 1]]).det(p[st[top]].sub(p[st[top - 1]])) >= 0;
	};
	for (int i = 1; i <= n; ++i) {
		while (top > 1 && check(i)) --top; 
		st[++top] = i;
	}
	o = p[st[1]].add(p[st[top]]); 
	o.x >>= 1, o.y >>= 1; 
	for (int i = n - 1, tmp = top; i; --i) { 
		while (top > tmp && check(i)) --top; 
		st[++top] = i;
	}
	if (top > 1) --top;
	for (int i = 1; i <= top; ++i) origin[st[i]] = true;
}

std::priority_queue<std::pair<double, int>>pq;

void solve(int id, double delta, std::vector<int> &v, bool tp) { 
	static int st[2][max_N], Q[max_N]; 
	int top = 0;
	auto check = [&](int i) -> bool {
		return p[i].sub(p[st[tp][top - 1]]).det(p[st[tp][top]].sub(p[st[tp][top - 1]])) >= 0;
	};
	for (auto i : v) {
		while (top > 1 && check(i)) --top; 
		st[tp][++top] = i;
	}
	for (int i = 1; i < top; ++i) {
		delta += p[st[tp][i]].sub(p[st[tp][i + 1]]).abs();
	}
	ans = std::max(ans, -delta); 
	if (tp) {
		pq.push({delta, id});
		// printfC’X.lGfNn", delta); 
		if (pq.size() > 4) pq.pop();
		for (int i = 1; i <= top; ++i) added[st[tp][i]] = true; 
		int tot = 0;
		for (auto i = 0; i < v.size(); ++i) { 
			int x = v[i]; 
			if (added[x]) {
				Q[++tot] = i;
				// printf("added(96d, %d)\n", p[x].x, p[x].y);
			}
		}
		for (int i = 2; i < tot; ++i) { 
			std::vector<int> vv;
			for (int j = Q[i - 1]; j <= Q[i + 1]; ++j) { 
				if (j == Q[i]) continue; 
				vv.push_back(v[j]);
			}
			double delta1 = delta;
			delta1 -= p[v[Q[i]]].sub(p[v[Q[i - 1]]]).abs(); 
			delta1 -= p[v[Q[i]]].sub(p[v[Q[i + 1]]]).abs(); 
			solve(0, delta1, vv, false);
		}
		for (int i = 1; i <= top; ++i) added[st[tp][i]] = false;
	}
}

void pickUp(int id, std::vector<int> &v) { 
	if (id == tot) {
		for (auto i = Q[tot] + 1; i < vec[2].size(); ++i) { 
			v.push_back(vec[2][i]);
		}
		for (int i = 1; i < Q[1]; ++i) { 
			v.push_back(vec[2][i]);
		}
	} else {
		for (int i = Q[id] + 1; i < Q[id + 1]; ++i) { 
			v.push_back(vec[2][i]);
		}
	}
}

bool adjacent(int i, int j) { 
	if (i == j) return true; 
	if (i > j) std::swap(i, j); 
	return i + 1 == j || (i == 1 && j == tot);
}

void solve() {
	for (int i = 1; i <= n; ++i) {
		P v = p[i].sub(o);
		int d = v.y > 0 || (!v.y && v.x > 0); 
		vec[d].push_back(i);
	}
	for (int d = 0; d < 2; ++d) {
		std::sort(ALL(vec[d]), [&](int x, int y) { 
			return p[x].sub(o).det(p[y].sub(o)) > 0;
		});
		for (auto x : vec[d]) vec[2].push_back(x);
	}
	for (auto i = 0; i < vec[2].size(); ++i) { 
		int x = vec[2][i]; 
		if (origin[x]) Q[++tot] = i;
		// printf('’sort(%d, 36d)\nn, p[x].x, p[x].y);
	}
	for (int i = 1; i <= tot; ++i) { 
		int prev = i == 1 ? tot : i - 1; 
		int next = i == tot ? 1 : i + 1; 
		std::vector<int> v; 
		v.push_back(vec[2][Q[prev]]); 
		pickUp(prev, v), pickUp(i, v); 
		v.push_back(vec[2][Q[next]]); 
		double delta = 0;
		delta -= p[vec[2][Q[i]]].sub(p[vec[2][Q[prev]]]).abs(); 
		delta -= p[vec[2][Q[i]]].sub(p[vec[2][Q[next]]]).abs();
		// printf("delta(96d, %d) = ", p[vec[2][Q[i]]j.x, p[vec[2][Q[i]]].y); 
		solve(i, delta, v, true);
	}

	std::vector<std::pair<double, int>> best4; 
	while (!pq.empty()) {
		best4.push_back(pq.top()); 
		pq.pop();
	}
	for (auto a : best4)
		for (auto b : best4) {
			if (adjacent(a.second, b.second)) continue;
			// printf("nonadjacent(%d, %d) = %.10f, %.10f\n”, a.second, b.second, a.first, b.first); 
			ans = std::max(ans, -(a.first + b.first));
		}
		
	for (int i = 1; i <= tot; ++i) { 
		int prev = i == 1 ? tot : i - 1;
		int next = i == tot ? 1 : i + 1;
		int nnext = next == tot ? 1 : next + 1;
		std::vector<int> v; 
		v.push_back(vec[2][Q[prev]]); 
		pickUp(prev, v), pickUp(i, v), pickUp(next, v); 
		v.push_back(vec[2][Q[nnext]]); 
		double delta = 0;
		delta -= p[vec[2][Q[i]]].sub(p[vec[2][Q[prev]]]).abs(); 
		delta -= p[vec[2][Q[i]]].sub(p[vec[2][Q[next]]]).abs(); 
		delta -= p[vec[2][Q[nnext]]].sub(p[vec[2][Q[next]]]).abs(); 
		solve(0, delta, v, false);
	}
}


int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", &p[i].x, &p[i].y);
	}
	initConvex();
	solve();
	printf("%.10f\n", ans); 
	return 0;
}
