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

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

namespace codeTester.todayProblem
{
    class ABC062C
    {
        static List<long> hw = new List<long>();
        static long answer = 0;

        public static void Main(string[] args)
        {
            Input();
            Logic();
            Output();
        }

        static void Input()
        {
            hw = ReadSpaceSeparatedValues();
        }

        public static List<long> ReadSpaceSeparatedValues()
        {
            List<long> values = new List<long>();
            string[] inputs = Console.ReadLine().Split(' ');
            foreach (string input in inputs)
            {
                values.Add(long.Parse(input));
            }

            return values;
        }

        static void Logic()
        {
            long h = hw[0];
            long w = hw[1];
            long hMod3 = h % 3;
            long wMod3 = w % 3;
            if (hMod3 == 0 || wMod3 == 0)
            {
                answer = 0;
                return;
            }
            answer = Math.Min(compromise(h, w), compromise(w, h));
        }

        /*
         * サイズ差が戻り値
         */
        static long compromise(long longer, long shorter)
        {
            long firstCutoutLength = longer % 3 == 2 ? (longer / 3 + 1) : (longer / 3);
            long firstPieceSize = firstCutoutLength * shorter;
            long remainLength = longer - firstCutoutLength;
            
            // 残りは正確に半分にできるならそれがベスト
            if (shorter % 2 == 0 || remainLength % 2 == 0)
            {
                return Math.Abs(firstPieceSize - (remainLength * shorter) / 2);
            }
            LogRectangle(1, firstCutoutLength, shorter);
            return compromise2(firstPieceSize, Math.Max(shorter, remainLength), Math.Min(shorter, remainLength));
        }

        static long compromise2(long firstPieceSize, long longer, long shorter)
        {
            long halfLonger = longer / 2;
            long secondPieceSize = halfLonger * shorter;
            long thirdPieceSize = (halfLonger + 1) * shorter;

            long diff12 = Math.Abs(firstPieceSize - secondPieceSize);
            long diff13 = Math.Abs(firstPieceSize - thirdPieceSize);
            long diff23 = Math.Abs(secondPieceSize - thirdPieceSize);

            return Math.Max(diff12, Math.Max(diff13, diff23));
        }


        static void Output()
        {
            Console.WriteLine(answer.ToString());
        }

        static void DebugPrint(string msg)
        {
            // 提出時に消す
            // Console.WriteLine(msg);
        }

        static void LogRectangle(int id, long longer, long shorter)
        {
            DebugPrint(id.ToString() + " : " + longer.ToString()
                + " * " + shorter.ToString() + " = " + (longer * shorter).ToString());
        }
    }
}