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=map(int,input().split())

if H%3!=0 and W%3!=0:
  a=min(H,W)
else:
  print(0)
  exit()
b=H*W
for i in range(1,H):
  x=W*i
  if W%2==0:
    y=(H*W-x)//2
    z=y
  else:
    y=((H*W-x)+(H-i))//2
    z=y-(H-i)
  A=max(x,y,z)-min(x,y,z)
  b=min(A,b)

c=H*W
for i in range(1,W):
  x=H*i
  if H%2==0:
    y=(H*W-x)//2
    z=y
  else:
    y=((H*W-x)+(W-i))//2
    z=y-(W-i)
  B=max(x,y,z)-min(x,y,z)
  c=min(B,c)
print(min(a,b,c))

Solution (Python):
h, w = map(int, input().split())
def row_cut(p,q):
    res = 10**5
    for i in range(p):
        a = i * q
        b = ((p - i) // 2) * q
        c = (p * q) - a - b
        res = min(res, max(a,b,c) - min(a,b,c))
    return res 
def row_column_cut(p,q):
    res = 10**5
    for i in range(p):
        a = i * q
        b = (q // 2) * (p - i)
        c = (p * q) - a - b
        res = min(res, max(a,b,c) - min(a,b,c))
    return res
cand = [row_cut(h,w), row_cut(w,h), row_column_cut(w,h), row_column_cut(h,w)]
print(min(cand))

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

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

cand = []
for i in range(1, H+1):
    s_1 = i*W
    s_2_hor = (H-i)//2*W
    s_3_hor = (H-(i+(H-i)//2))*W
    s_2_ver = (H-i)*(W//2)
    s_3_ver = (H-i)*(W-W//2)
    ans_hor = abs(max(s_1, s_2_hor, s_3_hor) - min(s_1, s_2_hor, s_3_hor))
    ans_ver = abs(max(s_1, s_2_ver, s_3_ver) - min(s_1, s_2_ver, s_3_ver))
    cand.append(ans_hor)
    cand.append(ans_ver)

H, W = W, H
for i in range(1, H+1):
    s_1 = i*W
    s_2_hor = (H-i)//2*W
    s_3_hor = (H-(i+(H-i)//2))*W
    s_2_ver = (H-i)*(W//2)
    s_3_ver = (H-i)*(W-W//2)
    ans_hor = abs(max(s_1, s_2_hor, s_3_hor) - min(s_1, s_2_hor, s_3_hor))
    ans_ver = abs(max(s_1, s_2_ver, s_3_ver) - min(s_1, s_2_ver, s_3_ver))
    cand.append(ans_hor)
    cand.append(ans_ver)


print(min(cand))

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

def three(h,w):
    ans = h*w
    for i in range(h//3,h//3+2):
        A = w*i
        B = w*((h-i)//2)
        C = w*((h-i+1)//2)
        ans = min(ans,max(A,B,C)-min(A,B,C))
    return ans
def onetwo(h,w):
    ans = h*w
    for i in range(h//3,h//3+2):
        A = w*i
        B = (h-i)*(w//2)
        C = (h-i)*((w+1)//2)
        ans = min(ans,max(A,B,C)-min(A,B,C))
    return ans
print(min(three(H,W),three(W,H),onetwo(H,W),onetwo(W,H)))

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

def three(h,w):
    ans = h*w
    for i in range(1,h):
        A = w*i
        B = w*((h-i)//2)
        C = w*((h-i+1)//2)
        ans = min(ans,max(A,B,C)-min(A,B,C))
    return ans
def onetwo(h,w):
    ans = h*w
    for i in range(1,h):
        A = w*i
        B = (h-i)*(w//2)
        C = (h-i)*((w+1)//2)
        ans = min(ans,max(A,B,C)-min(A,B,C))
    return ans
print(min(three(H,W),three(W,H),onetwo(H,W),onetwo(W,H)))

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    H, W = [int(x) for x in input().split()]

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

    if H % 2 == 0 and W % 2 == 0:
        print(min(H, W) // 2)
        return

    ans = min(H, W)
    ans2 = float("inf")
    for i in range(1, H // 2 + 2):
        a = i * W
        bc = H * W - a
        b = (W // 2) * (H - i)
        c = bc - b
        ma = max(a, b, c)
        mi = min(a, b, c)
        ans2 = min(ans2, ma - mi)

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

    print(min(ans, ans2))

if __name__ == '__main__':
    main()

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


def calc(h,w):
    minv = 2 ** 32
    for i in range(1, h):
        s1 = i * w
        s2 = ((h-i) // 2) * w
        s3 = (h - i - ((h-i) // 2)) * w
        lmin = max(s1, s2, s3) - min(s1, s2, s3)
        minv = min(minv, lmin)

        s2v = (h - i) * (w // 2)
        s3v = (h - i) * (w - w // 2)
        minv = min(minv, max(s1, s2v, s3v) - min(s1, s2v, s3v))
    return minv

m = min(calc(H, W), calc(W, H))
print(m)

Solution (Python):

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

MIN = float("inf")
for h in range(H):
    s1 = (h+1)*W
    s2 = W*((H-(h+1))//2)
    s3 = whole - (s1+s2)
    s4 = (H-(h+1))*(W//2)
    s5 = whole - (s1+s4)
    kari1 = max({s1,s2,s3})-min({s1,s2,s3})
    kari2 = max({s1,s4,s5})-min({s1,s4,s5})
    MIN = min({kari1,kari2,MIN})
for w in range(W):
    s1 = (w+1)*H
    s2 = H*((W-(w+1))//2)
    s3 = whole - (s1+s2)
    s4 = (W-(w+1))*(H//2)
    s5 = whole - (s1+s4)
    kari1 = max({s1,s2,s3})-min({s1,s2,s3})
    kari2 = max({s1,s4,s5})-min({s1,s4,s5})
    MIN = min({kari1,kari2,MIN})
    
print(MIN)

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

hwList = []
hhList = []
whList = []
wwList = []
for i in range(1,h+1):
  sa = i*w
  sb = (h-i)*(w//2)
  sc = (h-i)*(w-(w//2))
  hwList.append(max([sa,sb,sc])-min([sa,sb,sc]))
  sb = (h-i)//2*(w)
  sc = ((h-i)-((h-i)//2))*(w)
  hhList.append(max([sa,sb,sc])-min([sa,sb,sc]))
for i in range(1,w+1):
  sa = i*h
  sb = (w-i)*(h//2)
  sc = (w-i)*(h-(h//2))
  whList.append(max([sa,sb,sc])-min([sa,sb,sc]))
  sb = (w-i)//2*(h)
  sc = ((w-i)-((w-i)//2))*(h)
  wwList.append(max([sa,sb,sc])-min([sa,sb,sc]))

print(min([min(hwList),min(hhList),min(whList),min(wwList)]))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

ans = INF

for h in range(1, H+1):
	S1 = h*W
	S2 = (H-h)*(W//2)
	S3 = H*W-S1-S2
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

	S2 = (H-h)//2*W
	S3 = H*W-S1-S2
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

H, W = W, H

for h in range(1, H+1):
	S1 = h*W
	S2 = (H-h)*(W//2)
	S3 = H*W-S1-S2
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

	S2 = (H-h)//2*W
	S3 = H*W-S1-S2
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

ans = INF

for h in range(1, H+1):
	S1 = h*W
	w = W//2
	S2 = (H-h)*w
	S3 = (H-h)*(W-w)
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

	S2 = (H-h)//2*W
	S3 = H*W-S1-S2
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

H, W = W, H

for h in range(1, H+1):
	S1 = h*W
	w = W//2
	S2 = (H-h)*w
	S3 = (H-h)*(W-w)
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

	S2 = (H-h)//2*W
	S3 = H*W-S1-S2
	ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))

print(ans)

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()

ans = min(h,w)

for i in range(2):
    tmp = h//3 + i
    p = [0]*3
    p[0] = tmp*w
    p[1],p[2] = (h-tmp)*(w//2), (h-tmp)*((w+1)//2)
    p.sort()
    ans = min(ans,p[2]-p[0])

    tmp = w//3 + i
    p = [0]*3
    p[0] = tmp*h
    p[1],p[2] = (w-tmp)*(h//2), (w-tmp)*((h+1)//2)
    p.sort()
    ans = min(ans,p[2]-p[0])
print(ans)

Solution (Python):
H, W = map(int, input().split())
ans = min(H, W)
if H % 3 == 0 or W % 3 == 0:
    ans = 0
else:
    for h in range(1, H):
        s1 = h * W
        s2 = (H - h) * (W // 2)
        s3 = (H - h) * ((W + 1)// 2)
        ans = min(ans, max(s1,s2,s3) - min(s1,s2,s3))
    for w in range(1, W):
        s1 = w * H
        s2 = (W - w) * (H // 2)
        s3 = (W - w) * ((H + 1)// 2)
        ans = min(ans, max(s1,s2,s3) - min(s1,s2,s3))
print(ans)

Solution (Python):
H,W=map(int,input().split())
if H%3==0 or W%3==0:
  print(0)
  exit(0)

e=[0]*2
e[0]=max(H,W)
e[1]=min(H,W)

ans=H*W
for i in range(2):
  a=e[i]
  b=e[i^1]
  
  a2=[0]*2
  a2[0]=a//3
  a2[1]=a//3+1
  for j in range(2):
    area=[0]*3
    if a2[j]==0:
      continue
    area[0]=a2[j]*b
    
    a_rest=a-a2[j]
    
    c=[0]*2
    c[0]=max(a_rest,b)
    c[1]=min(a_rest,b)
    for k in range(2):
      p=c[k]
      q=c[k^1]
      if p%2==0:
        area[1]=area[2]=p*q//2
      else:
        
        p2=[0]*2
        p2[0]=p//2
        p2[1]=p//2+1
        for u in range(2):
          if p2[u]==0:
            continue
          area[1]=p2[u]*q
          area[2]=(p-p2[u])*q
      if area[0]<=0 or area[1]<=0 or area[2]<=0:
        continue
      diff=max(area)-min(area)
      if ans>diff:
        ans=diff
print(ans)

Solution (Python):
W,H = map(int,input().split())
ans = 10**30
for i in range(1,W):
    a = H*i
    w = W - i
    b = max((w//2)*H,w*(H//2))
    c = w*H - b
    x = max(a,b,c) - min(a,b,c)
    ans = min(ans,x)
for i in range(1,H):
    a = W*i
    h = H - i
    b = max((W//2)*h,W*(h//2))
    c = h*W - b
    x = max(a,b,c) - min(a,b,c)
    ans = min(ans,x)
print(ans)

Solution (Python):
h,w = map(int,input().split())
if h%3 == 0:
    m1 = 0
else:
    m1 = w
if w%3 == 0:
    m2 = 0
else:
    m2 = h
m3 = m4 = h*w
for i in range(1,h):
    m3 = min(m3, max(i*w, (h-i)*(w//2), (h-i)*(w-w//2)) - min(i*w, (h-i)*(w//2), (h-i)*(w-w//2)))
for i in range(1,w):
    m4 = min(m4, max(i*h, (w-i)*(h//2), (w-i)*(h-h//2)) - min(i*h, (w-i)*(h//2), (w-i)*(h-h//2)))
print(min(m1, m2, m3, m4))

Solution (Python):


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

def calc(main,sub):
    if main%3==1:
        a=main//3
    else:
        a=main//3+1
    b=sub//2
    temp=[a*sub,(main-a)*b,(main-a)*(sub-b)]
    
    return max(temp)-min(temp)
        

if H%3==0 or W%3==0:
    print(0)
else:
    print(min(calc(H,W),calc(W,H),H,W))

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

ans = h*w
for i in range(1,h):
  a = w*i
  b1 = w*((h-i+1)//2)
  c1 = w*((h-i)//2)
  
  ans = min(max(a,b1,c1)-min(a,b1,c1),ans)
  b2 = (h-i)*((w+1)//2)
  c2 = (h-i)*(w//2)
  
  ans = min(max(a,b2,c2)-min(a,b2,c2),ans)
for i in range(1,w):
  a = h*i
  b1 = h*((w-i+1)//2)
  c1 = h*((w-i)//2)
  
  ans = min(max(a,b1,c1)-min(a,b1,c1),ans)
  b2 = (w-i)*((h+1)//2)
  c2 = (w-i)*(h//2)
  
  ans = min(max(a,b2,c2)-min(a,b2,c2),ans)
print(ans)

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


def sdif(h1,w1,h2,w2,h3,w3):
    s1=h1*w1
    s2=h2*w2
    s3=h3*w3
    return max(abs(s1-s2),abs(s2-s3),abs(s3-s1))

if h % 3==0 or w% 3== 0:
    print(0)
else:
    h1=h//3
    w1=w
    h2=h-h1
    w2=w//2
    h3=h2
    w3=w-w2
    r1=sdif(h1,w1,h2,w2,h3,w3)
    h1=h//3+1
    w1=w
    h2=h-h1
    w2=w//2
    h3=h2
    w3=w-w2
    r2=sdif(h1,w1,h2,w2,h3,w3)
    w1=w//3
    h1=h
    w2=w-w1
    h2=h//2
    w3=w2
    h3=h-h2
    r3=sdif(h1,w1,h2,w2,h3,w3)
    w1=w//3+1
    h1=h
    w2=w-w1
    h2=h//2
    w3=w2
    h3=h-h2
    r4=sdif(h1,w1,h2,w2,h3,w3)
    print(min(h,w,r1,r2,r3,r4))

Solution (Python):
def parallel(h, w):
    q, r = divmod(h, 3)
    if r == 0:
        return 0
    else:
        return w


def t(h, w):
    ret = h * w

    q = h // 3
    c = w // 2
    for r in range(q, q + 2):
        set_ = (r * w, (h - r) * c, (h - r) * (w - c))
        ret = min(ret, max(set_) - min(set_))

    return ret


def main():
    h, w = map(int, input().split())
    print(min(parallel(h, w), parallel(w, h), t(h, w), t(w, h)))


if __name__ == '__main__':
    main()

Solution (Python):
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:    
    print(0)
    exit()

answer = float('inf')

h = 1
for _ in range(H):
    a = h * W
    b = (H-h) * (W // 2)
    c = H * W - a - b
    MAX = max([a, b, c])
    MIN = min([a, b, c])
    if MAX - MIN <= answer:
        answer = MAX - MIN
    h += 1

w = 1
for _ in range(W):
    a = w * H
    b = (W-w) * (H // 2)
    c = H * W - a - b
    MAX = max([a, b, c])
    MIN = min([a, b, c])
    if MAX - MIN <= answer:
        answer = MAX - MIN
    w += 1

a = W // 3
b = (W - a) // 2
c = W - a - ((W - a) // 2)
a *= H
b *= H
c *= H
MAX = max([a, b, c])
MIN = min([a, b, c])
if MAX - MIN <= answer:
    answer = MAX - MIN

a = H // 3
b = (H - a) // 2
c = H - a - ((H - a) // 2)
a *= W
b *= W
c *= W
MAX = max([a, b, c])
MIN = min([a, b, c])
if MAX - MIN <= answer:
    answer = MAX - MIN

print(answer)

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

S = H*W
ans = 10**10

hs = H//2
ws = W//2

for h in range(1,hs+1):
    a = h*W
    if (H-h)%2 == 0 or W%2 == 0:
        b = (S-a)//2
    else:
        b = min(H-h,W)*(max(H-h,W)//2)
    c = S-a-b
    d = max(a,b,c)-min(a,b,c)
    ans = min(ans,d)

for w in range(1,ws+1):
    a = H*w
    if (W-w)%2 == 0 or H%2 == 0:
        b = (S-a)//2
    else:
        b = min(W-w,H)*(max(W-w,H)//2)
    c = S-a-b
    d = max(a,b,c)-min(a,b,c)
    ans = min(ans,d)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
mn=float("inf")
for i in range(1,h):
    a=w*(h-i)
    b=i*-(-w//2)
    c=i*(w//2)
    dai=max(a,b,c)
    syo=min(a,b,c)
    
    mn=min(mn,dai-syo)
for i in range(1,w):
    a=h*(w-i)
    b=i*-(-h//2)
    c=i*(h//2)
    dai=max(a,b,c)
    syo=min(a,b,c)
    
    mn=min(mn,dai-syo)
mn=min(mn,h,w)
print(mn)

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

ans = H * W
for ha in range(1, H):
    a = ha * W

    hb = (H - ha) // 2

    b = hb * W
    c = H * W - a - b

    ans = min(ans, max(a, b, c) - min(a, b, c))

    wb = W // 2
    b = (H - ha) * wb
    c = H * W - a - b

    ans = min(ans, max(a, b, c) - min(a, b, c))

for wa in range(1, W):
    a = H * wa

    wb = (W - wa) // 2

    b = H * wb
    c = H * W - a - b

    ans = min(ans, max(a, b, c) - min(a, b, c))

    hb = H // 2
    b = (W - wa) * hb
    c = H * W - a - b

    ans = min(ans, max(a, b, c) - min(a, b, c))

print(ans)

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


tmp = h // 3
a = tmp * w
b = (h-tmp) // 2 * w
c = h*w - a - b
ans = max([a, b, c]) - min([a, b, c])

tmp = (h+2) // 3
a = tmp * w
b = (h-tmp) // 2 * w
c = h*w - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = h // 3
a = tmp * w
b = (h-tmp) * (w//2)
c = h*w - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = (h+2) // 3
a = tmp * w
b = (h-tmp) * (w//2)
c = h*w - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))


tmp = w // 3
a = tmp * h
b = (w-tmp) // 2 * h
c = w*h - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = (w+2) // 3
a = tmp * h
b = (w-tmp) // 2 * h
c = w*h - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = w // 3
a = tmp * h
b = (w-tmp) * (h//2)
c = w*h - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = (w+2) // 3
a = tmp * h
b = (w-tmp) * (h//2)
c = w*h - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

print(ans)

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

ans = 10**10

for _ in range(2):
    for h in range(1, H):
        a = h * W
        b = (H-h)//2 * W
        c = (H - h - (H-h)//2) * W
        d = max(a,b,c) - min(a,b,c)
        ans = min(ans, d)
        b = (H-h) * (W//2)
        c = (H-h) * (W-W//2)
        d = max(a,b,c) - min(a,b,c)
        ans = min(ans, d)
    H, W = W, H

print(ans)

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

ans = 10 ** 10
if (h % 3 == 0) or (w % 3 == 0):
    ans = 0

else:
    if h % 3 == 1:
        Sa = int(h//3) * w
        Sb = int(h//3) * w
        Sc = (int(h//3)+1) * w
        dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
        ans = min(ans, dif)
    elif h % 3 == 2:
        Sa = int(h//3) * w
        Sb = (int(h//3)+1) * w
        Sc = (int(h//3)+1) * w
        dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
        ans = min(ans, dif)
    
    if w % 3 == 1:
        Sa = int(w//3) * h
        Sb = int(w//3) * h
        Sc = (int(w//3)+1) * h
        dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
        ans = min(ans, dif)
    elif w % 3 == 2:
        Sa = int(w//3) * h
        Sb = (int(w//3)+1) * h
        Sc = (int(w//3)+1) * h
        dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
        ans = min(ans, dif)

    if h % 2 == 0:
        for i in range(1,w):
            Sa = int(h/2) * i
            Sc = h * (w-i)
            dif = abs(Sa-Sc)
            ans = min(ans, dif)
    elif h % 2 == 1:
        for i in range(1,w):
            Sa = int(h // 2)  * i
            Sb = (int(h // 2) + 1) * i
            Sc = h * (w-i)
            dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
            ans = min(ans, dif)
        
    if w % 2 == 0:
        for j in range(1,h):
            Sa = int(w/2) * j
            Sc = w * (h-j)
            dif = abs(Sa-Sc)
            ans = min(ans, dif)
    elif w % 2 == 1:
        for j in range(1,h):
            Sa = int(w // 2)  * j
            Sb = (int(w // 2) + 1) * j
            Sc = w * (h-j)
            dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
            ans = min(ans, dif)
print(ans)

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

ans = 10**18

for w in range(1,W):
    first_area = H*w
    if (W-w)%2 == 0:
        second_area = (W-w)*H//2
        third_area = (W-w)*H//2
    else:
        second_area = (W-w)//2*H
        third_area = (1+(W-w)//2)*H
    diff = max(first_area,second_area,third_area)-min(first_area,second_area,third_area)
    if diff < ans:
        ans = diff
    if H%2 == 0:
        second_area = H//2*(W-w)
        third_area = H//2*(W-w)
    else:
        second_area = H//2*(W-w)
        third_area = (H//2+1)*(W-w)
    diff = max(first_area,second_area,third_area)-min(first_area,second_area,third_area)
    if diff < ans:
        ans = diff

for h in range(1,H):
    first_area = W*h
    if (H-h)%2 == 0:
        second_area = (H-h)*W//2
        third_area = (H-h)*W//2
    else:
        second_area = (H-h)//2*W
        third_area = (1+(H-h)//2)*W
    diff = max(first_area,second_area,third_area)-min(first_area,second_area,third_area)
    if diff < ans:
        ans = diff

    if W%2 == 0:
        second_area = W//2*(H-h)
        third_area = W//2*(H-h)
    else:
        second_area = W//2*(H-h)
        third_area = (W//2+1)*(H-h)
    diff = max(first_area,second_area,third_area)-min(first_area,second_area,third_area)
    if diff < ans:
        ans = diff
print(ans)

Solution (Python):
hight, width = map(int, input().split())

ans = 10 ** 18


def F(H, W):
    global ans
    for h in range(1, H):
        sa = h * W
        
        h2 = (H - h) // 2
        sb = h2 * W
        sc = (H - h - h2) * W
        ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))

        
        w = W // 2
        sb = (H - h) * w
        sc = (H - h) * (W - w)
        ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))


F(hight, width)
F(width, hight)
print(ans)

Solution (Python):
h,w=map(int,input().split())
x=[]
for i in range(1,w):
    x1=[h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)]
    x2=[h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)]
    x.append(min(max(x1)-min(x1),max(x2)-min(x2)))
for i in range(1,h):
    x1=[w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)]
    x2=[w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)]
    x.append(min(max(x1)-min(x1),max(x2)-min(x2)))
print(min(x))

Solution (Python):
h,w=map(int,input().split())
x=[]
for i in range(1,w):
    x1=[h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)]
    x1.sort()
    x2=[h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)]
    x2.sort()
    x.append(min(x1[-1]-x1[0],x2[-1]-x2[0]))
for i in range(1,h):
    x1=[w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)]
    x1.sort()
    x2=[w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)]
    x2.sort()
    x.append(min(x1[-1]-x1[0],x2[-1]-x2[0]))
print(min(x))

Solution (Python):
h,w=map(int,input().split())
x=[]
for i in range(1,w):
    x1=[h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)]
    x1.sort()
    x.append(x1[-1]-x1[0])
    x2=[h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)]
    x2.sort()
    x.append(x2[-1]-x2[0])
for i in range(1,h):
    x1=[w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)]
    x1.sort()
    x.append(x1[-1]-x1[0])
    x2=[w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)]
    x2.sort()
    x.append(x2[-1]-x2[0])
print(min(x))

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
else:
    x=[]
    for i in range(1,w):
        sub=[]
        sub.append([h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)])
        
        sub.append([h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)])
        
        for j in sub:
            x.append(max(j)-min(j))
    for i in range(1,h):
        sub=[]
        sub.append([w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)])
        
        sub.append([w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)])
        
        for j in sub:
            x.append(max(j)-min(j))
    print(min(x))

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
else:
    x=[]
    for i in range(1,w):
        sub=[]
        sub.append([h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)])
        sub.append([h*i,h*((w-i)//2+1),h*(w-i)-h*((w-i)//2+1)])
        sub.append([h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)])
        sub.append([h*i,(w-i)*(h//2+1),h*(w-i)-(w-i)*(h//2+1)])
        for j in sub:
            x.append(max(j)-min(j))
    for i in range(1,h):
        sub=[]
        sub.append([w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)])
        sub.append([w*i,w*((h-i)//2+1),w*(h-i)-w*((h-i)//2+1)])
        sub.append([w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)])
        sub.append([w*i,(h-i)*(w//2+1),w*(h-i)-(h-i)*(w//2+1)])
        for j in sub:
            x.append(max(j)-min(j))
    print(min(x))

Solution (Python):
x,y = map(int,input().split())
if x%3 == 0 or y%3 == 0:
    print(0)
    exit()
ans = []
ans.append(x)
ans.append(y)
for x,y in (x,y),(y,x):
    a1 = x//3
    a2 = x-a1
    b1 = y//2
    b2 = y-y//2
    ans.append(max(a1*y,a2*b1,a2*b2)-min(a1*y,a2*b1,a2*b2))
    a1 = x//3+1
    a2 = x-a1
    ans.append(max(a1*y,a2*b1,a2*b2)-min(a1*y,a2*b1,a2*b2))
print(min(ans))

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

if H%3 == 0 or W%3 == 0:
    print(0)
    exit()
    
def solve(a, b):
    ans = 10**15
    for i in range(1, a+1):
        s1 = i * b
        s2 = (a-i) * (b//2)
        s3 = a*b - s1 - s2
        s4 = ((a-i)//2) * b
        s5 = a*b - s1 - s4
        list1 = sorted([s1, s2, s3])
        list2 = sorted([s1, s4, s5])
        s = min((list1[-1] - list1[0]), (list2[-1] - list2[0]))
        if ans > s:
            ans = s
    return ans

print(min(solve(H, W), solve(W, H)))

Solution (Python):
H, W = map(int,input().split())
ans1 = 10**18
ans2 = 10**18
for i in range(1, H):
  Sa = i*W
  w = int(W/2)
  Sb = (H-i)*w
  Sc = (H-i)*(W-w)
  l = [Sa, Sb, Sc]
  S1 = max(l)-min(l)
  h = int((H-i)/2)
  Sb1 = h*W
  Sc1 = ((H-i)-h)*W
  l = [Sa, Sb1, Sc1]
  S2 = max(l)-min(l)
  ans = min(S1,S2)
  ans1 = min(ans, ans1)
for i in range(1, W):
  Sa = i*H
  h = int(H/2)
  Sb = (W-i)*h
  Sc = (W-i)*(H-h)
  l = [Sa, Sb, Sc]
  S1 = max(l)-min(l)
  w = int((W-i)/2)
  Sb1 = w*H
  Sc1 = ((W-i)-w)*H
  l = [Sa, Sb1, Sc1]
  S2 = max(l)-min(l)
  ans = min(S1,S2)
  ans2 = min(ans, ans2)  
print(min(ans1, ans2))

Solution (Python):
import math

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

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

elif h % 2 == 0 and w %2 == 0:
    if h >= w:
        side1 = int(w/2)
        diff = 1000000000000000
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    else:
        side1 = int(h / 2)
        diff = 1000000000000000
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    print(min(diff, diff_2))


elif h % 2 == 0 or w % 2 == 0:
    if h % 2 == 0:
        side1 = int(h/2)
        diff = 1000000000000000
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
                
        print(min(diff, diff_2))
    else:
        side1 = int(w / 2)
        diff = 1000000000000000
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
        print(min(diff, diff_2))
else:
    if h >= w:
        side1 = math.ceil(h/2)
        side2 = math.floor(h/2)
        diff = 1000000000000000
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (w-i) * h
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))
    else:
        side1 = math.ceil(w/2)
        side2 = math.floor(w/2)
        diff = 1000000000000000
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (h-i) * w
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))

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

def half(h,w):
   h1 = h//2
   h2 = h - h1
   return h1*w, h2*w
def half2(h,w):
   w1 = w//2
   w2 = w-w1
   return w1*h, w2*h

ans = float("inf")
for i in range(h):
   a = i * w
   b,c = half(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   b,c = half2(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   


for i in range(w):
   a = i * h
   b,c = half(h, w-i)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   b,c = half2(h, w-i)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   
print(ans)

Solution (Python):
H, W = map(int, input().split())
ans = 1<<60


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

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


def f(x, y):
    res = float('inf')
    w = x // 2
    for h in range(1, y):
        a = h * x
        b = (y - h) * w
        c = (y - h) * (x - w)
        s = max(a, b, c) - min(a, b, c)
        res = min(s, res)

        h2 = (y - h) // 2
        if h2 == 0:
            continue
        b = h2 * x
        c = (y - h - h2) * x
        s = max(a, b, c) - min(a, b, c)
        res = min(s, res)
    return res


ans = float('inf')
ans = min(f(W, H), ans)
ans = min(f(H, W), ans)

print(ans)

Solution (Python):
H, W=map(int, input().split())
def yyy(h, w):
  if h%3==0:
    return 0
  else:
    return w
  
def yyt(h, w):
  out=h*w
  for i in range(1, w):
    temp=[(h//2+h%2)*(w-i), (h//2)*(w-i), h*i]
    if out>max(temp)-min(temp):
      out=max(temp)-min(temp)
  return out

def tty(h, w):
  return yyt(w, h)

def ttt(h, w):
  return yyy(w, h)

print(min(yyy(H, W), yyt(H, W), tty(H, W), ttt(H, W)))

Solution (Python):
from math import floor, ceil

H, W = [int(_) for _ in input().split()]


def f(H, W):
    res = 1e12
    for i in range(1, H + 1):
        a = i
        b = (H - a) // 2
        c = H - a - b
        d = max(a, b, c) - min(a, b, c)
        res = min(res, d * W)

        a = i * W
        H2 = H - i
        b = W // 2
        c = W - b
        b *= H2
        c *= H2
        d = max(a, b, c) - min(a, b, c)
        res = min(res, d)

    return res


ans = min(f(H, W), f(W, H))
print(ans)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

H, W = MAP()


def check1(h, w):
    if h < 3:
        return INF
    if h % 3 == 0:
        return 0
    else:
        return w


def check2(h, w):
    if h < 2 or w < 2:
        return INF
    res = INF
    
    for i in range(1, h):
        a = i * w
        b = (h-i) * (w // 2)
        c = (h-i) * ceil(w, 2)
        res = min(res, max(a, b, c) - min(a, b, c))
    return res


ans = min(check1(H, W), check1(W, H), check2(H, W), check2(W, H))
print(ans)

Solution (Python):
H,W=map(int,input().split())
answer=0
if not H%3==0 and not W%3==0:
  S_1=[H//3*W,(H-H//3)*(W//2),(H-H//3)*(W-W//2)]
  S_2=[(H//3+1)*W,(H-H//3-1)*(W//2),(H-H//3-1)*(W-W//2)]
  S_3=[W//3*H,(W-W//3)*(H//2),(W-W//3)*(H-H//2)]
  S_4=[(W//3+1)*H,(W-W//3-1)*(H//2),(W-W//3-1)*(H-H//2)]
  answer=min(max(S_1)-min(S_1),max(S_2)-min(S_2),max(S_3)-min(S_3),max(S_4)-min(S_4),H,W)
print(answer)

Solution (Python):

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

ans = H * W


for h in range(1, H):
    a = h * W

    
    b = ((H - h) // 2) * W
    c = H * W - a - b
    ans = min(ans, max(a,b,c) - min(a,b,c))
    

    
    b = (H - h) * (W // 2)
    c = H * W - a - b
    ans = min(ans, max(a,b,c) - min(a,b,c))
    


for w in range(1, W):
    a = w * H

    
    b = (W - w) * (H//2)
    c = H * W - a - b
    ans = min(ans, max(a,b,c) - min(a,b,c))
    

    
    b = ((W - w) // 2) * H
    c = H * W - a - b
    ans = min(ans, max(a,b,c) - min(a,b,c))
    

print(ans)

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

min1=float("inf")
i=0
while 1:
  kouho=[h*i,math.ceil(h/2)*(w-i),(h//2)*(w-i)]
  sa=max(kouho)-min(kouho)
  if sa<min1:
    min1=sa
  else:break
  i+=1

min2=float("inf")
i=0


while 1:
  kouho=[w*i,math.ceil(w/2)*(h-i),(w//2)*(h-i)]
  sa=max(kouho)-min(kouho)
  if sa<min2:
    min2=sa
  else:break
  i+=1

min3=w*(math.ceil(h/3)-h//3)
min4=h*(math.ceil(w/3)-w//3)


print(min(min1,min2,min3,min4))