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<cstdio>
#include<algorithm>
#define ls p<<1,l,m
#define rs p<<1|1,m+1,r
using namespace std;
const int N=3e5+5,P=4*N;
int nn,mm,n,i,x,y,k;
struct arr{int x,y;}a[N];
bool operator < (arr A,arr B){return A.x<B.x;}
int read(){
	char c=getchar();int k=0;for (;c<48||c>57;c=getchar());
	for (;c>47&&c<58;c=getchar()) k=(k<<3)+(k<<1)+c-48;return k;
}
int tl,tr;
struct stack{int x,k;}ql[N],qr[N];
int mx[P],ad[P];
void change(int p,int l,int r){
	if (x<=l&&r<=y){ad[p]+=k;mx[p]+=k;return;}
	int m=l+r>>1;
	if (x<=m) change(ls);if (y>m) change(rs);
	mx[p]=ad[p]+max(mx[p<<1],mx[p<<1|1]);
}
void build(int p,int l,int r){
	if (l==r){mx[p]=mm-a[l].x;return;}
	int m=l+r>>1;build(ls);build(rs);
	ad[p]=0;mx[p]=ad[p]+max(mx[p<<1],mx[p<<1|1]);
}
int work(){
	sort(a+1,a+n+1);int res=0;
	tl=1;ql[0].x=-1;ql[1]=(stack){0,0};
	tr=1;qr[0].x=-1;qr[1]=(stack){0,mm};
	build(1,0,n);int mid=mm>>1;
	for (i=1;i<=n;i++){
		res=max(res,(mx[1]+a[i].x)*2);
		if (a[i].y<=mid){
			for (;tl&&a[i].y>ql[tl].k;tl--)
				x=ql[tl-1].x+1,y=ql[tl].x,
				k=ql[tl].k-a[i].y,change(1,0,n);
			ql[++tl]=(stack){i-1,a[i].y};
		}
		else{
			for (;tr&&a[i].y<qr[tr].k;tr--)
				x=qr[tr-1].x+1,y=qr[tr].x,
				k=a[i].y-qr[tr].k,change(1,0,n);
			qr[++tr]=(stack){i-1,a[i].y};
		}
		ql[++tl]=(stack){i,0};qr[++tr]=(stack){i,mm};
	}
	res=max(res,(mx[1]+nn)*2);
}
int main(){
	nn=read();mm=read();
	for (n=read(),i=1;i<=n;i++)
		a[i]=(arr){read(),read()};
	int ans=work();swap(nn,mm);
	for (i=1;i<=n;i++) swap(a[i].x,a[i].y);
	ans=max(ans,work());
	printf("%d\n",ans);
}