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):
def f(Hn, Wn):
    ret = float('inf')
    for Wi in range(1, Wn):
        h = Hn // 2
        w = Wn - Wi
        Sn = Wi * Hn, w * h, w * (Hn - h)
        ret = min(ret, max(Sn) - min(Sn))
        w //= 2
        Sn = Wi * Hn, w * Hn, (Wn - Wi - w) * Hn
        ret = min(ret, max(Sn) - min(Sn))
    return ret


def main():
    H, W = map(int, input().split())
    ans = f(H, W)
    if not ans:
        print(ans)
        return
    ans = min(ans, f(W, H))
    if not ans:
        print(ans)
        return
    print(ans)
    return


main()

Solution (Python):
H,W = map(int,input().split())
a = W%3
if a==0:
    s1 = 0
else:
    s1 = H
a = H%3
if a==0:
    s2 = 0
else:
    s2 = W
s3 = H*W
for i in range(1,W):
    a = max(i*H,(H-H//2)*(W-i))
    b = min(i*H,(H//2)*(W-i))
    s3 = min(s3,a-b)
s4 = H*W
for i in range(1,H):
    a = max(i*W,(W-W//2)*(H-i))
    b = min(i*W,(W//2)*(H-i))
    s4 = min(s4,a-b)
print(min(s1,s2,s3,s4))

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

mind = float('inf')

for i in range(1,h):
  d = max(abs(w*i-(w//2)*(h-i)),abs(w*i+(-w//2)*(h-i)),(h-i)*bool(w%2))
  mind = min(mind,d)

for i in range(1,w):
  d = max(abs(h*i-(h//2)*(w-i)),abs(h*i+(-h//2)*(w-i)),(w-i)*bool(h%2))
  mind = min(mind,d)

mind = min(mind,min(h,w)*bool(max(h,w)%3)*bool((h*w)%3))

print(mind)

Solution (Python):
def sp(n):
  a = n // 3
  b = (n - a) // 2
  c = n -a -b
  return a,b,c

def ceil(x, y):
  return (x -1) // y + 1

def tatedake_diff(h, w):
  a,b,c = sp(h)
  s1 = (c-a) * w
  a,b,c = sp(w)
  s2 = (c-a) * h
  return min(s1, s2)

def tateyoko_diff(h, w):
  a,b,c = sp(h)
  s1 = (c * w)
  s2 = (h - c) * (w // 2)
  s3 = (h - c) * ceil(w, 2)
  r1 = max(s1,s2,s3) - min(s1,s2,s3)
  s1 = (a * w)
  s2 = (h - a) * (w // 2)
  s3 = (h - a) * ceil(w, 2)
  r2 = max(s1,s2,s3) - min(s1,s2,s3)
  return min(r1,r2)
  
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
  ans = 0
else:
  ans = min(tatedake_diff(H, W), tateyoko_diff(H, W), tateyoko_diff(W, H))
print(ans)

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
else:
    ans=float('inf')
    for i in range(1,h):
        c=max(i*(w//2),i*(w-w//2),(h-i)*w)-min(i*(w//2),i*(w-w//2),(h-i)*w)
        if ans>c:
            ans=c
        
    for i in range(1,w):
        d=max(i*(h//2),i*(h-h//2),(w-i)*h)-min(i*(h//2),i*(h-h//2),(w-i)*h)
        if ans>d:
            ans=d
        
    print(min(ans,h,w))

Solution (Python):
from math import ceil



def river(x, y):
    diff = ceil(x / 3) - x // 3
    return diff * y



def t(x, y):
    def f(uy):
        dy = y - uy
        lx = x // 2
        rx = x - lx
        us = x * uy
        ls = lx * dy
        rs = rx * dy
        return max(us, ls, rs) - min(us, ls, rs)

    return min(f(y // 3), f(ceil(y / 3)))


def solve(h, w):
    return min(river(h, w), river(w, h), t(h, w), t(w, h))


def main():
    h, w = map(int, input().split())

    ans = solve(h, w)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def main():
    h, w = map(int, input().split())
    p = [h//2+w//3+1, h//3+w//2+1, h, w]
    if h % 3 == 0 or w % 3 == 0:
        p += [0]
    if h % 2 == 0:
        p += [h//2]
    if w % 2 == 0:
        p += [w//2]
    print(min(p))

if __name__ == '__main__':
    main()

Solution (Python):


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

def ans_every_h1(H,W):
    piece = [0]*3
    for h1 in range(1,H):
        piece[0] = h1*W
        piece[1] = ((H - h1)//2) * W
        piece[2] = H*W - piece[0] - piece[1]
        ans_list.append(max(piece) - min(piece))
        
        piece[1] = (H - h1) * (W//2)
        piece[2] = H*W - piece[0] - piece[1]
        ans_list.append(max(piece) - min(piece))

if H%3 == 0 or W%3 == 0:
    print(0)
else:
    ans_list = []
    ans_every_h1(H,W)
    ans_every_h1(W,H)
    print(min(ans_list))

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

s1 = (h // 3) * w
s2 = (h - (h // 3)) * (w // 2)
s3 = h * w - s1 - s2
diff_sh1 = max(s1, s2, s3) - min(s1, s2, s3)

s1 = (h // 3 + 1) * w
s2 = (h - (h // 3 + 1)) * (w // 2)
s3 = h * w - s1 - s2
diff_sh2 = max(s1, s2, s3) - min(s1, s2, s3)

s1 = (h // 3) * w
s2 = ((h - (h // 3)) // 2) * w
s3 = h * w - s1 - s2
diff_sh3 = max(s1, s2, s3) - min(s1, s2, s3)

s1 = (h // 3 + 1) * w
s2 = ((h - (h // 3 + 1)) // 2) * w
s3 = h * w - s1 - s2
diff_sh4 = max(s1, s2, s3) - min(s1, s2, s3)


s1 = h * (w // 3)
s2 = (h // 2) * (w - (w // 3))
s3 = h * w - s1 - s2
diff_sw1 = max(s1, s2, s3) - min(s1, s2, s3)

s1 = h * (w // 3 + 1)
s2 = (h // 2) * (w - (w // 3 + 1))
s3 = h * w - s1 - s2
diff_sw2 = max(s1, s2, s3) - min(s1, s2, s3)

s1 = h * (w // 3)
s2 = h * ((w - (w // 3)) // 2)
s3 = h * w - s1 - s2
diff_sw3 = max(s1, s2, s3) - min(s1, s2, s3)

s1 = h * (w // 3 + 1)
s2 = h * ((w - (w // 3 + 1)) // 2)
s3 = h * w - s1 - s2
diff_sw4 = max(s1, s2, s3) - min(s1, s2, s3)

min_sh = min(diff_sh1, diff_sh2, diff_sh3, diff_sh4)
min_sw = min(diff_sw1, diff_sw2, diff_sw3, diff_sw4)
print(min(min_sh, min_sw))

Solution (Python):
n,m=map(int,input().split())
ans=10000000000000
for i in range(1,n):
    a=i*m
    nk=n-i
    if nk%2==0:
        b=m*(nk//2)
        ans=min(ans,abs(a-b))
    elif nk>1:
        b=m*((nk+1)//2)
        c=m*((nk-1)//2)
        ans=min(ans,max(a,b,c)-min(a,b,c))
    if m%2==0:
        b=nk*m//2
        ans=min(ans,abs(a-b))
    elif m>1:
        b=nk*(m+1)//2
        c=nk*(m-1)//2
        ans=min(ans,max(a,b,c)-min(a,b,c))

for i in range(1,m):
    a=i*n
    nk=m-i
    if nk%2==0:
        b=n*(nk//2)
        ans=min(ans,abs(a-b))
    elif nk>1:
        b=n*((nk+1)//2)
        c=n*((nk-1)//2)
        ans=min(ans,max(a,b,c)-min(a,b,c))
    if n%2==0:
        b=nk*n//2
        ans=min(ans,abs(a-b))
    elif n>1:
        b=nk*(n+1)//2
        c=nk*(n-1)//2
        ans=min(ans,max(a,b,c)-min(a,b,c))
print(ans)

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
  print(0)
else:
  ans=h*w
  v=w//2
  for i in range(1,h):
    a=i*w
    y=(h-i)//2
    z=h-i-y
    b=y*w
    c=z*w
    d=(h-i)*v
    e=(h-i)*(w-v)
    ans=min(ans,max(a,b,c)-min(a,b,c),max(a,d,e)-min(a,d,e))
  v=h//2
  for i in range(1,w):
    a=i*h
    y=(w-i)//2
    z=w-i-y
    b=y*h
    c=z*h
    d=(w-i)*v
    e=(w-i)*(h-v)
    ans=min(ans,max(a,b,c)-min(a,b,c),max(a,d,e)-min(a,d,e))
  print(ans)

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

for i in range(H+1):
  S = [0,0,0]
  S[0] = W * i
  S[1] = W // 2 * (H - i)
  S[2] = H * W - S[0] - S[1]
  ans = min(ans, max(S)-min(S))
  
  S[1] = (H-i) // 2 * W
  S[2] = H * W - S[0] - S[1]
  ans = min(ans, max(S)-min(S))

H, W = W, H
for i in range(H+1):
  S = [0,0,0]
  S[0] = W * i
  S[1] = W // 2 * (H - i)
  S[2] = H * W - S[0] - S[1]
  ans = min(ans, max(S)-min(S))
  
  S[1] = (H-i) // 2 * W
  S[2] = H * W - S[0] - S[1]
  ans = min(ans, max(S)-min(S))
  
print(ans)

Solution (Python):

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

ans = min(H, W)


if H % 3 == 0 or W % 3 == 0:
    ans = 0
    
for ha in range(H//3, H//3+3):
    Sa = ha * W
    
    
    hb = (H - ha)//2
    hc = H - ha - hb
    Sb = hb * W
    Sc = hc * W
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

for wa in range(W//3, W//3+3):
    Sa = H * wa
    
    
    hb = H//2
    hc = H - hb
    Sb = hb * (W - wa)
    Sc = hc * (W - wa)
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Solution (Python):
def minimize(H, W):
    ans = float('inf')
    for h in range(1, H):
        sa = h * W
        sb = (H - h) * (W // 2)
        sc = (H - h) * (W - W // 2)
        smax = max(sa, sb, sc)
        smin = min(sa, sb, sc)
        ans = min(ans, smax - smin)

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

H, W = tuple(map(int, input().split()))
ans1 = minimize(H, W)
ans2 = minimize(W, H)
print(min(ans1, ans2))

Solution (Python):

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

ans = min(H, W)

for h in range(H // 3, H // 3 + 3):
    w = W // 2
    s1 = h*W
    s2 = (H-h)*w
    s3 = (H-h)*(W-w)
    ans = min(ans, max(s1,s2,s3) - min(s1,s2,s3))

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

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

if H % 3 == 0:
    print(0)
    exit()
else:
    ans.append(W)

s1 = [H * (W // 3), H // 2 * (W - W // 3), (H // 2 + H % 2) * (W - W // 3)]
s2 = [H * (W // 3 + 1), H // 2 * (W - W // 3 - 1), (H // 2 + H % 2) * (W - W // 3 - 1)]

ans.append(min(max(s1) - min(s1), max(s2) - min(s2)))

H, W = W, H

if H % 3 == 0:
    print(0)
    exit()
else:
    ans.append(W)

s1 = [H * (W // 3), H // 2 * (W - W // 3), (H // 2 + H % 2) * (W - W // 3)]
s2 = [H * (W // 3 + 1), H // 2 * (W - W // 3 - 1), (H // 2 + H % 2) * (W - W // 3 - 1)]

ans.append(min(max(s1) - min(s1), max(s2) - min(s2)))

print(min(ans))

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

if H % 3 == 0:
    ans.append(0)
else:
    ans.append(W)

s1 = [H * (W // 3), H // 2 * (W - W // 3), (H // 2 + H % 2) * (W - W // 3)]
s2 = [H * (W // 3 + 1), H // 2 * (W - W // 3 - 1), (H // 2 + H % 2) * (W - W // 3 - 1)]

ans.append(min(max(s1) - min(s1), max(s2) - min(s2)))

H, W = W, H

if H % 3 == 0:
    ans.append(0)
else:
    ans.append(W)

s1 = [H * (W // 3), H // 2 * (W - W // 3), (H // 2 + H % 2) * (W - W // 3)]
s2 = [H * (W // 3 + 1), H // 2 * (W - W // 3 - 1), (H // 2 + H % 2) * (W - W // 3 - 1)]

ans.append(min(max(s1) - min(s1), max(s2) - min(s2)))

print(min(ans))

Solution (Python):
h,w = map(int,input().split())
ans = float("Inf")
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
for i in range(1,h):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    c = ((h-i)//2) * w
    d = -(-(h-i)//2) * w
    ans = min(ans,max(i*w,a,b)-min(i*w,a,b))
    ans = min(ans,max(i*w,c,d)-min(i*w,c,d))
for i in range(1,w):
    a = (w-i)*(h//2)
    b = (w-i)*(h-(h//2))
    c = ((w-i)//2) * h
    d = -(-(w-i)//2) * h
    ans = min(ans,max(i*h,a,b)-min(i*h,a,b))
    ans = min(ans,max(i*h,c,d)-min(i*h,c,d))
print(ans)

Solution (Python):
h,w = map(int,input().split())
ans = float("Inf")
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
for i in range(1,h):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    ans = min(ans,abs(max(i*w,a,b)-min(i*w,a,b)))
for i in range(1,w):
    a = (w-i)*(h//2)
    b = (w-i)*-(-h//2)
    ans = min(ans,abs(max(i*h,a,b)-min(i*h,a,b)))
print(min(ans,h,w))

Solution (Python):
H, W = map(int, input().split())
res = min(H, W)
h = round(H / 3)
w = W // 2
s1 = h * W
s2 = (H-h) * w
s3 = (H-h) * (W-w)
res = min(res, max(s1, s2, s3) - min(s1, s2, s3))
w = round(W / 3)
h = H // 2
s1 = w * H
s2 = (W-w) * h
s3 = (W-w) * (H-h)
res = min(res, max(s1, s2, s3) - min(s1, s2, s3))
if H % 3 == 0 or W % 3 == 0:
    res = 0
print(res)

Solution (Python):
H, W = map(int, input().split())
ans = 10**9

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())
if h%3==0 or w%3==0:
  a=0
else:
  def func(x,y):
    a=10**5
    for i in range(1,x//3+2):
      s=i*y
      t=((x-i)//2)*y
      b=max(s,t,x*y-s-t)-min(s,t,x*y-s-t)
      t=(y//2)*(x-i)
      a=min(a,b,max(s,t,x*y-s-t)-min(s,t,x*y-s-t))
    return a
  a=min(func(w,h),func(h,w))
print(a)

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
if h%3!=0 and w%3!=0:
    ansh=h*w
    for j in range(1,h):
        area1=w*j
        number=w//2
        area2=(h-j)*number
        area3=(h-j)*(w-number)
        check=max(abs(area1-area2),abs(area1-area3),abs(area2-area3))
        if check<ansh:
            ansh=check
        else:
            continue  
    answ=w*h
    for j in range(1,w):
        area1=h*j
        number=h//2
        area2=(w-j)*number
        area3=(w-j)*(h-number)
        check=max(abs(area1-area2),abs(area1-area3),abs(area2-area3))
        if check<answ:
            answ=check
        else:
             continue
    print(min(ansh,answ,w,h))

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

ds = []

for i in range(1, h):
  s1 = w*i
  s2 = (h-i)*(w//2)
  s3 = (h-i)*(w-w//2)
  d1 = max(s1, s2, s3) - min(s1, s2, s3)
  
  s1 = w*i
  s2 = w*((h-i)//2)
  s3 = w*(h-(h-i)//2-i)
  d2 = max(s1, s2, s3) - min(s1, s2, s3)
  
  if i == h-1:
    d = d1
  else:
    d = min(d1, d2)
  ds.append(d)

for i in range(1, w):
  s1 = h*i
  s2 = (w-i)*(h//2)
  s3 = (w-i)*(h-h//2)
  d1 = max(s1, s2, s3) - min(s1, s2, s3)
  
  s1 = h*i
  s2 = h*((w-i)//2)
  s3 = h*(w-(w-i)//2-i)
  d2 = max(s1, s2, s3) - min(s1, s2, s3)
  
  if i == w-1:
    d = d1
  else:
    d = min(d1, d2)
  ds.append(d)
  
print(min(ds))

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

ds = []

for i in range(1, h):
  s1 = w*i
  s2 = (h-i)*(w//2)
  s3 = (h-i)*(w-w//2)
  d1 = max(s1, s2, s3) - min(s1, s2, s3)
  
  s1 = w*i
  s2 = w*((h-i)//2)
  s3 = w*(h-(h-i)//2-i)
  d2 = max(s1, s2, s3) - min(s1, s2, s3)
  
  if i == h-1:
    d = d1
  else:
    d = min(d1, d2)
  ds.append(d)

for i in range(1, w):
  s1 = h*i
  s2 = (w-i)*(h//2)
  s3 = (w-i)*(h-h//2)
  d1 = max(s1, s2, s3) - min(s1, s2, s3)
  
  s1 = h*i
  s2 = h*((w-i)//2)
  s3 = h*(w-(w-i)//2-i)
  d2 = max(s1, s2, s3) - min(s1, s2, s3)
  
  if i == w-1:
    d = d1
  else:
    d = min(d1, d2)
  ds.append(d)
  
print(min(ds))

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    h,w=map(int,input().split())
    if h%3==0 or w%3==0:
        print(0)
        return
    ans=INF
    
    a=(h//3)*w
    b=(h-h//3)*(w//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    a=(h//3+1)*w
    b=(h-h//3-1)*(w//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    
    a=(w//3)*h
    b=(w-w//3)*(h//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    a=(w//3+1)*h
    b=(w-w//3-1)*(h//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    
    print(min(ans,h,w))
resolve()

Solution (Python):
h, w = map(int, input().split())
ans = 10000000000
for i in range(1, h):
  a = i * w
  b = ((h - i) // 2) * w
  c = h * w - a - b
  if ans > (max(a, c) - min(a, b)):
    ans = max(a, c) - min(a, b)
  
  b = (h - i) * (w // 2)
  c = h * w - a - b
  if ans > (max(a, c) - min(a, b)):
    ans = max(a, c) - min(a, b)

for i in range(1, w):
  a = i * h
  b = ((w - i) // 2) * h
  c = h * w - a - b
  if ans > (max(a, c) - min(a, b)):
    ans = max(a, c) - min(a, b)
  
  b = (w - i) * (h // 2)
  c = h * w - a - b
  if ans > (max(a, c) - min(a, b)):
    ans = max(a, c) - min(a, b)
print(ans)

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


def calc(a, b):
    return min([
        b,
        max([
            abs(a // 3 * b - (a - a // 3) * (b // 2)),
            abs(a // 3 * b - (a - a // 3) * (b // 2 + b % 2)),
            (a - a // 3) * (b % 2),
        ]),
        max([
            abs((a // 3 + 1) * b - (a - a // 3 - 1) * (b // 2)),
            abs((a // 3 + 1) * b - (a - a // 3 - 1) * (b // 2 + b % 2)),
            (a - a // 3 - 1) * (b % 2),
        ]),
    ])


if h * w % 3 == 0:
    print(0)
else:
    print(min([calc(h, w), calc(w, h)]))

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

if h % 3 == 0 or h % 3 == 0:
    print(0)
    exit()
for i in range(1,w):
    H1, H2= h//2, -(-h//2)
    check = max(h*i,H2*(w-i),H1*(w-i))
    check2 = min(h*i,H1*(w-i),H2*(w-i))
    ans = min(ans,abs(check-check2))
for i in range(1,h):
    w1,w2 = w//2, -(-w//2)
    check = max(w*i,w1*(h-i),w2*(h-i))
    check2 = min(w*i,w1*(h-i),w2*(h-i))
    ans = min(ans,abs(check-check2))
print(min(ans,h,w))

Solution (Python):
import sys


		
def main():
	h, w = map(int, input().split())
	
	if h > w:
		(h,w) = (w,h)
	
	if h*w % 3 == 0:
		return 0
	
	if h==2 and w==2:
		return 1
		
	
	mm=h
	
	
	for i in range(1, h-1 +1):
		a = w * i
		b = (w // 2) * (h - i)
		c = ((w // 2) + w % 2) * (h - i)
		rt=max(a,b,c)-min(a,b,c)
		mm=min(rt, mm)
	
	
	for i in range(1, w-1 +1):
		a = h * i
		b = (h // 2) * (w - i)
		c = ((h // 2) + h % 2) * (w - i)
		rt=max(a,b,c)-min(a,b,c)
		mm=min(rt, mm)	
	
	return mm
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
def main():
    h,w=map(int,input().split(' '))
    if h%3 == 0 or w%3 ==0:
        print(0)
        exit()
    ans = h*w
    for i in range(1,w):
        a=h*i
        b=(w-i)*(h//2)
        c=(w-i)*-(-h//2)
        if ans > max(a,b,c)-min(a,b,c):
            ans = max(a,b,c)-min(a,b,c)
    for j in range(1,h):
        a=w*j
        b=(h-j)*(w//2)
        c=(h-j)*-(-w//2)
        if ans > max(a,b,c)-min(a,b,c):
            ans = max(a,b,c)-min(a,b,c)
    print(min(ans,h,w))
if __name__ == '__main__':
    main()

Solution (Python):
h, w = map(int, input().split())
inf = 10 ** 9
ans1 = inf


div_h = round(h/3)
div_w = w // 2
s = [div_h * w, (h - div_h) * div_w, (h - div_h) * (w - div_w)]
ans11 = max(s) - min(s)


if w < 3:
    ans12 = inf
else:
    div_h2 = (h - div_h) // 2
    s = [div_h * w, div_h2 * w, (h - div_h - div_h2) * w]
    ans12 = max(s) - min(s)

div_w = round(w/3)
div_h = h // 2
s = [div_w * h, (w - div_w) * div_h, (w - div_w) * (h - div_h)]
ans21 = max(s) - min(s)


if h < 3:
    ans22 = inf
else:
    div_w2 = (w - div_w) // 2
    s = [div_w * h, div_w2 * h, (w - div_w - div_w2) * h]
    ans22 = max(s) - min(s)
print(min(ans11, ans12, ans21, ans22))

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

ans = float('inf')
for _ in range(2):
  for i in [H//3,H//3+1]:
    a = i*W
    b = ((H - i)//2)*W
    c = ((H - i + 1)//2)*W
    ans = min(ans, abs(max(a, b, c) - min(a, b, c)))
 
    b = (W//2) * (H-i)
    c = ((W + 1)//2) * (H-i)
    ans = min(ans, abs(max(a, b, c) - min(a, b, c)))
  H, W = W, H
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
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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 = 1000000007



h, w = LI()
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ret = INF
    for y in range(1, h):
        ret = min(ret, max(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)) - min(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)))
    h, w = w, h
    for y in range(1, h):
        ret = min(ret, max(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)) - min(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)))
    print(min(h, w, ret))

Solution (Python):
H,W = map(int,input().split())
ans = [H//2+W//3+1,H//3+W//2+1,H,W]

if(H%3==0 or W%3==0):
  ans+=[0]
if(H%2==0):
  ans+=[H//2]
if(W%2==0):
  ans+=[W//2]

print(min(ans))

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

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

def nasu(a, b, c):
  return max(a, b, c) - min(a, b, c)

mi = H * W
for i in range(1, H):
  x = W * i
  y = W * ((H - i) // 2)
  z = H * W - x - y
  mi = min(mi, nasu(x, y, z))
  y = (H - i) * (W // 2)
  z = H * W - x - y
  mi = min(mi, nasu(x, y, z))

for i in range(1, W):
  x = H * i
  y = H * ((W - i) // 2)
  z = H * W - x - y
  mi = min(mi, nasu(x, y, z))
  y = (W - i) * (H // 2)
  z = H * W - x - y
  mi = min(mi, nasu(x, y, z))
  
print(mi)

Solution (Python):

import sys
from math import ceil

def solve(H: int, W: int):
    ans = 100000000000
    if(H%3 == 0 or W%3 == 0):
        ans = 0
    
    if(H%3):
        ans = min(ans, W)
    if(W%3):
        ans = min(ans, H)
    
    for i in range(1,W):
        if(H%2):
            l = [H*i, (W-i)*(H//2), (W-i)*(H//2+1)]
            l.sort()
            
            ans = min(ans, abs(l[2]-l[0]))
        else:
            ans = min(ans, abs(H*i-(W-i)*(H//2)))

    for i in range(1,H):
        if(W%2):
            l = [W*i, (H-i)*(W//2), (H-i)*(W//2+1)]
            l.sort()
            
            ans = min(ans, abs(l[2]-l[0]))
        else:
            ans = min(ans, abs(W*i-(H-i)*(W//2)))

    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    solve(H, W)

if __name__ == '__main__':
    main()

Solution (Python):
H,W=map(int,input().split())
h,w=H//2,W//3
s=sorted([w*H,(W-w)*(H-h),(W-w)*h])
a=s[2]-s[0]
s=sorted([(w+1)*H,(W-w-1)*(H-h),(W-w-1)*h])
a=min(s[2]-s[0],a)
a=min(H*(W-3*w),a)
a=min(H*(3+3*w-W),a)
h,w=H//3,W//2
s=sorted([W*h,(W-w)*(H-h),w*(H-h)])
a=min(s[2]-s[0],a)
s=sorted([W*(h+1),(W-w)*(H-h-1),w*(H-h-1)])
a=min(s[2]-s[0],a)
s=sorted([W*(h+2),(W-w)*(H-h-2),w*(H-h-2)])
a=min(s[2]-s[0],a)
a=min((H-3*h)*W,a)
a=min((3+3*h-H)*W,a)
print(a)

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

if H % 3 == 0:
    ans = 0
else:
    ans = min(ans, W)

if W % 3 == 0:
    ans = 0
else:
    ans = min(ans, H)

for i in range(1, H):
    if W % 2 == 0:
        dif = abs(i*W - (H-i)*(W//2))
    else:
        dif = max(i*W, (H-i)*((W//2)+1)) - min(i*W, (H-i)*(W//2))
    ans = min(ans, dif)

for i in range(1, W):
    if H % 2 == 0:
        dif = abs(i*H - (W-i)*(H//2))
    else:
        dif = max(i*H, (W-i)*((H//2)+1)) - min(i*H, (W-i)*(H//2))
    ans = min(ans, dif)

print(ans)

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

ans = float('inf')
for _ in range(2):
    for i in range(1, H):
        a = i*W

        b = ((H - i) // 2) * W
        c = ((H - i + 1)// 2) * W
        ans = min(ans, abs(max(a, b, c) - min(a, b, c)))

        b = (W // 2) * (H -i)
        c = ((W + 1) // 2) * (H - i)
        ans = min(ans, abs(max(a, b, c) - min(a, b, c)))
    H, W = W, H
print(ans)

Solution (Python):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,fractions
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))
def inpl_str(): return list(sys.stdin.readline().split())
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

h,w = inpl()
ans = h*w
for i in range(1,h):
    sa = i*w
    sb = math.floor((h-i)/2) * w
    sc = math.ceil((h-i)/2) * w
    dif_1 = max(sa,sb,sc) - min(sa,sb,sc)
    sb = math.floor(w/2) * (h-i)
    sc = math.ceil(w/2) * (h-i)
    dif_2 = max(sa,sb,sc) - min(sa,sb,sc)
    ans = min(ans,dif_1,dif_2)
for i in range(1,w):
    sa = i*h
    sb = math.floor((w-i)/2) * h
    sc = math.ceil((w-i)/2) * h
    dif_3 = max(sa,sb,sc) - min(sa,sb,sc)
    sb = math.floor(h/2) * (w-i)
    sc = math.ceil(h/2) * (w-i)
    dif_4 = max(sa,sb,sc) - min(sa,sb,sc)
    ans = min(ans,dif_3,dif_4)
print(ans)

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

def main():
    H, W = map(int, input().split())

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

    ans = int(1e10)
    for h in range(1, H):
        s1 = h * W
        tmp = (H - h) // 2
        s2 = tmp * W
        s3 = (H - h - tmp) * W
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
        tmp = W // 2
        s2 = tmp * (H - h)
        s3 = (W - tmp) * (H - h)
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
    for w in range(1, W):
        s1 = w * H
        tmp = (W - w) // 2
        s2 = tmp * H
        s3 = (W - w - tmp) * H
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
        tmp = H // 2
        s2 = (W - w) * tmp
        s3 = (W - w) * (H - tmp)
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
h,w=map(int,input().split())
ans=h*w
if h%3==0 or w%3==0:
  ans=0
else:
  ans=min(h,w)
a=round(h/3)*w
b=(h-round(h/3))*(w//2)
c=h*w-a-b
d=max(a,b,c)-min(a,b,c)
ans=min(ans,d)
a=round(w/3)*h
b=(w-round(w/3))*(h//2)
c=h*w-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
sys.setrecursionlimit(10**6)


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


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

def calc(ans, height, width):
    
    if height % 2 == 0:
        
        for lw in range(1, width):
            a = lw * height
            b = (width-lw) * (height//2)
            ans = min(ans, abs(a-b))
    else:
        
        for lw in range(1,width):
            a = lw * height
            b = (width-lw) * (height//2)
            c = (width-lw) * (height//2 + 1)
            tmp = max(a,b,c) - min(a,b,c)
            ans = min(ans, tmp)

    return ans

ans = min(H,W)
ans = min(ans, calc(ans, H, W))


ans = min(ans, calc(ans, W, H))

print(ans)

Solution (Python):
H, W = [int(x) for x in input().split()]
ret = 10 ** 9 + 7

def f(a, b, c):
  s = [a[0] * a[1], b[0] * b[1], c[0] * c[1]]
  return max(s) - min(s)

a, b, c = [H // 3] * 3
if H % 3 > 0:
  a += 1
if H % 3 == 2:
  b += 1

ret = min(ret, f([a, W],[b, W],[c, W]))

a, b, c = [W // 3] * 3
if W % 3 > 0:
  a += 1
if W % 3 == 2:
  b += 1

ret = min(ret, f([a, H],[b, H],[c, H]))

a, b = [W // 2] * 2
if W % 2 == 1:
  a += 1
for i in range(1, H):
  t = f([a, H - i],[b, H - i],[i, W])
  ret = min(ret, t)
  
a, b = [H // 2] * 2
if H % 2 == 1:
  a += 1
for i in range(1, W):
  t = f([a, W - i],[b, W - i],[i, H])
  ret = min(ret, t)

print(ret)

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(1, W):
    w2 = (W - i) // 2
    w3 = W - i - w2
    s1 = H * i
    s2 = H * w2
    s3 = H * w3
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)

for i in range(1, H):
    h2 = (H - i) // 2
    h3 = H - i - h2
    s1 = i * W
    s2 = h2 * W
    s3 = h3 * W
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)


for i in range(1, W):
    h2 = H // 2
    
    s1 = i * H
    s2 = (W - i) * h2
    s3 = (W - i) * (H - h2)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)
    
    s1 = (W - i) * H
    s2 = i * h2
    s3 = i * (H - h2)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)


for i in range(1, H):
    w2 = W // 2
    
    s1 = W * i
    s2 = w2 * (H - i)
    s3 = (W - w2) * (H - i)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)
    
    s1 = W * (H - i)
    s2 = w2 * i
    s3 = (W - w2) * i
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)

print(ans)

Solution (Python):
import math

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    L = [[H,W], [W,H]]
    ans = 1e10
    for H,W in L: 
        Hp = [H, math.ceil(H/2), math.floor(H/2)]
        for x in range(1, W):
            S = [Hp[0]*x, Hp[1]*(W-x), Hp[2]*(W-x)]
            temp = max(S) - min(S)
            if ans > temp:  ans = temp

    ans = min([ans, W, H])
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H < W:
    H, W = W, H

S = H * W
for i in range(1, H):
    A = i * W
    BH1 = ((H-i)//2) * W
    BH2 = ((H-i)//2+(H-i)%2) * W
    BW1 = (W//2) * (H-i)
    BW2 = (W//2+W%2) * (H-i)
    SH = max(A,BH1,BH2) - min(A,BH1,BH2)
    SW = max(A,BW1,BW2) - min(A,BW1,BW2)
    S = min(S, SH, SW)

for i in range(1, W):
    A = i * H
    BH1 = ((W-i)//2) * H
    BH2 = ((W-i)//2+(W-i)%2) * H
    BW1 = (H//2) * (W-i)
    BW2 = (H//2+H%2) * (W-i)
    SH = max(A,BH1,BH2) - min(A,BH1,BH2)
    SW = max(A,BW1,BW2) - min(A,BW1,BW2)
    S = min(S, SH, SW)

print(S)

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

def calc(a, b):
    diff=[]
    s = [0]*3
    s[0] = a*int(b/3)
    if (a*b-s[0])%2==0:
        s[1]=int((a*b-s[0])/2)
        s[2]=s[1]
    else:
        s[1]=int(max(a,b-int(b/3))/2)*min(a,b-int(b/3))
        s[2] = a*b-s[0]-s[1]
    diff.append(max(s)-min(s))
    
    s = [0]*3
    s[0] = a*(int(b/3)+1)
    if (a*b-s[0])%2==0:
        s[1]=int((a*b-s[0])/2)
        s[2]=s[1]

    else:
        s[1]=int(max(a,(b-int(b/3)-1))/2)*min(a,(b-int(b/3)-1))
        s[2] = a*b-s[0]-s[1]
    diff.append(max(s)-min(s))
    return min(diff)

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

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
	print(0)
	exit()
x=[h,w]
import math
for i in range(1,w):
	p=abs(h*i-(w-i)*(h//2))
	q=abs(h*i-(w-i)*math.ceil((h/2)))
	x.append(max(p,q))
for i in range(1,h):
	p=abs(w*i-(h-i)*(w//2))
	q=abs(w*i-(h-i)*math.ceil((w/2)))
	x.append(max(p,q))
print(min(x))

Solution (Python):









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

if (H%3)*(W%3)==0:
  print(0)
else:
  d1 = min(H, W)
  ah = H//3
  h = H-ah
  d2 = abs(((W+1)//2)*h - ah*W)
  aw = W//3
  w = W-aw
  d3 = abs(((H+1)//2)*w - aw*H)
  ah = (H+2)//3
  h = H-ah
  d4 = abs((W//2)*h - ah*W)
  aw = (W+2)//3
  w = W-aw
  d5 = abs((H//2)*w - aw*H)

  print(min(d1,d2,d3,d4,d5))