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):
class AtCoder:
    def main(self):
        H, W = map(int, input().split())

        print(min(self.f(H, W), self.f(W, H)))

    def f(self, H: int, W: int) -> int:
        ans = None
        for h in range(1, H):
            Sa = h * W

            half_w = W // 2
            Sb = (H - h) * half_w
            Sc = (H - h) * (W - half_w)
            S1 = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)

            half_h = (H - h) // 2
            Sb = half_h * W
            Sc = (H - h - half_h) * W
            S2 = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
            S = min(S1, S2)
            if ans is None:
                ans = S
            else:
                ans = min(ans, S)
        return ans


if __name__ == '__main__':
    AtCoder().main()

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

if H > W:
    H, W = W, H

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

ans = H

b = (H // 2)

for w in range(W+1):
    tmp_1, tmp_2, tmp_3 = H * w, b * (W-w), (H-b) * (W-w)
    tmp = max(tmp_1, tmp_2, tmp_3) - min(tmp_1, tmp_2, tmp_3)
    ans = min(ans, tmp)

d = (W // 2)
for h in range(H+1):
    tmp_1, tmp_2, tmp_3 = h*W, d * (H-h), (W-d) * (H-h)
    tmp = max(tmp_1, tmp_2, tmp_3) - min(tmp_1, tmp_2, tmp_3)
    ans = min(ans, tmp)
print(ans)

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

h1 = H//3
h2 = h1 + 1
w1 = W//3
w2 = w1 + 1
hh = [h1, h2]
ww = [w1, w2]

ans = float('inf')
for i in range(2):
    s1 = hh[i] * W
    if W % 2 == 0:
        s2 = (W//2) * (H-hh[i])
        s3 = s2
    else:
        s2 = (W//2) * (H-hh[i])
        s3 = s2 + H - hh[i]
    ans = min(ans, max(s1, s2, s3)-min(s1, s2, s3))
    if (H-hh[i]) % 2 == 0:
        s2 = ((H-hh[i])//2) * W
        s3 = s2
    else:
        s2 = ((H-hh[i])//2) * W
        s3 = s2 + W
    ans = min(ans, max(s1, s2, s3)-min(s1, s2, s3))
    s1 = ww[i] * H
    if H % 2 == 0:
        s2 = (H//2) * (W-ww[i])
        s3 = s2
    else:
        s2 = (H//2) * (W-ww[i])
        s3 = s2 + W - ww[i]
    ans = min(ans, max(s1, s2, s3)-min(s1, s2, s3))
    if (W-ww[i]) % 2 == 0:
        s2 = ((W-ww[i])//2) * H
        s3 = s2
    else:
        s2 = ((W-ww[i])//2) * H
        s3 = s2 + H
    ans = min(ans, max(s1, s2, s3)-min(s1, s2, s3))

print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    H, W = (int(i) for i in input().split())
    ans = 100000
    for h in range(1,H):
        s1 = set()
        s1.add(h*W)
        w = W // 2
        s1.add((H-h) * w)
        s1.add((H-h) * (W-w))
        ans1 = max(s1) - min(s1)

        s2 = set()
        s2.add(h*W)
        hb = (H-h)//2
        hc = H - h - hb
        s2.add(hb*W)
        s2.add(hc*W)
        ans2 = max(s2) - min(s2)

        ans = min(ans,ans1,ans2)

    H,W = W,H
    for h in range(1,H):
        s1 = set()
        s1.add(h*W)
        w = W // 2
        s1.add((H-h) * w)
        s1.add((H-h) * (W-w))
        ans1 = max(s1) - min(s1)

        s2 = set()
        s2.add(h*W)
        hb = (H-h)//2
        hc = H - h - hb
        s2.add(hb*W)
        s2.add(hc*W)
        ans2 = max(s2) - min(s2)

        ans = min(ans,ans1,ans2)

    print(ans)

if __name__ == '__main__':
    main()

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

if h%3==0 or w%3 ==0:
    print(answer)
else:
    min1 = h*w
    min2 = h*w
    boxs = [0,0,h*w]
    for i in range(0,w):
        if h%2==0:
            boxs[0] += int(h//2)
            boxs[1] += int(h//2)
            boxs[2] -= h
            min1 = min(max(boxs)-min(boxs),min1)
        if h%2 ==1:
            boxs[0] += int(h//2)+1
            boxs[1] += int(h//2)
            boxs[2] -= h
            min1 = min(max(boxs)-min(boxs),min1)
    boxs = [0,0,h*w]      
    for i in range(0,h):
        if w%2==0:
            boxs[0] += int(w//2)
            boxs[1] += int(w//2)
            boxs[2] -= w
            min2 = min(max(boxs)-min(boxs),min2)
        if w%2 ==1:
            boxs[0] += int(w//2)+1
            boxs[1] += int(w//2)
            boxs[2] -= w
            min2 = min(max(boxs)-min(boxs),min2)
    print(min(min1,min2,h,w))

Solution (Python):

def getMinSDiff(H, W):
    Ans = H*W
    for x in range(1, W):
        
        S1 = H*x
        S2 = (W - x) * (H // 2)
        S3 = H*W - (S1 + S2)
        Ans1 = max(S1, S2, S3) - min(S1, S2, S3)

        
        S1 = x*H
        S2 = (W - x) // 2 * H
        S3 = H*W - (S1 + S2)
        Ans2 = max(S1, S2, S3) - min(S1, S2, S3)

        
        Ans = min(Ans, Ans1, Ans2)
    return Ans

if __name__ == "__main__":
    H, W = map(int, input().split(" "))
    if (W % 3 == 0) or (H % 3 == 0):
        print("0")
        exit()
    else:
        Ans = min(getMinSDiff(H, W), getMinSDiff(W, H))
        print(Ans)
        exit()

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

ans = h*w
if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    ct = h
    for cy in range(1, w):
        c0 = ct * cy
        c1 = (w-cy) * (ct//2)
        c2 = h*w - c0 - c1
        cmax = max(c0, c1, c2)
        cmin = min(c0, c1, c2)
        
        if cmin > 0:
            ans = min(ans, cmax - cmin)
    ct = w
    for cy in range(1, h):
        c0 = ct * cy
        c1 = (h-cy) * (ct//2)
        c2 = h*w - c0 - c1
        cmax = max(c0, c1, c2)
        cmin = min(c0, c1, c2)
        
        if cmin > 0:
            ans = min(ans, cmax - cmin)

print(min(ans, h, w))

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

def split_into_two(x, y):
    if x % 2 == 0 or y % 2 == 0:
        return x*y//2, x*y//2
    else:
        if x > y:
            x, y = y, x
        return (y//2)*x, (y//2 + 1)*x

ans = h * w
for i in range(1, h//2+2):
    s0 = w*i
    s1, s2 = split_into_two(h - i, w)
    ans = min(ans, max(s0, s1, s2) - min(s0, s1, s2))

for i in range(1, w//2+2):
    s0 = h*i
    s1, s2 = split_into_two(h, w - i)
    ans = min(ans, max(s0, s1, s2) - min(s0, s1, s2))

print(ans)

Solution (Python):
def area(a1,a2,b):
    s1=a1*b
    if b%2==0:
        s2=a2*b//2
        s3=s2
    else:
        s2=a2*(b//2+1)
        s3=a2*(b//2)
    return max(s1,s2,s3)-min(s1,s2,s3)

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()
"""
ans2_1 = 10 ** 10
ans2_2 = 10 ** 10

ans3_1 = 10 ** 10
ans3_2 = 10 ** 10
"""

ans1 = W
ans4 = H


h1 = (H + 2) // 3
h2 = H - h1
ans2_1=area(h1,h2,W)

h1 = H // 3
h2 = H - h1
ans2_2=area(h1,h2,W)

w1 = (W + 2) // 3
w2 = W - w1
ans3_1=area(w1,w2,H)

w1 = W // 3
w2 = W - w1
ans3_2=area(w1,w2,H)

print(int(min(ans1, ans2_1, ans2_2, ans3_1, ans3_2, ans4)))

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

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

ans2_1 = 10 ** 10
ans2_2 = 10 ** 10
ans2_3 = 10 ** 10
ans2_4 = 10 ** 10
ans3_1 = 10 ** 10
ans3_2 = 10 ** 10
ans3_3 = 10 ** 10
ans3_4 = 10 ** 10

ans1 = W
ans4 = H

h1 = (H + 2) // 3
h2 = H - h1
s1 = h1 * W
if W % 2 == 0:
    ans2_1 = abs(s1 - h2 * (W // 2))
else:
    s2 = h2 * (W // 2 + 1)
    s3 = h2 * (W // 2)
    ans2_2 = max(s1, s2, s3) - min(s1, s2, s3)

h1 = H // 3
h2 = H - h1
s1 = h1 * W
if W % 2 == 0:
    ans2_3 = abs(s1 - h2 * (W // 2))
else:
    s2 = h2 * (W // 2 + 1)
    s3 = h2 * (W // 2)
    ans2_4 = max(s1, s2, s3) - min(s1, s2, s3)

w1 = (W + 2) // 3
w2 = W - w1
s1 = H * w1
if H % 2 == 0:
    ans3_1 = abs(s1 - w2 * (H // 2))
else:
    s2 = w2 * (H // 2 + 1)
    s3 = w2 * (H // 2)
    ans3_2 = max(s1, s2, s3) - min(s1, s2, s3)

w1 = W // 3
w2 = W - w1
s1 = w1 * H
if H % 2 == 0:
    ans3_3 = abs(s1 - w2 * (H // 2))
else:
    s2 = w2 * (H // 2 + 1)
    s3 = w2 * (H // 2)
    ans3_4 = max(s1, s2, s3) - min(s1, s2, s3)

print(int(min(ans1, ans2_1, ans2_2, ans2_3, ans2_4, ans3_1, ans3_2, ans3_3, ans3_4, ans4)))

Solution (Python):
def ok_exit(res_):
    print(res_)
    exit(0)

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

res = float("inf")


if (-(-H//3) * 3) == H:   
    ok_exit(0)
res = min(res, W)         


if (-(-W//3) * 3) == W:
    ok_exit(0)
res = min(res, H)


for h1 in range(1, -(-H//2)+1):
    h2 = H - h1
    A3 = (W*h1, (W//2)*h2, (W-(W//2))*h2)
    res = min(res, max(A3)-min(A3))

if res == 0:
    ok_exit(0)
if H == W:
    ok_exit(res)


H, W = W, H
for h1 in range(1, -(-H//2)+1):
    h2 = H - h1
    A3 = (W*h1, (W//2)*h2, (W-(W//2))*h2)
    res = min(res, max(A3)-min(A3))

print(res)

Solution (Python):



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


p_list = []
hw_list = [(H, W)]

if H != W:
    
    hw_list.append((W, H))

for hw in hw_list:
    h, w = hw
    for h_i in range(h):
        a_area = h_i * w
        
        
        h2, amari = divmod((h-h_i), 2)
        area_list = [a_area, h2*w, (h2+amari)*w]
        p_1 = max(area_list) - min(area_list)

        
        w2, amari = divmod(w, 2)
        area_list = [a_area, (h-h_i)*w2, (h-h_i)*(w2+amari)]
        p_2 = max(area_list) - min(area_list)
        p_list.append(min(p_1, p_2))

ans = int(min(p_list))
print(ans)

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


minD = 10 ** 9
for w in range(1, W):
    s1 = w * H
    s2 = (W - w) * (H // 2)
    s3 = (W - w) * (H - H // 2)
    if s1 * s2 * s3 > 0:
        d = max(s1, s2, s3) - min(s1, s2, s3)
        if minD > d:
            minD = d
    s2 = ((W - w) // 2) * H
    s3 = (W - w - (W - w) // 2) * H
    if s1 * s2 * s3 > 0:
        d = max(s1, s2, s3) - min(s1, s2, s3)
        if minD > d:
            minD = d


for h in range(1, H):
    s1 = W * h
    s2 = (H - h) * (W // 2)
    s3 = (H - h) * (W - W // 2)
    if s1 * s2 * s3 > 0:
        d = max(s1, s2, s3) - min(s1, s2, s3)
        if minD > d:
            minD = d
    s2 = ((H - h) // 2) * W
    s3 = (H - h - (H - h) // 2) * W
    if s1 * s2 * s3 > 0:
        d = max(s1, s2, s3) - min(s1, s2, s3)
        if minD > d:
            minD = d

print(minD)

Solution (Python):
H, W = [int(i) for i in input().split()]

candidate = [
    H // 2 + W // 3 + 1,
    H // 3 + W // 2 + 1,
    H,
    W,
]
if H % 3 == 0 or W % 3 == 0:
    candidate.append(0)
if H % 2 == 0:
    candidate.append(H // 2)
if W % 2 == 0:
    candidate.append(W // 2)

print(min(candidate))

Solution (Python):
h,w=map(int,input().split())
ans=h*w
if h==w==2:
    print(1)
    exit()
if h>2:
    t=[h//3*w,(h-(h//3))//2*w,(h-(h//3)+1)//2*w]
    t.sort()
    ans=min(ans,t[2]-t[0])
if w>2:
    t=[w//3*h,(w-(w//3))//2*h,(w-(w//3)+1)//2*h]
    t.sort()
    ans=min(ans,t[2]-t[0])
a,b=h,w
for i in range(max(1,a//3-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
a,b=w,h
for i in range(max(1,a//3-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
print(ans)

Solution (Python):
import math

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

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

ans = float("inf")

if h%2==0:
    a = h//2
    b = h//2
else:
    a = h//2
    b = h//2+1

for i in range(1, w):
    l = [a*i, b*i, h*w-a*i-b*i]
    ans = min(ans, max(l)-min(l))


if w%2==0:
    a = w//2
    b = w//2
else:
    a = w//2
    b = w//2+1

for i in range(1, h):
    l = [a*i, b*i, h*w-a*i-b*i]
    ans = min(ans, max(l)-min(l))

ans = min(ans, h)
ans = min(ans, w)

print(ans)

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    temp1 = 100000000000000000
    temp2 = 100000000000000000
    for i in range(w):
        bs = [h*i,(w-i)*(h//2),(w-i)*(h//2+h%2)]
        d = max(bs)-min(bs)
        if temp1 > d :
            temp1 = d
    for i in range(h):
        bs = [w * i, (h - i) * (w // 2), (h - i) * (w // 2 + w % 2)]
        d = max(bs)-min(bs)
        if temp2 > d:
            temp2 = d
    
    print(min(temp1,temp2,min(w,h)))

Solution (Python):
import sys
h, w = [int(i) for i in sys.stdin.readline().split()]
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    _min = min(h,w)
    _max = max(h,w)
    s = h * w
    res = 1e9
    for x in [_max // 3, _max//3+1]:
        if (_max-x)%2==0:
            ls = [_min * x, _min*(_max - x)//2]
        elif _min % 2 == 0:
            ls = [_min * x, (s - _min * x)//2]
        else:
            __max = max(_max-x, _min)
            ls = [_min*x, (_max - x)*(__max//2), (_max-x)*(__max - __max//2)]
        res = min(res, max(ls)-min(ls))
    for y in [_min // 3, _min // 3 - 1]:
        if (_min-x)%2==0:
            ls = [_max * y, _max*(_min - y)//2]
        elif _max % 2 == 0:
            ls = [_max * y, (s - _max * y)//2]
        else:
            __max = max(_min-y, _max)
            ls = [_max*y, (_min - y)*(__max//2), (_min - y)*(__max - __max//2)]
        res = min(res, max(ls)-min(ls))
    print(res)

Solution (Python):
h, w = map(int, input().split(' '))
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    
    res = h * w
    for i in range(1, w):
        A = h * i
        B_a = h * ((w - i) // 2)
        C_a = h * w - A - B_a
        B_b = (h // 2) * (w - i)
        C_b = h * w - A - B_b
        res = min(res, max(A, B_a, C_a) - min(A, B_a, C_a), max(A, B_b, C_b) - min(A, B_b, C_b))
        
    
    for i in range(1, h):
        A = w * i
        B_a = w * ((h - i) // 2)
        C_a = h * w - A - B_a
        B_b = (w // 2) * (h - i)
        C_b = h * w - A - B_b
        res = min(res, max(A, B_a, C_a) - min(A, B_a, C_a), max(A, B_b, C_b) - min(A, B_b, C_b))
        
    print(res)

Solution (Python):



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

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


ah = h//3
aw = h//3
ans = h*w
if ah:
    ans = min(ans,w)
if aw:
    ans = min(ans,h)

for i in range(1,w):
    x = h*i
    if w-i>1:
        y = ((w-i)//2)*h
        z = h*(w-i)-y
        ans =min(ans,max(x,y,z)-min(x,y,z))
    if h>1:
        y = (h//2)*(w-i)
        z = h*(w-i)-y
        ans =min(ans,max(x,y,z)-min(x,y,z))
for i in range(1,h):
    x = w*i
    if h-i>1:
        y = ((h-i)//2)*w
        z = w*(h-i)-y
        ans =min(ans,max(x,y,z)-min(x,y,z))
    if w>1:
        y = (w//2)*(h-i)
        z = w*(h-i)-y
        ans =min(ans,max(x,y,z)-min(x,y,z))

print(ans)

Solution (Python):
h,w=map(int,input().split())
ans=10**5
for _ in range(2):
    for i in range(1,h):
        th=(h-i)//2
        m1=max(i*w,th*w,(h-i-th)*w)-min(i*w,th*w,(h-i-th)*w)
        tw=w//2
        m2=max(i*w,(h-i)*tw,(h-i)*(w-tw))-min(i*w,(h-i)*tw,(h-i)*(w-tw))
        ans=min(ans,m1,m2)
    h,w=w,h
print(ans)

Solution (Python):
import sys

h, w = (int(i) for i in input().split())  

ans = min(h,w)

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

for i in range(1,h+1):
	ans = min(ans, max(w*i, (h-i)*(w//2), (h-i)*(w-w//2)) - min(w*i, (h-i)*(w//2), (h-i)*(w-w//2)))
for i in range(1, w+1):
	ans = min(ans, max(h*i, (w-i)*(h//2), (w-i)*(h-h//2)) - min(h*i, (w-i)*(h//2), (w-i)*(h-h//2)))

print(ans)

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

if H % 3 == 0:
    ans = 0
else:
    ans = W
    h_small = max(1, H // 3)
    a = W * h_small
    b = (H - h_small) * (W // 2)
    c = (H - h_small) * ((W + 1) // 2)
    ans_new = max(a, b, c) - min(a, b, c)
    ans = min(ans_new, ans)
    h_big = H // 3 + 1
    a = W * h_big
    b = (H - h_big) * (W // 2)
    c = (H - h_big) * ((W + 1) // 2)
    ans_new = max(a, b, c) - min(a, b, c)
    ans = min(ans_new, ans)
if W % 3 == 0:
    ans = 0
else:
    ans = min(H, ans)
    w_small = max(1, W // 3)
    a = H * w_small
    b = (W - w_small) * (H // 2)
    c = (W - w_small) * ((H + 1) // 2)
    ans_new = max(a, b, c) - min(a, b, c)
    ans = min(ans_new, ans)
    w_big = W // 3 + 1
    a = H * w_big
    b = (W - w_big) * (H // 2)
    c = (W - w_big) * ((H + 1) // 2)
    ans_new = max(a, b, c) - min(a, b, c)
    ans = min(ans_new, ans)

print(ans)

Solution (Python):
def threeArea(w, h, ans):
  for i in range(1, h):
    recA = w * i
    width = w//2
    recB = (h-i) * width
    recC = w * h - recA - recB
    diff = max(recA, recB, recC) - min(recA, recB, recC)
    
    height = (h - i) //2
    recB = w * height
    recC = w * h - recA - recB
    diff = min(diff, max(recA, recB, recC) - min(recA, recB, recC))
    if ans > diff:
      ans = diff
  return ans

H, W = (int(i) for i in input().split())
 
ans = float('inf')
ans = threeArea(W, H, ans)
ans = threeArea(H, W, ans)
 
print(ans)

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


def main():
    high, width = map(int, input().split())
    sum_s = high * width
    ans = sum_s
    for i in range(1, high):
        a = i * width
        b1 = (high - i) * (width // 2)
        b2 = ((high - i) // 2) * width
        c1 = sum_s - (a + b1)
        c2 = sum_s - (a + b2)

        ans = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2), ans)

    for i in range(1, width):
        a = i * high
        b1 = (width - i) * (high // 2)
        b2 = ((width - i) // 2) * high
        c1 = sum_s - (a + b1)
        c2 = sum_s - (a + b2)

        ans = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2), ans)

    print(ans)




if __name__ == '__main__':
    main()

Solution (Python):
from math import ceil


def check(hs, ws):
    rtn = []
    for i in range(3):
        for j in range(i + 1, 3):
            rtn.append(abs(hs[i] * ws[i] - hs[j] * ws[j]))
    return max(rtn)


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

ans = []


hh = [ceil(h / 3), h // 3, h - ceil(h / 3) - h // 3]
ww = [w, w, w]
if hh.count(0) == 0 and ww.count(0) == 0:
    ans.append(check(hh, ww))

h, w = w, h
hh = [ceil(h / 3), h // 3, h - ceil(h / 3) - h // 3]
ww = [w, w, w]
if hh.count(0) == 0 and ww.count(0) == 0:
    ans.append(check(hh, ww))

hh[0] = h // 3
ww[0] = w
hh[1:3] = [h - hh[0], h - hh[0]]
ww[1:3] = [w // 2, ceil(w / 2)]
if hh.count(0) == 0 and ww.count(0) == 0:
    ans.append(check(hh, ww))

hh[0] = ceil(h / 3)
ww[0] = w
hh[1:3] = [h - hh[0], h - hh[0]]
ww[1:3] = [w // 2, ceil(w / 2)]
if hh.count(0) == 0 and ww.count(0) == 0:
    ans.append(check(hh, ww))

h, w = w, h
hh[0] = h // 3
ww[0] = w
hh[1:3] = [h - hh[0], h - hh[0]]
ww[1:3] = [w // 2, ceil(w / 2)]
if hh.count(0) == 0 and ww.count(0) == 0:
    ans.append(check(hh, ww))

hh[0] = ceil(h / 3)
ww[0] = w
hh[1:3] = [h - hh[0], h - hh[0]]
ww[1:3] = [w // 2, ceil(w / 2)]
if hh.count(0) == 0 and ww.count(0) == 0:
    ans.append(check(hh, ww))

print(min(ans))

Solution (Python):
def threeArea(w, h, ans):
    for i in range(1, h):
        recA = w * i
        width = w//2
        recB = (h-i) * width
        recC = w * h - recA - recB
        diff = max(recA, recB, recC) - min(recA, recB, recC)

        height = (h - i) //2
        recB = w * height
        recC = w * h - recA - recB
        diff = min(diff, max(recA, recB, recC) - min(recA, recB, recC))
        if ans > diff:
            ans = diff
    return ans

H, W = (int(i) for i in input().split())

ans = float('inf')
ans = threeArea(W, H, ans)
ans = threeArea(H, W, ans)

print(ans)

Solution (Python):
import bisect
from collections import deque
import sys
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

H,W = IL()

if (H%3 == 0) or (W%3 == 0):
  print(0)
else:
  tate_m = H//2
  yoko_m = W//2

  ans = MAX_INT
  
  for w in range(1,yoko_m+1):
    Area1 = H*w
    Area2 = tate_m*(W-w)
    Area3 = (H-tate_m)*(W-w)
    ans = min(ans, max(Area1,Area2,Area3)-min(Area1,Area2,Area3))

  
  for h in range(1,tate_m+1):
    Area1 = h*W
    Area2 = (H-h)*yoko_m
    Area3 = (H-h)*(W-yoko_m)
    ans = min(ans, max(Area1,Area2,Area3)-min(Area1,Area2,Area3))
  
  ans = min(ans, ((H//3)+1)*W - (H//3)*W)
  ans = min(ans, ((W//3)+1)*H - (W//3)*H)

  print(ans)

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

def split2(h,w):
    if h%2==0 or w%2==0:
        return h*w//2,h*w//2
    else:
        h,w = max(h,w),min(h,w)
        return h//2*w,(h+1)//2*w

ans = H*W
for i in range(1,H):
    h = H-i
    w = W
    s1 = W*i
    s2,s3 = split2(h,w)
    ans = min(max(s1,s2,s3)-min(s1,s2,s3),ans)

for i in range(1,W):
    h = H
    w = W-i
    s1 = H*i
    s2,s3 = split2(h,w)
    ans = min(max(s1,s2,s3)-min(s1,s2,s3),ans)

print(ans)

Solution (Python):


def main():
    H, W = map( int, input().split())
    ans = min(H,W)
    if H%3 == 0 or W%3 == 0:
        ans = 0
    for i in range(1,H):
        L = [W*i]
        L.append(W//2*(H-i))
        if W%2 == 1:
            L.append((W//2+1)*(H-i))
        if max(L) - min(L) < ans:
            ans = max(L) - min(L)
    for i in range(1,W):
        L = [H*i]
        L.append(H//2*(W-i))
        if H%2 == 1:
            L.append((H//2+1)*(W-i))
        if max(L) - min(L) < ans:
            ans = max(L) - min(L)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):


def main():
    H, W = map( int, input().split())
    ans = min(H,W)
    if H%3 == 0 or W%3 == 0:
        ans = 0
    for i in range(1,H):
        L = [W*i]
        L.append(W//2*(H-i))
        if W%2 == 1:
            L.append((W//2+1)*(H-i))
        if max(L) - min(L) < ans:
            ans = max(L) - min(L)
    for i in range(1,W):
        L = [H*i]
        L.append(H//2*(W-i))
        if H%2 == 1:
            L.append((H//2+1)*(W-i))
        if max(L) - min(L) < ans:
            ans = max(L) - min(L)
    print(ans)
if __name__ == '__main__':
    main()

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

ans=float('inf')
for i in range(h):
    ma=max(i*w,(h-i)*(w//2),(h-i)*(-(-w//2)))
    mi=min(i*w,(h-i)*(w//2),(h-i)*(-(-w//2)))
    ma2=max(i*w,((h-i)//2)*w,(-(-(h-i)//2))*w)
    mi2=min(i*w,((h-i)//2)*w,(-(-(h-i)//2))*w)
    
    ans=min(ans, ma-mi, ma2-mi2)
    
for i in range(w):
    ma=max(i*h,(w-i)*(h//2),(w-i)*(-(-h//2)))
    mi=min(i*h,(w-i)*(h//2),(w-i)*(-(-h//2)))
    ma2=max(i*h,((w-i)//2)*h,(-(-(w-i)//2))*h)
    mi2=min(i*h,((w-i)//2)*h,(-(-(w-i)//2))*h)
    
    ans=min(ans, ma-mi, ma2-mi2)
                    
print(ans)

Solution (Python):
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)
    exit()
def Tcheck(X,Y):
    res=X*Y
    for i in range(Y+1):
        
        Z=[X*i,(Y-i)*(X//2),(Y-i)*(X-(X//2))]
        res=min(res,max(Z)-min(Z))
    return res
print(min(H,W,Tcheck(H,W),Tcheck(W,H)))

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

if H % 3 == 0 or W % 3 == 0:
    ans = 0
else:
    hp = [H//3, H//3+1]
    ans = H*W
    for h in hp:
        S1 = h*W
        if ((H-h)*W) % 2 == 0:
            ans = min(abs((H-h)*W//2 - S1), ans)
        else:
            S2 = (H-h-1)*W//2 
            S3 = (H-h+1)*W//2
            a = max([abs(S2-S1), abs(S1-S3), abs(S3-S2)])
            S4 = (H-h)*(W+1)//2
            S5 = (H-h)*(W-1)//2
            b = max([abs(S1-S4), abs(S4-S5), abs(S5-S1)])
            ans = min([ans, a, b])
    wp = [W//3, W//3+1]
    for w in wp:
        S1 = H*w
        if ((W-w)*H) % 2 == 0:
            ans = min(abs((W-w)*H//2 - S1), ans)
        else:
            S2 = (W-w-1)*H//2 
            S3 = (W-w+1)*H//2
            a = max([abs(S2-S1), abs(S1-S3), abs(S3-S2)])
            S4 = (W-w)*(H+1)//2
            S5 = (W-w)*(H-1)//2
            b = max([abs(S1-S4), abs(S4-S5), abs(S5-S1)])
            ans = min([ans, a, b])
print(ans)

Solution (Python):
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7

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

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

    ans = min(H, W)

    for w in [W // 3, W // 3 + 1]:
        A = w * H
        for h in [H // 2, H // 2 + 1]:
            B = (W - w) * h
            C = H * W - B - A

            Smax = max(A, B, C)
            Smin = min(A, B, C)
            ans = min(ans, abs(Smax - Smin))

    for h in [H // 3, H // 3 + 1]:
        A = h * W
        for w in [W // 2, W // 2 + 1]:
            B = (H - h) * w
            C = H * W - B - A

            Smax = max(A, B, C)
            Smin = min(A, B, C)
            ans = min(ans, abs(Smax - Smin))

    print(ans)

sol()

Solution (Python):





def calc(h, w):
    ans = float('inf')

    
    
    

    
    for hi in range(1, h):
        sa = hi * w

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

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

    return ans


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

    print(min(calc(h, w), calc(w, h)))


if __name__ == '__main__':
    main()

Solution (Python):
import math
H,W=map(int,input().split())
A=max(H,W)
B=min(H,W)
mini=10000000
if A%3==0 or B%3==0:
    print(0)
    exit(0)

for i in range(1,A):
    X=B*i
    Y=math.floor((A-i)/2)*B
    Z = math.ceil((A - i) /2) * B
    if mini>max(X,Y,Z)-min(X,Y,Z):
        mini=max(X,Y,Z)-min(X,Y,Z)

for i in range(1,A):
    X=B*i
    Y=math.floor(B/2)*(A-i)
    Z = math.ceil(B/ 2) * (A-i)
    if mini>max(X,Y,Z)-min(X,Y,Z):
        mini=max(X,Y,Z)-min(X,Y,Z)

for i in range(1,B):
    X=A*i
    Y=math.floor((B-i)/2)*A
    Z = math.ceil((B - i) / 2) * A
    if mini>max(X,Y,Z)-min(X,Y,Z):
        mini=max(X,Y,Z)-min(X,Y,Z)

for i in range(1,B):
    X=A*i
    Y=math.floor(A/2)*(A-i)
    Z = math.ceil(A/ 2) * (A-i)
    if mini>max(X,Y,Z)-min(X,Y,Z):
        mini=max(X,Y,Z)-min(X,Y,Z)

print(mini)

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

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


ans = H*W

for h in range(1, H+1):
    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+1):
    a = H*w
    
    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):
def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0: return 0
    min_d = min(H, W)
    if H % 3 != 0:
        for h in [H//3, H//3+1]:
            v1 = W*h
            v2 = (H-h)*(W//2)
            v3 = (H-h)*(W-W//2)
            min_d = min(min_d, max(v1,v2,v3)-min(v1,v2,v3))
    if W % 3 != 0:
        for w in [W//3, W//3+1]:
            v1 = H*w
            v2 = (W-w)*(H//2)
            v3 = (W-w)*(H-H//2)
            min_d = min(min_d, max(v1,v2,v3)-min(v1,v2,v3))

    return min_d

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

Solution (Python):
from math import ceil, floor
def chocolateBar():
    h,w=map(int,input().split())
    ans=10**18

    
    ans=min(ans, (ceil(w/3)-floor(w/3))*h )
    ans=min(ans, (ceil(h/3)-floor(h/3))*w )

    
    for i in range(1, h):
        s1=i*w
        s2=(h-i)*floor(w*0.5)
        s3=(h-i)*ceil(w*0.5)
        ans=min(ans, max(s1,s2,s3)-min(s1,s2,s3))
    for i in range(1, w):
        s1=i*h
        s2=(w-i)*floor(h*0.5)
        s3=(w-i)*ceil(h*0.5)
        ans=min(ans, max(s1,s2,s3)-min(s1,s2,s3))
    
    return ans

print(chocolateBar())

Solution (Python):
H, W = map(int, input().split())
if H%3 == 0 or W%3 == 0:
  print(0)
  exit()
r = min(H, W)
def calc(a, b, c):
  a, b, c = sorted([a, b, c])
  return c-a
h = H//3
w = W//2
r = min(r, calc(h*W, (H-h)*w, (H-h)*(W-w)))
h = H//3+1
w = W//2
r = min(r, calc(h*W, (H-h)*w, (H-h)*(W-w)))
w = W//3
h = H//2
r = min(r, calc(H*w, h*(W-w), (H-h)*(W-w)))
w = W//3+1
h = H//2
r = min(r, calc(H*w, h*(W-w), (H-h)*(W-w)))
print(r)

Solution (Python):
import sys
from math import factorial, ceil, floor
from bisect import bisect_right as bsr
from operator import itemgetter as ig
from collections import defaultdict as dd
from collections import deque


args = None
INF = float("inf")
MOD = int(1e9 + 7)
def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][0]) for _ in range(ps[0][1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def main():
    H, W = input([[int], 2])

    def solve(a_max, b_max):
        min_diff = INF
        for a in range(a_max):
            
            area1 = (a * b_max
                    , b_max // 2 * (a_max - a)
                    , (b_max // 2 + b_max % 2) * (a_max - a))
            
            area2 = (a * b_max
                    , (a_max - a) // 2 * b_max
                    , ((a_max - a) // 2 + (a_max - a) % 2) * b_max)
            min_diff = min(min_diff, max(area1) - min(area1), max(area2) - min(area2))
        return min_diff
    print(min(solve(H, W), solve(W, H)))

if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Solution (Python):

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

h,w=getints()

def solve2(h,w):
    if h % 2 == 0:
        e = h // 2 * w
        return (e,e)
    if w % 2 == 0:
        e = h * w // 2
        return (e,e)
    h1 = h // 2
    h2 = h - h1
    e1 = (h1 * w, h2 * w)
    w1 = w // 2
    w2 = w - w1
    e2 = (h * w1, h * w2)
    return e2 if abs(e2[0] - e2[1]) < abs(e1[0] - e1[1]) else e1

def solve3():
    res = h * w
    for i in range(1, h):
        m = i * w
        r = solve2(h-i,w)
        values = list(sorted([m, r[0], r[1]]))
        res = min(res, values[2] - values[0])
    for j in range(1, w):
        m = h * j
        r = solve2(h, w - j)
        values = list(sorted([m, r[0], r[1]]))
        res = min(res, values[2] - values[0])
    return res

print(solve3())