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 (Python):
H, W = list(map(int, input().split()))
ans = 10**10

for w1 in range(1, W//2+1):
    s1 = H*w1

    w2 = (W-w1)//2
    w3 = W-w1-w2
    s2 = H*w2
    s3 = H*w3
    ans = min(ans, max(s1, s2, s3)-min(s1,s2,s3))

    h2 = H//2
    h3 = H-h2
    s2 = (W-w1)*h2
    s3 = (W-w1)*h3
    ans = min(ans, max(s1, s2, s3)-min(s1,s2,s3))

H, W = W, H

for w1 in range(1, W//2+1):
    s1 = H*w1

    w2 = (W-w1)//2
    w3 = W-w1-w2
    s2 = H*w2
    s3 = H*w3

    ans = min(ans, max(s1, s2, s3)-min(s1,s2,s3))

    h2 = H//2
    h3 = H-h2
    s2 = (W-w1)*h2
    s3 = (W-w1)*h3
    ans = min(ans, max(s1, s2, s3)-min(s1,s2,s3))

print(ans)

Solution (Python):
import sys

H, W = map(int, sys.stdin.readline().split())

def minimize(H, W):
    if H % 3 == 0:
        return 0
    res1 = W
    h = round(H / 3)
    w = W // 2
    block = [h * W, (H - h) * w, (H - h) * (W - w)]
    block.sort()
    res2 = block[-1] - block[0]
    return min(res1, res2)
    
def main():
    return min(minimize(H, W), minimize(W, H))

if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 100000000



h, w = LI()
ans = INF
for i, j in ((h, w), (w, h)):

    for k in range(1, i):
        for l, m in (j, k), (k, j):
            part = [j * (i - k), l // 2 * m, (l - l // 2) * m]
            ans = min(ans, max(part) - min(part))



print(ans)

Solution (Python):
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()
ans = H * W
for i in range(2):
    for h in range(1, H):
        s = [h * W, W // 2 * (H - h), (W - W // 2) * (H - h)]
        s2 = [h * W, (H - h) // 2 * W, ((H - h) - (H - h) // 2) * W]
        ans = min(ans, min(max(s) - min(s), max(s2) - min(s2)))
    H, W = W, H
print(ans)

Solution (Python):

import math
H,W = map(int, input().split())







devided = [[1,1],[1,1],[1,1]]
ans = H * W

def devision(dividend,divisor): 
    over = math.ceil(dividend/divisor)
    under = dividend//divisor
    
    if over == under:
        return over
    else:
        if abs(dividend/divisor - over) < abs(dividend/divisor - under):
            return over
        else:
            return under

        
def calc_ans(list_of_devided):   
    a = devided[0][0] * devided[0][1]
    b = devided[1][0] * devided[1][1]
    c = devided[2][0] * devided[2][1]
    
    
    if 0 in [a,b,c]:
        
        return H*W
    else:
        
        return max(a,b,c) - min(a,b,c)


devided[0][1] = W
devided[1][1] = W
devided[2][1] = W
devided[0][0] = devision(H,3)
devided[1][0] = devision(H - devided[0][0],2)
devided[2][0] = H - devided[0][0] - devided[1][0]
if ans > calc_ans(devided):
    ans = calc_ans(devided)
    

devided[0][1] = devision(W,3)
devided[1][1] = devision(W - devided[0][1],2)
devided[2][1] = W - devided[0][1] - devided[1][1]
devided[0][0] = H
devided[1][0] = H
devided[2][0] = H
if ans > calc_ans(devided):
    ans = calc_ans(devided)
    

devided[0][1] = devision(W,3)
devided[0][0] = H
devided[1][1] = W - devided[0][1]
devided[2][1] = W - devided[0][1]
devided[1][0] = devision(H,2)
devided[2][0] = H - devided[1][0]
if ans > calc_ans(devided):
    ans = calc_ans(devided)
    

devided[0][1] = W
devided[0][0] = devision(H,3)
devided[1][0] = H - devided[0][0]
devided[2][0] = H - devided[0][0]
devided[1][1] = devision(W,2)
devided[2][1] = W - devided[1][1]
if ans > calc_ans(devided):
    ans = calc_ans(devided)

print(ans)

Solution (Python):
import sys
import math
h,w=map(int,input().split())
x=h
y=w

ans=x*y

for i in range(1,x):
    x1=i
    
    x2=(x-x1)//2
    x3=x-x1-x2
    s1=x1*y
    s2=x2*y
    s3=x3*y
    a_yoko=max(s1,s2,s3)-min(s1,s2,s3)
    
    y2=y//2
    y3=y-y2
    x2=x-x1
    x3=x2
    y1=y
    
    s1=x1*y1
    s2=x2*y2
    s3=x3*y3
    a_tate=max(s1,s2,s3)-min(s1,s2,s3)
    
    ans=min(a_yoko,a_tate,ans)

x=w
y=h
 
for i in range(1,x):
    x1=i
    
    x2=(x-x1)//2
    x3=x-x1-x2
    s1=x1*y
    s2=x2*y
    s3=x3*y
    a_yoko=max(s1,s2,s3)-min(s1,s2,s3)
    
    y2=y//2
    y3=y-y2
    x2=x-x1
    x3=x2
    y1=y
    
    s1=x1*y1
    s2=x2*y2
    s3=x3*y3
    a_tate=max(s1,s2,s3)-min(s1,s2,s3)
    
    ans=min(a_yoko,a_tate,ans)
    
print(ans)

Solution (Python):
h, w = map(int, input().split())

ans = float('inf')
if h % 3 == 0:
    ans = min(ans, 0)
else:
    ans = min(ans, w)
if w % 3 == 0:
    ans = min(ans, 0)
else:
    ans = min(ans, h)

for i in range(1, w):
    s1 = h // 2 * i
    s2 = (h - h // 2) * i
    s3 = h * (w - i)
    s = sorted([s1, s2, s3])
    ans = min(ans, s[-1] - s[0])

for i in range(1, w):
    s1 = h * i
    s2 = (h // 2) * (w - i)
    s3 = (h - h // 2) * (w - i)
    s = sorted([s1, s2, s3])
    ans = min(ans, s[-1] - s[0])

for i in range(1, h):
    s1 = w // 2 * i
    s2 = (w - w // 2) * i
    s3 = w * (h - i)
    s = sorted([s1, s2, s3])
    ans = min(ans, s[-1] - s[0])

for i in range(1, h):
    s1 = w  * i
    s2 = (w // 2) * (h - i)
    s3 = (w - w // 2) * (h - i)
    s = sorted([s1, s2, s3])
    ans = min(ans, s[-1] - s[0])

print(ans)

Solution (Python):
h,w=map(int,input().split())
ans=50000
for a,b in [(w,h),(h,w)]:
    for i in range(b):
        for c,d in [(a,b-i), (b-i,a)]:
            l=[i*a,c*(d//2),c*(d-d//2)]
            ans=min(ans,max(l)-min(l))
print(ans)

Solution (Python):
h, w = map(int, input().split())
import sys

def yokowari(h, w):
  min_dfr = 10**10
  for h_wareme in range(1, h):
    rest_h = h - h_wareme
    rest_w = w
    if rest_h % 2 == 0 or rest_w % 2 == 0:
      rest_s1 = rest_h * rest_w // 2
      rest_s2 = rest_h * rest_w // 2
    elif rest_w >= rest_h:
      rest_s1 = (rest_w // 2) * rest_h
      rest_s2 = (rest_w // 2 + 1) * rest_h
    else:
      rest_s1 = (rest_h // 2) * rest_w
      rest_s2 = (rest_h // 2 + 1) * rest_w
    s = h_wareme * w
    if s == 0 or rest_s1 == 0 or rest_s2 == 0:
      continue
    dfr = max(s, rest_s1, rest_s2) - min(s, rest_s1, rest_s2)
    min_dfr = min(min_dfr, dfr)

  return min_dfr

"""
def yokowari(h, w):
  o_th = h * w / 3
  print(o_th, h, o_th / h)
  h_wareme = round(o_th / h)
  rest_h = h - h_wareme
  rest_w = w
  print("", rest_h, rest_w)
  if rest_h % 2 == 0 or rest_w % 2 == 0:
    rest_s = rest_h * rest_w // 2
  elif rest_w >= rest_h:
    rest_s = (resr_w // 2) * rest_h
  else:
    rest_s = (resr_h // 2) * rest_w
  dfr = h_wareme * w - rest_s
  print(h_wareme * w, rest_s ,dfr)
  print()
  return abs(dfr)
"""
if h % 3 == 0 or w % 3 == 0:
  print(0)
  sys.exit()
else:
  print(min(yokowari(h, w), yokowari(w, h)))

Solution (Python):
H,W=map(int,input().split())

import sys
if H*W%3==0:
    print(0)
    sys.exit()

ans=min(H,W)  

for i in range(1,H):
    a=i*W
    b=(H-i)*(W//2)
    c=(H-i)*(-(-W//2))
    m=min([a,b,c])
    M=max([a,b,c])
    ans=min(ans,M-m)

for i in range(1,W):
    a=i*H
    b=(W-i)*(H//2)
    c=(W-i)*(-(-H//2))
    m=min([a,b,c])
    M=max([a,b,c])
    ans=min(ans,M-m)

print(ans)

Solution (Python):
h,w=list(map(int,input().split()))

anses=[]
for x,y in (h,w),(w,h):
  if x%3==0:
    anses.append(0)
  else:
    anses.append(y)

  for i in range(x):
    ss = (i * y, (x - i) * (y // 2), (x - i) * (y - y // 2))
    anses.append(max(ss)-min(ss))


print(min(anses))

Solution (Python):
H,W = map(int, input().split())

ans = H*W
if H%3 == 0 or W%3 == 0:
    ans = 0
else:
    h = H//2+min(1, H%2)
    for i in range(1, W):
        ans = min(ans, max(H*i, h*(W-i))-min(H*i, (H-h)*(W-i)))
    w = W//2+min(1, W%2)
    for i in range(1, H):
        ans = min(ans, max(W*i, w*(H-i))-min(W*i, (W-w)*(H-i)))
        
    ans = min(ans, H, W)
            
print(ans)

Solution (Python):
H, W = map(int, input().split())

mi = min(H, W)
if H % 3 == 0 or W % 3 == 0:
    mi = 0

for i in range(H):
    a, b, c = W * i, W//2 * (H-i), (W - W//2) * (H-i)
    mi = min(mi, max(a, b, c) - min(a, b, c))

for i in range(W):
    a, b, c = H * i, H//2 * (W-i), (H - H//2) * (W-i)
    mi = min(mi, max(a, b, c) - min(a, b, c))

print(mi)

Solution (Python):

import sys

H, W = map(int, input().split())

if H % 3 == 0 or W % 3 == 0:
	print(0)
	sys.exit()

kai_1 = min(H,W)

def solve_2(a,b):
	w = a // 2
	s_l = (a-w) * b
	s_s = w * b
	s_r = 0

	tmp = s_l

	for i in range(b):
		s_r += a
		s_s -= w
		s_l -= (a-w)

		sa = max(s_r,s_s,s_l) - min(s_r,s_s,s_l)

		if tmp > sa:
			tmp = sa
			continue
		else:
			return tmp

kai_2 = solve_2(H,W)
kai_3 = solve_2(W,H)

print(min(kai_1,kai_2,kai_3))

Solution (Python):
h, w = map(int, input().split())

s_org = h * w

answer = h * w
for i in range(2):
  if i == 1:
    h, w = w, h
  for height in range(1, h+1):
    s1 = height * w
    remain_height = h - height
    s2 = remain_height * int(w / 2)
    s3 = s_org - (s1 + s2)
    s_max = max([s1, s2, s3])
    s_min = min([s1, s2, s3])
    answer = min([answer, (s_max - s_min)])

    s1 = height * w
    s2 = int((h - height) / 2) * w
    s3 = s_org - (s1 + s2)
    s_max = max([s1, s2, s3])
    s_min = min([s1, s2, s3])
    answer = min([answer, (s_max - s_min)])
  
print(answer)

Solution (Python):
from math import floor, ceil

H,W = map(int,input().split())
ans = float('inf')

if H%3==0 or W%3==0:
  print(0)
  exit()

for x in range(1,H):
  S = [W*x, (H-x)*floor(W/2), (H-x)*ceil(W/2)]
  ans = min(ans, max(S)-min(S))

for x in range(1,W):
  S = [H*x, (W-x)*floor(H/2), (W-x)*ceil(H/2)]
  ans = min(ans, max(S)-min(S))

print(min(ans,H,W))

Solution (Python):
def examC(inf):
    L = LI()
    ans = inf
    for i in range(4):
        if i==0:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[0] // 3
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[0] // 3+1
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
        elif i==1:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[0] // 3
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[0] // 3+1
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
        elif i==2:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[1] // 3
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[1] // 3+1
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
        else:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[1] // 3
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[1] // 3+1
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
    print(ans)

import sys
import copy
import bisect
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC(inf)

Solution (Python):
def examC(inf):
    L = LI()
    ans = inf
    for i in range(4):
        if i==0:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[0] // 3
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[0] // 3+1
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
        elif i==1:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[0] // 3
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[0] // 3+1
                    ansC.append(L[1] * cut)
                    L2 = [ L[0] - cut,L[1]]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
        elif i==2:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[1] // 3
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[1] // 3+1
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[0] // 2
                    ansC.append(L2[1] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
        else:
            for j in range(2):
                ansC = []
                if j==0:
                    cut = L[1] // 3
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
                else:
                    cut = L[1] // 3+1
                    ansC.append(L[0] * cut)
                    L2 = [ L[0],L[1]-cut]
                    cut = L2[1] // 2
                    ansC.append(L2[0] * cut)
                    ansC.append(L[1]*L[0] - sum(ansC))
                    ans = min(ans, max(ansC) - min(ansC))
    print(ans)

import sys
import copy
import bisect
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC(inf)

Solution (Python):
import sys

H, W = map(int, input().split())

if H % 3 == 0 or W % 3 == 0:
	print(0)
	sys.exit()

kai_1 = min(H,W)

def solve_2(a,b):
	w = a // 2
	s_l = (a-w) * b
	s_s = w * b
	s_r = 0

	tmp = s_l

	for i in range(b):
		s_r += a
		s_s -= w
		s_l -= (a-w)

		sa = max(s_r,s_s,s_l) - min(s_r,s_s,s_l)

		if tmp > sa:
			tmp = sa
			continue
		else:
			return tmp

kai_2 = solve_2(H,W)
kai_3 = solve_2(W,H)

print(min(kai_1,kai_2,kai_3))

Solution (Python):

def main():
    H, W = map(int, input().split())
    ans = float('inf')
    for h in range(1, H):
        S1 = h*W
        S2 = ((H-h)//2) * W
        S3 = ((H-h+1)//2) * W
        dif = max(S1, S2, S3) - min(S1, S2, S3)
        ans = min(ans, dif)
        S2 = (H-h) * (W//2)
        S3 = (H-h) * ((W+1)//2)
        dif = max(S1, S2, S3) - min(S1, S2, S3)
        ans = min(ans, dif)
    tmp = H
    H = W
    W = tmp
    for h in range(1, H):
        S1 = h*W
        S2 = ((H-h)//2) * W
        S3 = ((H-h+1)//2) * W
        dif = max(S1, S2, S3) - min(S1, S2, S3)
        ans = min(ans, dif)
        S2 = (H-h) * (W//2)
        S3 = (H-h) * ((W+1)//2)
        dif = max(S1, S2, S3) - min(S1, S2, S3)
        ans = min(ans, dif)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    h, w = list(map(int, readline().split()))
    if h % 3 == 0 or w % 3 == 0:
        print(0)
    else:
        mt = 10 ** 20
        for i in range(h):
            y = i * w
            x = (h - i) * (w // 2)
            z = h * w - x - y
            mt = min(mt, abs(max(x, y, z) - min(x, y, z)))

            x = (h - i) // 2 * w
            z = h * w - x - y
            mt = min(mt, abs(max(x, y, z) - min(x, y, z)))

        for i in range(w):
            y = i * h
            x = (w - i) * (h // 2)
            z = h * w - x - y
            mt = min(mt, abs(max(x, y, z) - min(x, y, z)))

            x = (w - i) // 2 * h
            z = h * w - x - y
            mt = min(mt, abs(max(x, y, z) - min(x, y, z)))

        print(mt)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    [H, W] = string_to_int(inputs[0])
    IDEAL_SIZE = H * W / 3

    def separate_first(SH, SW):
        most_closed_size = None
        most_closed_i = None
        for h in range(1, SH):
            tmp_size = h * SW
            if most_closed_size is None:
                most_closed_size = tmp_size
                most_closed_i = h
            else:
                most_closed_diff = abs(IDEAL_SIZE - most_closed_size)
                tmp_diff = abs(IDEAL_SIZE - tmp_size)

                if most_closed_diff > tmp_diff:
                    most_closed_size = tmp_size
                    most_closed_i = h

        return (most_closed_i, most_closed_size)

    def separate_second_row(start_h, SH, SW):
        second_i = (SH - start_h) // 2 + start_h
        second_s = (second_i - start_h) * SW
        third_s = (SH - second_i) * SW
        return (second_i, second_s, third_s)

    def separate_second_column(start_h, SH, SW):
        second_i = SW // 2
        second_s = (SH - start_h) * second_i
        third_s = (SH - start_h) * (SW - second_i)
        return (second_i, second_s, third_s)

    def max_diff(f_s, s_s, t_s):
        diff_s = []
        diff_s.append(abs(f_s - s_s))
        diff_s.append(abs(f_s - t_s))
        diff_s.append(abs(s_s - t_s))

        return max(diff_s)

    diffs = []
    if H == 1:
        f_i, f_s = separate_first(W, H)
        s_i, s_s, t_s = separate_second_row(f_i, W, H)
        diffs.append(max_diff(f_s, s_s, t_s))
    elif W == 1:
        f_i, f_s = separate_first(W, H)
        s_i, s_s, t_s = separate_second_row(f_i, W, H)
        diffs.append(max_diff(f_s, s_s, t_s))
    else:
        f_i, f_s = separate_first(H, W)
        s_i, s_s, t_s = separate_second_row(f_i, H, W)
        diffs.append(max_diff(f_s, s_s, t_s))

        f_i, f_s = separate_first(W, H)
        s_i, s_s, t_s = separate_second_row(f_i, W, H)
        diffs.append(max_diff(f_s, s_s, t_s))

        f_i, f_s = separate_first(H, W)
        s_i, s_s, t_s = separate_second_column(f_i, H, W)
        diffs.append(max_diff(f_s, s_s, t_s))

        f_i, f_s = separate_first(W, H)
        s_i, s_s, t_s = separate_second_column(f_i, W, H)
        diffs.append(max_diff(f_s, s_s, t_s))
    return min(diffs)


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

Solution (Python):
def pat1(H,W):
    if H % 3 == 0:
        return 0
    else:
        return W

def pat2(H,W):
    if W % 3 == 0:
        return 0
    else:
        return H

def pat3(H,W):
    h1 = H // 2
    h2 = H - h1
    ans = 10**15 
    for w in range(1,W//2+1):
        s1 = h1 * (W-w)
        s2 = h2 * (W-w)
        s3 = H*w
        min1 = min(s1,s2,s3)
        max1 = max(s1,s2,s3)
        ans = min(ans,max1-min1)
    return ans

def pat4(H,W):
    w1 = W // 2
    w2 = W - w1
    ans = 10**15
    for h in range(1,H//2+1):
        s1 = w1*(H-h)
        s2 = w2*(H-h)
        s3 = W*h
        min1 = min(s1,s2,s3)
        max1 = max(s1,s2,s3)
        ans = min(ans,max1-min1)
    return ans

H,W = map(int,input().split())
ans = 10**15
ans = min(ans,pat1(H,W))
ans = min(ans,pat2(H,W))
ans = min(ans,pat3(H,W))
ans = min(ans,pat4(H,W))

print(ans)

Solution (Python):
H, W = list(map(int,input().split()))
ans = H*W
h = H
w = W
for i in range(h//3,h//2+1):
    p1 = i*w
    for j in range((h-i)//2-1,(h-i)//2+1):
        p2 = (h-i-j)*W
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    for j in range(w//2-1,w//2+1):
        p2 = (h-i)*(w-j)
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
h = W
w = H
for i in range(h//3,h//2+1):
    p1 = i*w
    for j in range((h-i)//2-1,(h-i)//2+1):
        p2 = (h-i-j)*H
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    for j in range(w//2-1,w//2+1):
        p2 = (h-i)*(w-j)
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
print(ans)

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
else:
    if w%2==0:
        a=w*(h//3)
        b=(h-h//3)*(w//2)
        c=w*(h//3+1)
        d=(h-h//3-1)*(w//2)
    else:
        a=w*(h//3)
        b=(h-h//3)*(w//2+1)
        c=w*(h//3+1)
        d=(h-h//3-1)*(w//2)
    ans=min(b-a,c-d,h,w)
    if h%2==0:
        a=h*(w//3)
        b=(w-w//3)*(h//2)
        c=h*(w//3+1)
        d=(w-w//3-1)*(h//2)
    else:
        a=h*(w//3)
        b=(w-w//3)*(h//2+1)
        c=h*(w//3+1)
        d=(w-w//3-1)*(h//2)
    ans=min(ans,b-a,c-d)
    print(ans)

Solution (Python):
def a(n,m):
	l1=n//3*2+1
	l2=n-l1
	l3=m//2
	l4=m-l3
	A=[l2*m,l1*l3,l1*l4]
	r=max(A)-min(A)
	if n>3:
		l5=l1//2
		l6=l1-l5
		B=[l2*m,l5*m,l6*m]
		r=min(r,max(B)-min(B))
	return r
h,w=map(int,input().split())
if h%3==0 or w%3==0:
	print(0)
else:
	print(min(a(h,w),a(w,h)))

Solution (Python):
import sys
input = sys.stdin.readline

h, w = [int(x) for x in input().split()]

if h % 3 == 0 or 2 % 3 == 0:
    print(0)
    exit()

if h % 3 == 1:
    d = h // 3 
else:
    d = h // 3 + 1
block = [w * d, (h-d) * (w // 2), (h-d) * (w - w // 2)]
res1 = max(block) - min(block)
block = [w * d, w * d, w * (h - d * 2)]
res2 = max(block) - min(block)

if w % 3 == 1:
    d = w // 3 
else:
    d = w // 3 + 1
block = [h * d, (w-d) * (h // 2), (w-d) * (h - h // 2)]
res3 = max(block) - min(block)
block = [h * d, h * d, h * (w - d * 2)]
res4 = max(block) - min(block)

print(min(res1, res2, res3, res4))

Solution (Python):
INF = float('inf')


def f(H, W):
    return min(f1(H, W), f1(W, H))


def f1(H, W):
    result = INF
    for h in range(1, H):
        t = [h * W] + f2(H - h, W)
        t.sort()
        if abs(t[0] - t[2]) < result:
            result = abs(t[0] - t[2])
        t = [h * W] + f2(W, H - h)
        t.sort()
        if abs(t[0] - t[2]) < result:
            result = abs(t[0] - t[2])
    return result


def f2(H, W):
    if H < 2:
        return [-INF, INF]
    h = H // 2
    return [h * W, (H - h) * W]


H, W = map(int, input().split())

print(f(H, W))

Solution (Python):

H, W = map(int, input().split())
 
if H*W %3 ==0:
    print(0)
    exit()
 
diff_list = [W,H]
 

split1 = [W//3, W//3 + 1]
split2 = [H//2, H//2 +1]
 
for w in split1:
    for h in split2:
        temp = [H*w, h*(W-w), (H-h)*(W-w)]
        diff_list.append(abs(max(temp)-min(temp)))
 

if H != W:
    split1 = [H//3, H//3 + 1]
    split2 = [W//2, W//2 + 1]
 
    for h in split1:
        for w in split2:
            temp = [h*W, (H-h)*w, (H-h)*(W-w)]
            diff_list.append(abs(max(temp)-min(temp)))
 

print(min(diff_list))

Solution (Python):

h,w=map(int,input().split())


if h%3==0 or w%3==0:
    print(0)
else:
    l=[]
    
    l.append(h)
    
    l.append(w)
    
    for i in range(1,w):
        x,y,z=sorted([i*h,(w-i)*(h//2),(w-i)*(h-h//2)])
        l.append(z-x)
    
    for i in range(1,h):
        x,y,z=sorted([i*w,(h-i)*(w//2),(h-i)*(w-w//2)])
        l.append(z-x)
    print(min(l))

Solution (Python):
h,w=map(int,input().split())

ans=h*w

if h%3==0:
    ans=0
else:
    ans=w
if w%3==0:
    ans=0
else:
    ans=min(ans,h)


for i in range(1,h):
    if w%2==0:
        l=[i*w,(h-i)*(w//2),(h-i)*(w//2)]
        ans=min(ans,max(l)-min(l))
    else:
        l=[i*w,(h-i)*(w//2+1),(h-i)*(w//2)]
        ans=min(ans,max(l)-min(l))

for i in range(1,w):
    if h%2==0:
        l=[i*h,(w-i)*(h//2),(w-i)*(h//2)]
        ans=min(ans,max(l)-min(l))
    else:
        l=[i*h,(w-i)*(h//2+1),(w-i)*(h//2)]
        ans=min(ans,max(l)-min(l))

print(ans)

Solution (Python):
H,W=map(int,input().split())
ans=min((H%3>0)*W,(W%3>0)*H)
A=[0]*3
A[0]=H//3*W
A[1]=(H-H//3)*(W//2)
A[2]=H*W-A[0]-A[1]
A.sort()
ans=min(ans,A[2]-A[0])
A[0]=(H//3+1)*W
A[1]=(H-H//3-1)*(W//2)
A[2]=H*W-A[0]-A[1]
A.sort()
ans=min(ans,A[2]-A[0])
H,W=W,H
A[0]=H//3*W
A[1]=(H-H//3)*(W//2)
A[2]=H*W-A[0]-A[1]
A.sort()
ans=min(ans,A[2]-A[0])
A[0]=(H//3+1)*W
A[1]=(H-H//3-1)*(W//2)
A[2]=H*W-A[0]-A[1]
A.sort()
ans=min(ans,A[2]-A[0])
print(ans)

Solution (Python):
def func(H,W):
    if H % 3 == 0:
        return 0

    H_3 = int(H/3)
    S1 = H_3 * W
    S2 = (H - H_3) * int(W/2)
    S3 = H * W - S1 - S2 
    
    res1 = max(S1, S2, S3) - min(S1, S2, S3)
    
    H_3_1 = int(H/3) + 1
    S1 = H_3_1 * W
    S2 = (H - H_3_1) * int(W/2)
    S3 = H * W - S1 - S2 
    
    res2 = max(S1, S2, S3) - min(S1, S2, S3)
    
    return min(res1 ,res2)
H, W = list(map(int, input().split()))
print(min(func(H,W), func(W,H), H, W))

Solution (Python):

si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

h,w = nm()

if h==2 and w==2:
    print(1)
    exit()

s = h*w
s1 = 0
s2 = 0
s3 = 0
wm = w//2
hm = h//2
s_max = 0
s_min = 0
ans_1 = 10**10
ans_2 = 10**10
ans_3 = 10**10
ans_4 = 10**10

for i in range(1,h-1):
    s1 = i*w
    s2 = ((h-i)//2)*w
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_1 = min(ans_1,s_max-s_min)

for i in range(1,h-1):
    s1 = i*w
    s2 = (h-i)*wm
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_2 = min(ans_2,s_max-s_min)

for i in range(1,w-1):
    s1 = i*h
    s2 = ((w-i)//2)*h
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_3 = min(ans_3,s_max-s_min)

for i in range(1,w-1):
    s1 = i*h
    s2 = (w-i)*hm
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_4 = min(ans_4,s_max-s_min)

print(min(ans_1,ans_2,ans_3,ans_4))

Solution (Python):
def func(H,W):
    area = H * W

    half_H = int(H/2)
    half_W = int(W/2)  
    
    res = 10000000
    
    
    
    for x in range(1,W):
        S1 = H * x
        S2_T = (W - x) * half_H
        S3_T = area - S1 - S2_T
        
        S2_2 = H * int((W - x)/2)
        S3_2 = area - S1 - S2_2
        
        tmpres_T = max(S1, S2_T, S3_T) - min(S1, S2_T, S3_T)
        tmpres_2 = max(S1, S2_2, S3_2) - min(S1, S2_2, S3_2)
        
        if tmpres_T < res:
            res = tmpres_T
        if tmpres_2 < res:
            res = tmpres_2
    
    
    for y in range(1,H):
        S1 = W * y
        S2_T = (H - y) * half_W
        S3_T = area - S1 - S2_T
        
        S2_2 = W * int((H - y)/2)
        S3_2 = area - S1 - S2_2
        
        tmpres_T = max(S1, S2_T, S3_T) - min(S1, S2_T, S3_T)
        tmpres_2 = max(S1, S2_2, S3_2) - min(S1, S2_2, S3_2)
        
        if tmpres_T < res:
            res = tmpres_T
        if tmpres_2 < res:
            res = tmpres_2
    
    return res

H, W = list(map(int, input().split()))
print(func(H,W))