Problem:
Score : 1600 points

Problem Statement

There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.

Snuke plotted N points into the rectangle. The coordinate of the i-th (1 ≦ i ≦ N) point was (x_i, y_i).

Then, for each 1 ≦ i ≦ N, he will paint one of the following four regions black:

the region satisfying x < x_i within the rectangle

the region satisfying x > x_i within the rectangle

the region satisfying y < y_i within the rectangle

the region satisfying y > y_i within the rectangle

Find the longest possible perimeter of the white region of a rectangular shape within the rectangle after he finishes painting.

Constraints

1 ≦ W, H ≦ 10^8

1 ≦ N ≦ 3 \times 10^5

0 ≦ x_i ≦ W (1 ≦ i ≦ N)

0 ≦ y_i ≦ H (1 ≦ i ≦ N)

W, H (21:32, added), x_i and y_i are integers.

If i ≠ j, then x_i ≠ x_j and y_i ≠ y_j.

Input

The input is given from Standard Input in the following format:

W H N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the longest possible perimeter of the white region of a rectangular shape within the rectangle after Snuke finishes painting.

Sample Input 1

10 10 4
1 6
4 1
6 9
9 4

Sample Output 1

32

In this case, the maximum perimeter of 32 can be obtained by painting the rectangle as follows:

Sample Input 2

5 4 5
0 0
1 1
2 2
4 3
5 4

Sample Output 2

12

Sample Input 3

100 100 8
19 33
8 10
52 18
94 2
81 36
88 95
67 83
20 71

Sample Output 3

270

Sample Input 4

100000000 100000000 1
3 4

Sample Output 4

399999994

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

//*
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <functional>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

ll w, h;
int n;
pll a[400000];

ll ans;

ll xc;

vector<pll> le, re;
vector<pair<ll, pll>> lg, rg;
vector<int> v;

ll intlen(pll x, pll y)
{
	return min(x.second, y.second)-max(x.first, y.first);
}

void dodo(int st, int en, ll xdown, ll xup)
{
	if(st > en) return;
	int mid=(st+en)/2;
	ll yc;
	if(st < en) yc=(a[mid].second+a[mid+1].second)/2;
	else
	{
		if(a[mid].second+1 >= xdown && a[mid].second+1 <= xup) yc=a[mid].second+1;
		else yc=a[mid].second-1;
	}
	int i, j, k;
	le.clear(), re.clear(), lg.clear(), rg.clear();
	v.clear();
	for(i=st;i<=en;i++)
	{
		if(a[i].first <= xc) le.push_back(a[i]);
		else re.push_back(a[i]);
	}
	sort(all(le)), sort(all(re));
	ll up=xup, down=xdown;
	for(i=le.size()-1;i>=0;i--)
	{
		pll u=le[i];
		lg.emplace_back(u.first, mp(down, up));
		if(u.second > yc) up=min(up, u.second);
		else down=max(down, u.second);
	}
	lg.emplace_back(0, mp(down, up));
	reverse(all(lg));
	up=xup, down=xdown;
	for(i=0;i<re.size();i++)
	{
		pll u=re[i];
		rg.emplace_back(u.first, mp(down, up));
		if(u.second > yc) up=min(up, u.second);
		else down=max(down, u.second);
	}
	rg.emplace_back(w, mp(down, up));
	int ix1=rg.size(), ix2=rg.size();
	int vix=0;
	ll tmax=0;
	for(i=0;i<lg.size();i++)
	{
		auto u=lg[i];
		while(ix1 > 0 && !(rg[ix1-1].second.first <= u.second.first && u.second.second <= rg[ix1-1].second.second))
		{
			while(v.size() && intlen(rg[v.back()].second, u.second)+rg[v.back()].first-u.first <= intlen(rg[ix1-1].second, u.second)+rg[ix1-1].first-u.first)
			{
				v.pop_back();
			}
			v.push_back(ix1-1);
			ix1--;
		}
		while(ix2 > 0 && u.second.first <= rg[ix2-1].second.first && rg[ix2-1].second.second <= u.second.second && u.second != rg[ix2-1].second)
		{
			if(v.size() > vix && v[vix] == ix2-1) vix++;
			tmax=max(tmax, rg[ix2-1].second.second-rg[ix2-1].second.first+rg[ix2-1].first);
			ix2--;
		}
		if(ix1 != 0) ans=max(ans, u.second.second-u.second.first+rg[ix1-1].first-u.first);
		if(ix1 <= ix2-1) ans=max(ans, intlen(rg[v[vix]].second, u.second)+rg[v[vix]].first-u.first);
		if(ix2 <= rg.size()-1) ans=max(ans, tmax-u.first);
	}
	if(st == en) return;
	dodo(st, mid, xdown, yc);
	dodo(mid+1, en, yc, xup);
}

int main()
{
	int i, j, k, l;
	cin>>w>>h>>n;
	for(i=0;i<n;i++) scanf("%lld%lld", &a[i].first, &a[i].second), a[i].first*=2, a[i].second*=2;
	w*=2, h*=2;
	for(k=0;k<2;k++)
	{
		xc=w/2;
		if(xc%2 == 0) xc++;
		sort(a, a+n, [](pll x, pll y){return x.second < y.second;});
		ll dmax=0;
		for(i=0;i<n-1;i++) dmax=max(dmax, a[i+1].second-a[i].second);
		dmax=max(dmax, a[0].second);
		dmax=max(dmax, h-a[n-1].second);
		ans=max(ans, dmax+w);
		dodo(0, n-1, 0, h);
		swap(w, h);
		for(i=0;i<n;i++) swap(a[i].first, a[i].second);
	}
	cout<<ans<<endl;
	return 0;
}
//*/