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 <bits/stdc++.h>
using namespace std;

const int N = 300000 + 10;

struct Point {
	int x, y;
	Point(){}
	Point( int x, int y ):x(x),y(y){}
};
bool cmp_y( const Point &r, const Point &s ) {
	return r.y < s.y;
}

struct Node;
void modify( Node *nd, int lf, int rg, int L, int R, int delta );
struct Node {
	int vmax;
	int flag;
	void pushdown( int lf, int rg ) {
		if( flag ) {
			int mid = (lf + rg) >> 1;
			modify( ls, lf, mid, lf, mid, flag );
			modify( rs, mid+1, rg, mid+1, rg, flag );
			flag = 0;
		}
	}
	void update() {
		vmax = max( ls->vmax, rs->vmax );
	}
	Node *ls, *rs;
}pool[N*2], *tail=pool, *root;

int n, H, W;
int disc[N], dtot;
int ltop, rtop;
Point pts[N];
Point lstk[N], rstk[N];

Node *build( int lf, int rg ) {
	Node *nd = ++tail;
	if( lf == rg ) {
		nd->vmax = W - disc[lf];
	} else {
		int mid = (lf + rg) >> 1;
		nd->ls = build( lf, mid );
		nd->rs = build( mid+1, rg );
		nd->flag = 0;
		nd->update();
	}
	return nd;
}
void modify( Node *nd, int lf, int rg, int L, int R, int delta ) {
	if( L <= lf && rg <= R ) {
		nd->vmax += delta;
		nd->flag += delta;
		return;
	}
	int mid = (lf + rg) >> 1;
	nd->pushdown(lf,rg);
	if( L <= mid )
		modify( nd->ls, lf, mid, L, R, delta );
	if( R > mid ) 
		modify( nd->rs, mid+1, rg, L, R, delta );
	nd->update();
}
int query( Node *nd, int lf, int rg, int L, int R ) {
	if( L <= lf && rg <= R ) {
		return nd->vmax;
	}
	int mid = (lf + rg) >> 1;
	nd->pushdown(lf,rg);
	if( R <= mid )
		return query( nd->ls, lf, mid, L, R );
	else if( L > mid )
		return query( nd->rs, mid+1, rg, L, R );
	else 
		return max( query( nd->ls, lf, mid, L, R ),
			query( nd->rs, mid+1, rg, L, R ) );
}

int solve() {
	/*
	printf( "n = %d W = %d H = %d\n", n, W, H );
	for( int i = 1; i <= n; i++ )
		printf( "%d %d\n", pts[i].x, pts[i].y );
	*/

	sort( pts+1, pts+1+n, cmp_y );
	dtot = 0;
	disc[++dtot] = 0;
	disc[++dtot] = H;
	for( int i = 1; i <= n; i++ )
		disc[++dtot] = pts[i].y;
	sort( disc+1, disc+1+dtot );
	dtot = unique( disc+1, disc+1+dtot ) - disc - 1;
	for( int i = 1; i <= n; i++ )
		pts[i].y = lower_bound( disc+1, disc+1+dtot, pts[i].y ) - disc;
	tail = pool;
	root = build( 1, dtot );
	int M = W>>1;
	int ans = 0;
	ltop = rtop = 0;
	for( int i = 1; i <= n; i++ ) {
		Point &p = pts[i];
		if( p.y == 1 ) continue;
		int s = query( root, 1, dtot, 1, p.y - 1 );
		ans = max( ans, (disc[p.y] + s)<<1 );
//		printf( "y = %d ans = %d\n", disc[p.y], ans );
		if( p.x <= M ) {
			int i;
			for( i = ltop; i >= 1; i-- ) {
				modify( root, 1, dtot, lstk[i].y, p.y - 1, i == ltop ? -p.x : -(p.x - lstk[i+1].x) );
				if( lstk[i].x > p.x ) break;
			}
			if( i == 0 ) {
				if( ltop == 0 )
					modify( root, 1, dtot, 1, p.y - 1, -p.x );
				else
					modify( root, 1, dtot, 1, lstk[1].y - 1, -(p.x - lstk[1].x) );
			}
			lstk[ltop = i + 1] = p;
		} else {
			int i;
			for( i = rtop; i >= 1; i-- ) {
				modify( root, 1, dtot, rstk[i].y, p.y - 1, i == rtop ? p.x - W : p.x - rstk[i+1].x );
				if( rstk[i].x < p.x ) break;
			}
			if( i == 0 ) {
				if( rtop == 0 ) 
					modify( root, 1, dtot, 1, p.y - 1, p.x - W );
				else
					modify( root, 1, dtot, 1, rstk[1].y - 1, p.x - rstk[1].x );
			}
			rstk[rtop = i + 1] = p;
		}
	}
	int s = query( root, 1, dtot, 1, dtot - 1 );
	ans = max( ans, (s + H)<<1 );
//	printf( "y = %d ans = %d\n", disc[dtot], ans );
	for( int i = 1; i <= n; i++ )
		pts[i].y = disc[pts[i].y];
	return ans;
}

int main() {
	scanf( "%d%d%d", &W, &H, &n );
	for( int i = 1; i <= n; i++ ) 
		scanf( "%d%d", &pts[i].x, &pts[i].y );
	int ans1, ans2, ans3;
	ans1 = solve();
	swap( W, H );
	for( int i = 1; i <= n; i++ )
		swap( pts[i].x, pts[i].y );
	ans2 = solve();
	ans3 = max( H, W ) * 2 + 2;
//	printf( "%d %d %d\n", ans1, ans2, ans3 );
	printf( "%d\n", max(max(ans1,ans2),ans3) );
}