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: Accepted)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define L(__) (__ << 1)
#define R(__) (L(__)|1) 
#define LL long long
#define oo (1<<30)
#define N 300005
using namespace std;
int st1[N],top1,st2[N],top2;
int n,w,h,m,z[N],ans,A[4*N],add[4*N];
struct dot{
	int x,y;
	dot(int _x=0,int _y=0){ x=_x,y=_y;}
}a[N],b[N];
bool cmp(const dot &p,const dot &q){ return p.x<q.x||(p.x==q.x&&p.y<q.y);}
int map(int x){ return lower_bound(z+1,z+m+1,x)-z;}
void update(int t){ A[t]=max(A[L(t)],A[R(t)]);}
void build(int t,int l,int r)
{
	int mid=(l+r)>>1; add[t]=0;
	if(l==r){ A[t]=-z[l]; return ;}
	build(L(t),l,mid);
	build(R(t),mid+1,r);
	update(t);
}
void Add(int t,int d){ add[t]+=d,A[t]+=d;}
void push(int t)
{
	if(add[t]==0) return ;
	Add(L(t),add[t]);
	Add(R(t),add[t]);
	add[t]=0;
}
void modify(int t,int l,int r,int l1,int r1,int d)
{
	int mid=(l+r)>>1;
	if(l>r1||r<l1||l1>r1) return ;
	if(l>=l1&&r<=r1) return Add(t,d);
	push(t);
	modify(L(t),l,mid,l1,r1,d);
	modify(R(t),mid+1,r,l1,r1,d);
	update(t);
}
void solve(int n)
{
	int i,mi=(h>>1);
	for(i=1;i<=n;i++) a[i]=b[i];
	sort(a+1,a+n+1,cmp);
	if(a[n].x<w)
		a[++n]=dot(w,0);
	top1=top2=0;
	for(i=1;i<=n;i++) z[i]=a[i].x;
	m=n,z[++m]=0,z[++m]=w;
	sort(z+1,z+m+1);
	for(i=1;i<=n;i++)
		a[i].x=map(a[i].x);
	build(1,1,m);
	if(a[1].x>1)
		modify(1,1,m,1,1,h);
	a[0].x=1;
	for(i=1;i<=n;i++){
		if(a[i].y<=mi){
			modify(1,1,m,a[i].x,a[i].x,h);
			ans=max(ans,A[1]+z[a[i].x]);
			modify(1,1,m,a[st1[top1]].x,a[i].x-1,-a[i].y);
			while(top1&&a[st1[top1]].y<=a[i].y){
				modify(1,1,m,a[st1[top1-1]].x,a[st1[top1]].x-1,a[st1[top1]].y-a[i].y);
				top1--;
			  }
			st1[++top1]=i;
		  }
		else{
			modify(1,1,m,a[i].x,a[i].x,h);
			ans=max(ans,A[1]+z[a[i].x]);
			modify(1,1,m,a[st2[top2]].x,a[i].x-1,a[i].y-h);
			while(top2&&a[st2[top2]].y>=a[i].y){
				modify(1,1,m,a[st2[top2-1]].x,a[st2[top2]].x-1,a[i].y-a[st2[top2]].y);
				top2--;
			  }
			st2[++top2]=i;
		  }
	  }
}
int main()
{
	int i;
	scanf("%d %d %d",&w,&h,&n);
	for(i=1;i<=n;i++)
		scanf("%d %d",&b[i].x,&b[i].y);
	solve(n);
	for(i=1;i<=n;i++) swap(b[i].x,b[i].y);
	swap(w,h);
	solve(n);
	cout<<ans*2;
	return 0;
}