Problem:
Score : 400 points

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min}.

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

Print the minimum possible value of S_{max} - S_{min}.

Sample Input 1

3 5

Sample Output 1

0

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		bool[][][] forbid = new bool[N+1][][];
		for(int i=0;i<N+1;i++){
			forbid[i] = new bool[N+1][];
			for(int j=0;j<N+1;j++){
				forbid[i][j] = new bool[N+1];
			}
		}
		
		for(int k=0;k<M;k++){
			int r = R[k];
			int l = L[k];
			int x = X[k];
			for(int i=0;i<r;i++){
				for(int j=0;j<r;j++){
					int cnt = 1;
					if(l <= i) cnt++;
					if(l <= j) cnt++;
					if(cnt != x){
						forbid[r][i][j] = true;
						forbid[r][j][i] = true;
						forbid[i][r][j] = true;
						forbid[i][j][r] = true;
						forbid[j][i][r] = true;
						forbid[j][r][i] = true;
					}
				}
			}
		}
		
		int mod = (int)1e9 + 7;
		int[][][] dp = new int[N+1][][];
		for(int i=0;i<N+1;i++){
			dp[i] = new int[N+1][];
			for(int j=0;j<N+1;j++){
				dp[i][j] = new int[N+1];
			}
		}
		
		dp[0][0][0] = 1;
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				for(int k=0;k<=N;k++){
					
					if(forbid[i][j][k]){
						dp[i][j][k] = 0;
						continue;
					}
					int nxt = 1 + Math.Max(Math.Max(i, j), k);
					if(nxt > N || dp[i][j][k] == 0) continue;
					dp[nxt][j][k] += dp[i][j][k]; if(dp[nxt][j][k] >= mod) dp[nxt][j][k] -= mod;
					dp[i][nxt][k] += dp[i][j][k]; if(dp[i][nxt][k] >= mod) dp[i][nxt][k] -= mod;
					dp[i][j][nxt] += dp[i][j][k]; if(dp[i][j][nxt] >= mod) dp[i][j][nxt] -= mod;
				}
			}
		}
		
		int tot = 0;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				tot += dp[i][j][N]; if(tot >= mod) tot -= mod;
				tot += dp[i][N][j]; if(tot >= mod) tot -= mod;
				tot += dp[N][i][j]; if(tot >= mod) tot -= mod;
			}
		}
		Console.WriteLine(tot);
		
	}
	int N, M;
	int[] L,R,X;
	public Sol(){
		var d = ria();
		N = d[0]; M = d[1];
		L = new int[M];
		R = new int[M];
		X = new int[M];
		for(int i=0;i<M;i++){
			d = ria();
			L[i] = d[0]; R[i] = d[1]; X[i] = d[2];
		
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}