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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double DD;
const int inf = 1e9;
const int mo= 1e9+7;
const int N = 3e5+10;
int n, W, H, cnt, x[N], y[N], tmp[N * 3];
int d[N * 3][2], stk[N * 3][2], top[2];
int mx[N << 2], tag[N << 2];

inline void add(int x){
	if(x >= 1 && x <= H)
		tmp[++cnt] = x;
}

inline void update(int p){
	mx[p] = max(mx[p << 1], mx[p << 1 | 1]);
}

inline void ad(int p,int x){
	tag[p] += x;
	mx[p] += x;
}

void clear(int p){
	if(!tag[p])
		return;
	ad(p << 1, tag[p]);
	ad(p << 1 | 1, tag[p]);
	tag[p] = 0;
}

void build(int p, int l, int r){
	if(l == r){
		mx[p] = -tmp[l];
		tag[p] = 0;
		return;
	}
	int mid = (l + r) >> 1;
	build(p << 1, l, mid);
	build(p << 1 | 1, mid + 1, r);
	update(p);
}

void add(int p, int l, int r, int s, int t, int x){
	if(l == s && r == t){
		ad(p, x);
		return ;
	}
	clear(p);
	int mid = (l + r) >> 1;
	if(t <= mid)
		add(p << 1, l, mid, s, t, x);
	else{
		if(s > mid)
			add(p << 1 | 1, mid + 1, r, s, t, x);
		else {
			add(p << 1, l, mid, s, mid, x);
			add(p << 1 | 1, mid + 1, r, mid + 1, t, x);
		}
	}
	update(p);
}

int solve(){
	int mid = W >> 1;
	cnt = 0;
	for(int i = 1; i <= n; i++){
		add(y[i]);
		add(y[i] + 1);
		add(y[i] - 1);
	}
	sort(tmp + 1, tmp + cnt + 1);
	cnt = unique(tmp + 1, tmp + cnt + 1) - tmp - 1;
	if(!cnt){
		cout << (W + H) * 2 << endl;
		exit(0);
	}
	build(1, 1, cnt);
	for(int i = 1; i <= cnt; i++){
		d[i][0] = mid;
		d[i][1] = W - mid;
	}
	for(int i = 1; i <= n ; i++){
		if(y[i] == 0 || y[i] == H)
			continue;
		int t = lower_bound(tmp + 1, tmp + cnt + 1, y[i]) - tmp;
		if(x[i] <= mid)
			d[t][0] = min(d[t][0], mid - x[i]); 
		else
			d[t][1] = min(d[t][1], x[i] - mid);
	}
	top[0] = top[1] = 0;
	int ans = H + 1;
	for(int i = 1; i <= cnt; i++){
		for(int k = 0; k < 2; k++){
			add(1, 1, cnt, i , i, d[i][k]);
			while(top[k] && d[ stk[top[k]][k] ][k] > d[i][k]){
				int l = stk[top[k] - 1][k] + 1, r = stk[top[k]][k];
				add(1, 1, cnt, l, r, d[i][k] - d[r][k]);
				top[k]--;
			}
			stk[++top[k]][k]=i;
		}
		ans = max(ans, tmp[i] + mx[1] + 2);
	}
	return ans << 1;
}


int main(){
	ios::sync_with_stdio(false);
	cin >> W >> H >> n;
	for(int i = 1; i <= n; i++)
		cin >> x[i] >> y[i];
	int t1 = solve();
	
	for(int i = 1; i <= n; i++)
		swap(x[i], y[i]);
	swap(W, H);
	int t2=solve();
	cout << max(t1, t2) << endl;
	
	return 0;
}