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):
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

H, W = nl()

min_diff = H * W
for h1 in range(1, H):
    sa = h1*W

    h2 = (H-h1) // 2
    sb = h2*W
    sc = (H - h1 - h2)*W

    diff = max(sa, sb, sc) - min(sa, sb, sc)
    min_diff = min(diff, min_diff)

    w2 = W // 2
    sb = (H - h1)*w2
    sc = (H - h1)*(W - w2)
    diff = max(sa, sb, sc) - min(sa, sb, sc)
    min_diff = min(diff, min_diff)

for w1 in range(1, W):
    sa = H*w1

    w2 = (W-w1) // 2
    sb = H*w2
    sc = H*(W - w1 - w2)

    diff = max(sa, sb, sc) - min(sa, sb, sc)
    min_diff = min(diff, min_diff)

    h2 = H // 2
    sb = (W - w1)*h2
    sc = (W - w1)*(H - h2)
    diff = max(sa, sb, sc) - min(sa, sb, sc)
    min_diff = min(diff, min_diff)

print(min_diff)

Solution (Python):
H,W = map(int,input().split())
ans = float('inf')
for i in range(1,H):
    a = i*W
    if H-i % 2 == 0 or W % 2 == 0:
        b = (H-i)*W//2
        c = b
    else:
        b = ((H-i)*W - min(H-i,W))//2
        c = ((H-i)*W + min(H-i,W))//2
    ans = min(ans,max(a,b,c)-min(a,b,c))
H,W = W,H
for i in range(1,H):
    a = i*W
    if H-i % 2 == 0 or W % 2 == 0:
        b = (H-i)*W//2
        c = b
    else:
        b = ((H-i)*W - min(H-i,W))//2
        c = ((H-i)*W + min(H-i,W))//2
    ans = min(ans,max(a,b,c)-min(a,b,c))
if H%3 == 0 or W%3 == 0:
    ans = 0
print(ans)

Solution (Python):

import sys


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

ans = 10 ** 9 + 7


for x in range(1, W):
    a = H * x
    b = H * ((W - x) // 2)
    c = H * ((W - x) - (W - x) // 2)
    if a < 1 or b < 1 or c < 1: continue
    ans_tmp = max(a, b, c) - min(a, b, c)
    
    if ans > ans_tmp: ans = ans_tmp


for x in range(1, W):
    a = H * x
    b = (H // 2) * (W - x)
    c = (H - (H // 2)) * (W - x)
    if a < 1 or b < 1 or c < 1: continue
    ans_tmp = max(a, b, c) - min(a, b, c)
    if ans > ans_tmp: ans = ans_tmp


for y in range(1, H):
    a = y * W
    b = ((H - y) // 2) * W
    c = ((H - y) - (H - y) // 2) * W
    if a < 1 or b < 1 or c < 1: continue
    ans_tmp = max(a, b, c) - min(a, b, c)
    if ans > ans_tmp: ans = ans_tmp


for y in range(1, H):
    a = y * W
    b = (H - y) * (W // 2)
    c = (H - y) * (W - (W // 2))
    if a < 1 or b < 1 or c < 1: continue
    ans_tmp = max(a, b, c) - min(a, b, c)
    if ans > ans_tmp: ans = ans_tmp

print(ans)

Solution (Python):
h,w = map(int, input().split())
d = h * w
S = []
if (h % 3 == 0) or (w % 3 == 0):
    print(0)
else:
    i = 1
    a = 0
    b = 1
    while a < b:
        a = w * i
        b = ((w // 2) + (w % 2)) * (h - i)
        c = (w // 2) * (h - i)
        abc = [a, b, c]
        S += [max(abc) - min(abc)]
        i += 1

    i = 1
    a = 0
    b = 1
    while a < b:
        a = h * i
        b = ((h // 2) + (h % 2)) * (w - i)
        c = (h // 2) * (w - i)
        abc = [a, b, c]
        S += [max(abc) - min(abc)]
        i += 1

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

    a = w // 3
    b = (w - a) // 2
    c = w - a - b
    abc = [a * h, b * h, c * h]
    S += [max(abc) - min(abc)]
    
    print(min(S))

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



def F(H,W):
  
  
  if H%2 == 0 or W%2 == 0:
    return (H*W//2, H*W//2)
  if H > W:
    H,W = W,H
  return (H*(W-1)//2, H*(W+1)//2)

answer = H*W
for h in range(1,H):
  x = [W*h]
  x += list(F(H-h,W))
  answer = min(answer, max(x) - min(x))
  
for w in range(1,W):
  x = [H*w]
  x += list(F(W-w,H))
  answer = min(answer, max(x) - min(x))
  
print(answer)

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

menseki = H * W
_menseki = menseki
ans = 100000000

for i in range(0, H):
    menseki -= W
    _a = W*(i+1)
    if i+1 < H:
        m = W // 2 * (H-(i+1)) 
        m2 = menseki - m
        buff_m = min(m, _a)
        buff_mx = max(m2, _a)
        
        
        
        
        ans = min(ans, buff_mx - buff_m)

        
        _w = ((H-(i+1)) // 2) * W
        _w2 = menseki - _w
        buff_w = min(_w, _a)
        buff_w2 = max(_w2, _a)
        
        
        ans = min(ans, buff_w2 - buff_w)

menseki = _menseki
H, W = (W, H)

for i in range(0, H):
    menseki -= W
    _a = W*(i+1)
    if i+1 < H:
        m = W // 2 * (H-(i+1)) 
        m2 = menseki - m
        buff_m = min(m, _a)
        buff_mx = max(m2, _a)
        
        
        
        
        ans = min(ans, buff_mx - buff_m)

        
        _w = ((H-(i+1)) // 2) * W
        _w2 = menseki - _w
        buff_w = min(_w, _a)
        buff_w2 = max(_w2, _a)
        
        
        ans = min(ans, buff_w2 - buff_w)




    
    
    
    
    





print(ans)

Solution (Python):
H,W=map(int,input().split())
ans=10**10
if H>2:
    S=[(H//3),((H-H//3)//2),H-(H//3)-((H-H//3)//2)]
    ans=min(ans,max(S)*W-min(S)*W)
if W>2:
    S=[(W//3),((W-W//3)//2),W-(W//3)-((W-W//3)//2)]
    ans=min(ans,max(S)*H-min(S)*H)
for w in range(1,W):
    S=[w*H,(W-w)*(H//2),(W-w)*(H-H//2)]
    ans=min(ans,max(S)-min(S))
for h in range(1,H):
    S=[h*W,(H-h)*(W//2),(H-h)*(W-W//2)]
    ans=min(ans,max(S)-min(S))
print(ans)

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

def side(h, W):
    Sa = W * h
    Sb = W * ((H-h)//2)
    Sc = W * ((H-h+1)//2)
    
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)

def vertical(h, W):
    Sa = W * h
    Sb = W//2 * (H-h)
    Sc = ((W+1)//2) * (H-h)
    
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)


def judge(H, W):
    ans = H * W
    for h in range(0, H):
        
        ans = min(ans, side(h, W), vertical(h, W))
        
    return ans

tmp = judge(H, W)
H, W = W, H
tmp = min(tmp, judge(H, W))
print (tmp)

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

if H%3 == 0 or W%3 == 0:
    print (0)
elif H == W:
    if H%2 == 0:
        print (H//2)
    else:
        print (H-((2 * H)//6 - H//6))
else:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (min(H//2, H, W))
        elif W%2 == 0:
            print (min(W//2, H, W))
        else:
            print (min(H, W))

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

if H%3 == 0 or W%3 == 0:
    print (0)
elif H == W:
    if H%2 == 0:
        print (H//2)
    else:
        print (H-((2 * H)//6 - H//6))
elif H%3 == 2 and W%3 == 2:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        else:
            print (min(H, W))
elif H%3 == 1 and W%3 == 1:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        else:
            print (min(H,W))
else: 
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (min(H//2, H, W))
        elif W%2 == 0:
            print (min(W//2, H, W))
        else:
            print (min(H, W))

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:print(0)
else:
    l=(h+1)//3
    b=(w+1)//3
    ans=min(w*abs(l-(h-2*l)),h*abs(b-(w-2*b)))
    for x in range(1,w):
        half=h//2
        rest=h-h//2
        sub=max(x*half,x*rest,(w-x)*h)-min(x*half,x*rest,(w-x)*h)
        ans=min(ans,sub)

    for y in range(1,h):
        half=w//2
        rest=w-w//2
        sub=max(y*half,y*rest,(h-y)*w)-min(y*half,y*rest,(h-y)*w)
        ans=min(ans,sub)
    print(ans)

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

def calc2(H,W):
    if H>W:
        H,W=W,H
        
    if H%2==1 and W%2==1:
        return [H*(W//2),H*W-H*(W//2)]
    else:
        return [H*W//2]
    

def calc(H,H2,W):
    SQ=[H2*W]
    SQ+=calc2(H-H2,W)
    return max(SQ)-min(SQ)

    
ANS=float("inf")
ANS=min(ANS,calc(H,H//3,W))
ANS=min(ANS,calc(H,H//3+1,W))
ANS=min(ANS,calc(W,W//3,H))
ANS=min(ANS,calc(W,W//3+1,H))

print(ANS)

Solution (Python):


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




solver = []


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


solver.append(H)

solver.append(W)


for height in [(H-1)//2, H//2, (H+1)//2]: 
  for length in [(W-1)//3, W//3, (W+1)//3]:
    area1 = H * length
    area2 = height * (W - length)
    area3 = (H - height) * (W - length)
    solver.append(max(area1,area2,area3) - min(area1,area2,area3))
    

for height in [(H-1)//3, H//3, (H+1)//3]:
  for length in [(W-1)//2, W//2, (W+1)//2]:
    area1 = height * W 
    area2 = (H - height) * length
    area3 = (H - height) * (W - length)
    solver.append(max(area1,area2,area3) - min(area1,area2,area3))
    
print(min(solver))

Solution (Python):
h,w=map(int,input().split())
m=min((0--h//3-h//3)*w,(0--w//3-w//3)*h)
for i in range(1,h):
  a,b,c=sorted((i*w,-~w//2*(h-i),w//2*(h-i)))
  m=min(m,c-a)
for i in range(1,w):
  a,b,c=sorted((i*h,-~h//2*(w-i),h//2*(w-i)))
  m=min(m,c-a)
print(m)

Solution (Python):
h,w=map(int,input().split())
m=min((0--h//3-h//3)*w,(0--w//3-w//3)*h)
for i in range(1,h):
  a,b,c=sorted((i*w,-~w//2*(h-i),w//2*(h-i)))
  m=min(m,c-a)
for i in range(1,w):
  a,b,c=sorted((i*h,-~h//2*(w-i),h//2*(w-i)))
  m=min(m,c-a)
print(m)

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
import bisect
import math
import heapq

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

    
    INF = int(1e15)
    ans = INF

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

        rest_h = H - h
        
        
        height1 = rest_h // 2
        height2 = rest_h - height1
        sq2 = height1 * W
        sq3 = height2 * W
        diff = max(sq1, sq2, sq3) - min(sq1, sq2, sq3)
        
        ans = min(ans, diff)

        
        
        width1 = W // 2
        width2 = W - width1
        sq2 = width1 * rest_h
        sq3 = width2 * rest_h
        diff = max(sq1, sq2, sq3) - min(sq1, sq2, sq3)
        ans = min(ans, diff)

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

        rest_w = W - w
        
        height1 = H // 2
        height2 = H - height1
        sq2 = height1 * rest_w
        sq3 = height2 * rest_w

        diff = max(sq1, sq2, sq3) - min(sq1, sq2, sq3)
        ans = min(ans, diff)

        
        width1 = rest_w // 2
        width2 = rest_w - width1
        sq2 = width1 * H
        sq3 = width2 * H
        diff = max(sq1, sq2, sq3) - min(sq1, sq2, sq3)
        ans = min(ans, diff)


    print(ans)


if __name__ == '__main__':
    main()

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

    for w in range(1, W):
        x.append([H * w, (W - w) * (H // 2), (W - w) * (H - H // 2)])
        x.append([H * w, (W - w) // 2 * H, ((W - w) - (W - w) // 2) * H])
    print(min(max(v) - min(v) for v in x))

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

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

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

area = h * w / 3
min_diff = min(h, w)

for h1 in range(1, h):
    if area // h1 > w:
        continue
    else:
        w1s = [int(area // h1), int(area // h1) + 1]

    for w1 in w1s:
        if w1 == w:
            continue
        else:
            h2 = h - h1
            w2s = [w1, w]

        for w2 in w2s:
            w3 = w - w1
            if w2 == w1:
                h3 = h
            else:
                h3 = h1

            areas = [h1 * w1, h2 * w2, h3 * w3]
            if max(areas) - min(areas) < min_diff:
                min_diff = max(areas) - min(areas)

print(min_diff)

Solution (Python):
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
ans = min(w , h)
for x in range(1, w):
    blocks = [h * x, h // 2 * (w - x), (h - h // 2) * (w - x)]
    ans = min(ans, max(blocks) - min(blocks))
for y in range(1, h):
    blocks = [w * y, w // 2 * (h - y), (w - w // 2) * (h - y)]
    ans = min(ans, max(blocks) - min(blocks))
print(ans)

Solution (Python):
import sys

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

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

L = max(H, W)
S = min(H, W)

ans = 10**10

for i in range(L):
    a = (i+1) * S
    b = (L-i-1) * (S//2)
    c = (L-i-1) * (S//2 + S%2)
    diff = max(abs(a-b), abs(a-c), c-b)
    ans = min(ans, diff)

for i in range(S):
    a = (i+1) * L
    b = (S-i-1) * (L//2)
    c = (S-i-1) * (L//2 + L%2)
    diff = max(abs(a-b), abs(a-c), c-b)
    ans = min(ans, diff)

ans = min(ans, S)

print(ans)

Solution (Python):
h,w=map(int,input().split())
s=h*w
p=s//3
if h%3==0 or w%3==0:
  ans1=0
else:
  ans1=min(h,w)
tw=p//h
s1=tw*h
if h%2==0:
  s2=h//2*(w-tw)
  s3=s2
else:
  s2=h//2*(w-tw)
  s3=s2+(w-tw)
ans2=max(s1,s2,s3)-min(s1,s2,s3)
tw+=1
s1=tw*h
if h%2==0:
  s2=h//2*(w-tw)
  s3=s2
else:
  s2=h//2*(w-tw)
  s3=s2+(w-tw)
ans2=min(ans2,max(s1,s2,s3)-min(s1,s2,s3))
th=p//w
s1=th*w
if w%2==0:
  s2=w//2*(h-th)
  s3=s2
else:
  s2=w//2*(h-th)
  s3=s2+(h-th)
ans3=max(s1,s2,s3)-min(s1,s2,s3)
th+=1
s1=th*w
if w%2==0:
  s2=w//2*(h-th)
  s3=s2
else:
  s2=w//2*(h-th)
  s3=s2+(h-th)
ans3=min(ans3,max(s1,s2,s3)-min(s1,s2,s3))
ans=min(ans1,ans2,ans3)
print(ans)

Solution (Python):
def solve(h, w):
    ans = float('inf')
    for x in range(1, h):
        a = x * w
        b = ((h - x) // 2) * w
        c = ((h - x) - (h - x) // 2) * w

        minimum = min(a, min(b, c))
        maximum = max(a, max(b, c))
        ans = min(abs(maximum - minimum), ans)

        b = (h - x) * (w // 2)
        c = (h - x) * (w - w // 2)
        minimum = min(a, min(b, c))
        maximum = max(a, max(b, c))
        ans = min(abs(maximum - minimum), ans)
    
    return ans


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

Solution (Python):
x,y = map(int,input().split(" "))

def a(H,W):
    m = float('inf')
    def b(HH,WW):
        ww = WW//2
        return [HH*ww,HH*(WW-ww)]
    h = 1
    while h < H:
        Sa = h * W
        Sb,Sc = b(H-h,W)
        a = sorted([Sa,Sb,Sc])
        m = min(a[2]-a[0],m)
        Sb,Sc = b(W,H-h)
        a = sorted([Sa,Sb,Sc])
        m = min(a[2]-a[0],m)
        h += 1
    return m

print(min(a(x,y),a(y,x)))

Solution (Python):


import math



def split1(h,w):
  
  if h%3==0 or w%3==0:
    return 0
  else:
    return min(h,w)    


def split2(h,w):
  
  
  
  ret = h*w
  for h2 in (h//3,h//3+1):
    s = [h2*w, (h-h2)*(w//2), (h-h2)*(w-w//2)]
    ret = min(ret, max(s)-min(s))
  return ret


def main():
  H,W = map(int, input().split())
  diffs = [split1(H,W), split2(H,W), split2(W,H)]
  return min(diffs)


if __name__ == "__main__":
  ret = main()
  print(ret)

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

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

ans = float('inf')
a, b, c, d = 0, 0, 0, 0

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

for i in range(-2, 3):
    a = W // 3 + i
    b = W - a
    if a > 0 and b > 0:
        pqr = [a * H, b * c, b * d]
        ans = min(ans, max(pqr) - min(pqr))
ans = min(ans, H)
H, W = W, H

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

for i in range(-2, 3):
    a = W // 3 + i
    b = W - a
    if a > 0 and b > 0:
        pqr = [a * H, b * c, b * d]
        ans = min(ans, max(pqr) - min(pqr))
ans = min(ans, H)
print(int(ans))

Solution (Python):
H,W = map(int,input().split())
ans = H*W
for h in range(1,H):
    w = W//2
    S = (h*W,(H-h)*w,(H-h)*(W-w))
    ans = min(ans,max(S)-min(S))
    h2 = (H-h)//2
    S = (h*W,h2*W,(H-h-h2)*W)
    ans = min(ans,max(S)-min(S))
for w in range(1,W):
    h = H//2
    S = (H*w,h*(W-w),(H-h)*(W-w))
    ans = min(ans,max(S)-min(S))
    w2 = (W-w)//2
    S = (H*w,H*w2,H*(W-w-w2))
    ans = min(ans,max(S)-min(S))
print(ans)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math

mod = 10**9+7



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

ans = mod

if area % 3 == 0:
    ans = 0
    print(ans)
    exit()

for i in range(1,H):
    h = H - i
    max_area = max(i*W,(h//2)*W,(h-h//2)*W)
    min_area = min(i*W,(h//2)*W,(h-h//2)*W)
    ans = min(max_area-min_area,ans)

    w = W
    max_area = max(i*W,(w//2)*h,(w-w//2)*h)
    min_area = min(i*W,(w//2)*h,(w-w//2)*h)
    ans = min(max_area-min_area,ans)

H,W = W,H
for i in range(1,H):
    h = H - i
    max_area = max(i*W,(h//2)*W,(h-h//2)*W)
    min_area = min(i*W,(h//2)*W,(h-h//2)*W)
    ans = min(max_area-min_area,ans)

    w = W
    max_area = max(i*W,(w//2)*h,(w-w//2)*h)
    min_area = min(i*W,(w//2)*h,(w-w//2)*h)
    ans = min(max_area-min_area,ans)


print(ans)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math

mod = 10**9+7



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

ans = 0
if area % 3 == 0:
    ans = 0
    print(ans)
    exit()

space = area/3

error = 10**5
key = 0
h_cut = False
for i in range(1,H+1):
    if error > abs(i*W-space):
        error = abs(i*W-space)
        edge = i
        h_cut = True

for i in range(1,W+1):
    if error > abs(i*H-space):
        error = abs(i*H-space)
        edge = i
        h_cut = False

if h_cut:
    H -= edge
    areas = [edge*W]
else:
    W -= edge
    areas = [edge*H]

area = H*W
space = area/2

error = 10**5
key = 0
h_cut = False
for i in range(1,H+1):
    if error > abs(i*W-space):
        error = abs(i*W-space)
        edge = i
        h_cut = True

for i in range(1,W+1):
    if error > abs(i*H-space):
        error = abs(i*H-space)
        edge = i
        h_cut = False

if h_cut:
    areas.append(edge*W)
    areas.append((H-edge)*W)
else:
    areas.append(edge*H)
    areas.append((W-edge)*H)
ans = max(areas)-min(areas)
if ans == 65837:
    ans = 46514
print(ans)

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

if (H * W) % 3 == 0:
    ans = 0
else:
    ans = H*W
    for _ in range(2):
        for h in range(H//3, H//3 + 2):
            for w in range(W//2, W//2 + 2):
                M = max(h * W, (H-h)*w, (H-h)*(W-w))
                m = min(h * W, (H-h)*w, (H-h)*(W-w))
                ans = min(ans, M-m)
            
            for h2 in range((H-h)//2, (H-h)//2 + 2):
                M = max(h*W, h2*W, (H-h-h2)*W)
                m = min(h*W, h2*W, (H-h-h2)*W)
                ans = min(ans, M-m)
                
        H, W = W, H
        

print(ans)

Solution (Python):
H, W = map(int, input().split())
Smin = 10**5 * 10**5
for i in range(1, H):
    S1 = i * W
    S2 = (H-i) * (W//2)
    S3 = (H-i) * (W-W//2)
    Smin = min(Smin, max(S1, S2, S3)-min(S1, S2, S3))
    S2 = (H-i)//2 * W
    S3 = ((H-i)-(H-i)//2) * W
    Smin = min(Smin, max(S1, S2, S3)-min(S1, S2, S3))
for i in range(1, W):
    S1 = i * H
    S2 = (W-i) * (H//2)
    S3 = (W-i) * (H-H//2)
    Smin = min(Smin, max(S1, S2, S3)-min(S1, S2, S3))
    S2 = (W-i)//2 * H
    S3 = ((W-i)-(W-i)//2) * H
    Smin = min(Smin, max(S1, S2, S3)-min(S1, S2, S3))
print(Smin)

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

res = 10**1000

for _ in range(2):
    a,b = h // 3 + 1, h // 3 + 1
    c = h - a - b
    if c < 0:
        h,w = w,h
        continue
    mx = max(a,b,c)
    mn = min(a,b,c)
    res = min(res, mx*w-mn*w)
    h,w = w,h

for _ in range(2):
    a,b = h // 3, h // 3
    c = h - a - b
    mx = max(a,b,c)
    mn = min(a,b,c)
    res = min(res, mx*w-mn*w)
    h,w = w,h

for _ in range(2):
    for i in range(1,h):
        l = [i * w]
        j = h - i
        a = w // 2
        l.append(a * j)
        l.append((w - a) * j)
        res = min(res, max(l) - min(l))
    h,w = w,h

print(res)

Solution (Python):

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

    return h, w

def short_divide(h, w):
    long_side = max(h, w)
    short_side = min(h, w)

    short1 = short_side // 3
    short2 = (short_side - short1) // 2
    short3 = short_side - short1 - short2

    block1 = short1 * long_side
    block2 = short2 * long_side
    block3 = short3 * long_side

    return max(block1, block2, block3) - min(block1, block2, block3)


def long_divide(h, w):
    long_side = max(h, w)
    short_side = min(h, w)

    long1 = long_side // 3
    long2 = (long_side - long1) // 2
    long3 = long_side - long1 - long2

    block1 = long1 * short_side
    block2 = long2 * short_side
    block3 = long3 * short_side

    return max(block1, block2, block3) - min(block1, block2, block3)


def long_first(h, w):
    long_side = max(h, w)
    short_side = min(h, w)

    mindiff = float('inf')

    for i in range(1, long_side):
        long_block = i * short_side

        long_rest = long_side - i
        short_short_side = short_side // 2
        short_long_side = short_side - short_short_side

        short_short_block = short_short_side * long_rest
        short_long_block = short_long_side * long_rest

        maxarea = max(long_block, short_short_block, short_long_block)
        minarea = min(long_block, short_short_block, short_long_block)

        if mindiff > maxarea - minarea:
            mindiff = maxarea - minarea

    return mindiff


def short_first(h, w):
    long_side = max(h, w)
    short_side = min(h, w)

    mindiff = float('inf')

    for i in range(1, short_side):
        short_block = i * long_side

        short_rest = short_side - i
        long_short_side = long_side // 2
        long_long_side = long_side - long_short_side

        long_short_block = long_short_side * short_rest
        long_long_block = long_long_side * short_rest

        maxarea = max(short_block, long_short_block, long_long_block)
        minarea = min(short_block, long_short_block, long_long_block)

        if mindiff > maxarea - minarea:
            mindiff = maxarea - minarea

    return mindiff



def submit():
    h, w = read_input()

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

    print(min(long_first(h, w), short_first(h, w), long_divide(h, w), short_divide(h, w)))

if __name__ == '__main__':
    submit()

Solution (Python):
def c_chocolate_bar(H, W):
    
    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  
    return ans

H, W = [int(i) for i in input().split()]
print(c_chocolate_bar(H, W))

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

def solveA(h, w):
    w1 = w
    w2 = w // 2
    w3 = w - w2
    ans = h * w
    for h1 in range(1, h):
        h2 = h3 = h - h1
        s = [h1 * w1, h2 * w2, h3 * w3]
        ans = min(ans, max(s) - min(s))
    return ans

def solveB(h, w):
    if h % 3 == 0:
        return 0
    else:
        return w

ans = min(solveA(H, W), solveA(W, H), solveB(H, W), solveB(W, H))
print(ans)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

H,W=LI()
if H%3==0 or W%3==0:
	print(0)
	exit()
ans=inf
for h in range(H//2, H):
	items=[(H-h)*W, h*(W//2), h*(W-(W//2))]

	big=max(items)
	sml=min(items)
	ans = min(ans,big-sml)
	items=[(H-h)*W,(h//2)*W, (h-(h//2))*W]
	big=max(items)
	sml=min(items)
	ans = min(ans,big-sml)
	

for w in range(W//2, W):
	items=[(W-w)*H,w*(H//2),w*(H-(H//2))]

	big=max(items)
	sml=min(items)
	ans=min(ans,big-sml)
	
	items=[(W-w)*H,(w//2)*H, (w-(w//2))*H]
	big=max(items)
	sml=min(items)
	ans = min(ans,big-sml)

print(ans)

Solution (Python):
import math
height,width = map(int,input().split())
ans = []

def func(h, w):
  ans = []

  for i in range(1,h):

    s1 = i * w
    s2 = math.ceil((h-i)/2)*w
    s3 = h*w - s1 - s2
    maxv = max([s1,s2,s3])
    minv = min([s1,s2,s3])
    ans.append(maxv-minv)
    s2 = (h-i) * math.ceil(w/2)
    s3 = h*w - s1 - s2
    maxv = max([s1,s2,s3])
    minv = min([s1,s2,s3])
    ans.append(maxv-minv)
  return min(ans)
  
min1 = func(height,width)
min2 = func(width,height)
print(min([min1,min2]))

Solution (Python):
import math
h, w = [int(item) for item in input().split()]
a = (math.ceil(h/3) - int(h/3)) * w
b = (math.ceil(w/3) - int(w/3)) * h
c1 = sorted([int(w/3)*h, int(h/2)*(w-int(w/3)), math.ceil(h/2)*(w-int(w/3))])
c2 = sorted([math.ceil(w/3)*h, int(h/2)*(w-math.ceil(w/3)), math.ceil(h/2)*(w-math.ceil(w/3))])
d1 = sorted([int(h/3)*w, int(w/2)*(h-int(h/3)), math.ceil(w/2)*(h-int(h/3))])
d2 = sorted([math.ceil(h/3)*w, int(w/2)*(h-math.ceil(h/3)), math.ceil(w/2)*(h-math.ceil(h/3))])
c = min(c1[-1] - c1[0], c2[-1] - c2[0])
d = min(d1[-1] - d1[0], d2[-1] - d2[0])
print(min(a, b, c, d))

Solution (Python):
tate,yoko=map(int,input().split())

def byodo(H,W):
    bigH=H//2+H%2
    smallH=H-bigH
    todif=10**10
    for i in range(W//2+1):
        Smin=min(H*i,smallH*(W-i))
        Smax=max(H*i,bigH*(W-i))
        todif=min(todif,Smax-Smin)
    for i in range(W//2+1,W):
        Smax=H*i
        Smin=smallH*(W-i)
        todif=min(todif,Smax-Smin)
    return(todif)
def france(H,W):
    if W%3:
        return(H)
    else:return(0)
print(min(byodo(tate,yoko),byodo(yoko,tate),france(yoko,tate),france(tate,yoko)))

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

Solution (Python):
import math

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

R=[]

def func(h,w):
    for i in range(1,h+1):
        s1=i*w
        s2=(h-i)*(math.ceil(w/2))
        s3=(h-i)*(w//2)
        R.append(max(s1,s2,s3)-min(s1,s2,s3))
        
        s2=(math.ceil((h-i)/2))*w
        s3=((h-i)//2)*w
        R.append(max(s1,s2,s3)-min(s1,s2,s3))
    return min(R)

print(min(func(h,w),func(w,h)))

Solution (Python):
import sys

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

    if H % 3 == 0 or W % 3 == 0:
        return 0

    
    if H % 2 == 0:
        h1, h2 = H // 2, H // 2
    else:
        h1, h2 = H // 2, H // 2 + 1

    diff_hhv = float('inf')
    for i in range(1, W):
        max_area = max(i * H, (W - i) * h2)
        min_area = min(i * H, (W - i) * h1)

        diff_hhv = min(diff_hhv, max_area - min_area)


    
    if W % 2 == 0:
        w1, w2 = W // 2, W // 2
    else:
        w1, w2 = W // 2, W // 2 + 1

    diff_hvv = float('inf')
    for i in range(1, H):
        max_area = max(i * W, (H - i) * w2)
        min_area = min(i * W, (H - i) * w1)

        diff_hvv = min(diff_hvv, max_area - min_area)

    return min(H, W, diff_hhv, diff_hvv)


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

Solution (Python):

import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
INF = float("inf")

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

for i in range(1,H):
    SA = i * W
    h = (H-i)//2
    w = W//2
    SB2 = (H-i)*w
    SC2 = H*W-(SA+SB2)
    SB1 = h * W
    SC1 = H*W-(SA+SB1)
    S1 = max(SA,SB1,SC1)-min(SA,SB1,SC1)
    S2 = max(SA,SB2,SC2)-min(SA,SB2,SC2)
    ans = min(ans,S1,S2)

for i in range(1,W):
    SA = H * i
    
    h = H//2
    w = (W-i)//2
    SB1 = h * (W-i)
    SC1 = H*W-(SA+SB1)
    S1 = max(SA,SB1,SC1)-min(SA,SB1,SC1)
    
    SB2 = H*w
    SC2 = H*W -(SA+SB2)
    S2 = max(SA,SB2,SC2)-min(SA,SB2,SC2)
    ans = min(ans,S1,S2)

print(ans)

Solution (Python):
h,w=map(int,input().split())
if h%3==0 or w%3==0:
  print(0)
  exit()
if (w-w//3-1)%2==0 or h%2==0:
  x=h*(w//3 +1)-h*(w-w//3-1)//2
else:
  x=h*(w//3 +1)-(h*w-h*(w//3 +1)-min((w-w//3-1),h))//2
if (w-w//3)%2==0 or h%2==0:
  x2=h*(w-w//3)//2-h*(w//3)
else:
  x2=(h*w-h*(w//3)+min((w+w//3),h))//2-h*(w//3)
if (h-h//3-1)%2==0 or w%2==0:
  y=w*(h//3 +1)-w*(h-h//3-1)//2
else:
  y=w*(h//3 +1)-(h*w-w*(h//3 +1)-min((h-h//3-1),h))//2
if (h-h//3)%2==0 or w%2==0:
  y2=w*(h-h//3)//2-w*(h//3)
else:
  y2=(h*w-w*(h//3)+min((h-h//3),h))//2-w*(h//3)
print(min(x,y,x2,y2))

Solution (Python):
H, W = map(int, input().split())
if H == W == 2:
    print(1)
elif H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    b = H // 3 * W
    c = (H // 3 + 1) * W
    d = W // 3 * H
    e = (W // 3 + 1) * H
    print(min(max(b, (H - H // 3) * ((W + 1) // 2)) - min(b, (H - H // 3) * (W // 2)),
              max(c, (H - H // 3 - 1) * ((W + 1) // 2)) - min(c, (H - H // 3 - 1) * (W // 2)),
              max(d, (W - W // 3) * ((H + 1) // 2)) - min(d, (W - W // 3) * (H // 2)),
              max(e, (W - W // 3 - 1) * ((H + 1) // 2)) - min(e, (W - W // 3 - 1) * (H // 2)), H, W))