Problem:
 Score : 400 points    Problem Statement There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.  Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min} , where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min} .      Constraints  2 ≤ H, W ≤ 10^5         Input Input is given from Standard Input in the following format:  H  W       Output Print the minimum possible value of S_{max} - S_{min} .        Sample Input 1 3 5      Sample Output 1 0  In the division below, S_{max} - S_{min} = 5 - 5 = 0 .          Sample Input 2 4 5      Sample Output 2 2  In the division below, S_{max} - S_{min} = 8 - 6 = 2 .          Sample Input 3 5 5      Sample Output 3 4  In the division below, S_{max} - S_{min} = 10 - 6 = 4 .          Sample Input 4 100000 2      Sample Output 4 1       Sample Input 5 100000 100000      Sample Output 5 50000

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

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

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

Solution (Python):
H,W = map(int,input().split())
s = H*W
def func(h,w):
    tmp = float("inf")
    for x in range(1,w):
        s1 = h*x
        s2 = (w-x)*(h//2)
        s3 = s - s1 - s2
        d = max(s1,s2,s3) - min(s1,s2,s3)
        if d < tmp:
            tmp = d
    return tmp
ans = min(func(H,W),func(W,H),min(W%3,1)*H,(min(H%3,1)*W))
print(ans)

Solution (Python):
h,w = (int(i) for i in input().split())
ans = 99999999999999999
for i in range(0,h):
  sa = i*w
  
  h2 = (h-i)//2
  sb = h2*w
  sc = (h-i-h2)*w
  judgema = max([sa,sb,sc])
  judgemi = min([sa,sb,sc])
  ans = min(ans,(judgema-judgemi))
  
  w2 = (w//2)
  sb = (h-i)*w2
  sc = (h-i)*(w-w2)
  judgema = max([sa,sb,sc])
  judgemi = min([sa,sb,sc])
  ans = min(ans,(judgema-judgemi))

tmp1 = h
tmp2 = w
w =tmp1
h =tmp2
for i in range(0,h):
  sa = i*w
  
  h2 = (h-i)//2
  sb = h2*w
  sc = (h-i-h2)*w
  judgema = max([sa,sb,sc])
  judgemi = min([sa,sb,sc])
  ans = min(ans,(judgema-judgemi))
  
  w2 = (w//2)
  sb = (h-i)*w2
  sc = (h-i)*(w-w2)
  judgema = max([sa,sb,sc])
  judgemi = min([sa,sb,sc])
  ans = min(ans,(judgema-judgemi))
  
print(ans)

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

ans = 9999999999

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



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

print(ans)

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

if 0 in [H % 3, W % 3]:
    print(0)
    exit()


def calc(x, y):
    res = float('inf')
    for p in [y // 3, 1 + y // 3]:
        q = x // 2
        a = sorted([x * p, q * (y - p), (x - q) * (y - p)])
        res = min(res, abs(a[2] - a[0]))
    return res


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

Solution (Python):
import math

def tcut(H,W):
    arr1 = [math.floor(2*H/3),math.ceil(2*H/3)]
    arr2 = [math.floor(W/2),math.ceil(W/2)]
    minS = H*W
    for h in arr1:
        for w in arr2:
            smax = max(h*w,(H-h)*W,h*(W-w))
            smin = min(h*w,(H-h)*W,h*(W-w))
            if minS > smax - smin:
                minS = smax - smin
    return minS

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

if H%3 == 0 or W%3 == 0:
    print(0)
else:
    Tcut = min(tcut(H,W),tcut(W,H))
    stcut = min(H,W)
    print(min(Tcut,stcut))

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()
else:
    cand = []
    for i in range(1, H):
        
        S1 = i * W
        S2 = (H - i) * (W // 2)
        S3 = (H - i) * (W - W // 2)
        cand.append(max(S1, S2, S3) - min(S1, S2, S3))
        
        S2 = (H - i) // 2 * W
        S3 = ((H - i) - (H - i) // 2) * W
        cand.append(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)
        cand.append(max(S1, S2, S3) - min(S1, S2, S3))
        
        S2 = (W - i) // 2 * H
        S3 = ((W - i) - (W - i) // 2) * H
        cand.append(max(S1, S2, S3) - min(S1, S2, S3))
    print(min(cand))

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

result = []
for h in range(1, H//2+1):
    Sa = W * h

    remain = H - h
    h2 = (remain + 1) // 2
    Sb1 = W * h2
    Sc1 = W * (remain - h2)
    result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1))

    w = (W + 1) // 2
    Sb2 = w * remain
    Sc2 = (W - w) * remain
    result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2))

for w in range(1, W//2+1):
    Sa = H * w

    remain = W - w
    w3 = (remain + 1) // 2
    Sb1 = H * w3
    Sc1 = H * (remain - w3)
    result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1))

    h = (H + 1) // 2
    Sb2 = h * remain
    Sc2 = (H - h) * remain
    result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2))

print(min(result))

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

result = []
for h in range(1, H):
    Sa = W * h

    remain = H - h
    h2 = (remain + 1) // 2
    Sb1 = W * h2
    Sc1 = W * (remain - h2)
    result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1))

    w = (W + 1) // 2
    Sb2 = w * remain
    Sc2 = (W - w) * remain
    result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2))

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

    remain = W - w
    w3 = (remain + 1) // 2
    Sb1 = H * w3
    Sc1 = H * (remain - w3)
    result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1))

    h = (H + 1) // 2
    Sb2 = h * remain
    Sc2 = (H - h) * remain
    result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2))

print(min(result))

Solution (Python):
h, w = map(int, input().split())
div1 = 0
div2 = 0
if h%3==0 or w%3==0:
	print("0")
else:
	div1 = min(h, w)
	def div2(h, w):
		m = h*w
		for l in range(1, w):
			s = [l*h, (w-l)*(h-h//2), (w-l)*(h//2)]
			m = min(m, max(s)-min(s))
		return m
	div2 = min(div2(h, w), div2(w, h))
	print(min(div1, div2))

Solution (Python):
h, w = map(int, input().split())
div1 = 0
div2 = 0
if h%3==0 or w%3==0:
	print("0")
else:
	div1 = min(h, w)

	def div2(h, w):
		m = h*w
		for l in range(1, w):
			s = [l*h, (w-l)*(h-h//2), (w-l)*(h//2)]
			m = min(m, max(s)-min(s))
		return m

	div2 = min(div2(h, w), div2(w, h))
	print(min(div1, div2))

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

h, w = min(x, y), max(x, y)

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

ans1 = h

k = h//2
if h % 2:
    for i in range(1, w):
        l = w - i
        s1 = h * i
        s2 = k * l
        s3 = (k+1)*l
        mn = min(s1, s2)
        mx = max(s1, s3)
        ans1 = min(ans1, mx-mn)

else:
    for i in range(1, w):
        l = w - i
        s1 = h*i
        s2 = k*l
        mn = min(s1, s2)
        mx = max(s1, s2)
        ans1 = min(ans1, mx-mn)

w, h = min(x, y), max(x, y)

ans2 = h

k = h//2
if h % 2:
    for i in range(1, w):
        l = w - i
        s1 = h * i
        s2 = k * l
        s3 = (k+1)*l
        mn = min(s1, s2)
        mx = max(s1, s3)
        ans2 = min(ans2, mx-mn)

else:
    for i in range(1, w):
        l = w - i
        s1 = h*i
        s2 = k*l
        mn = min(s1, s2)
        mx = max(s1, s2)
        ans2 = min(ans2, mx-mn)

print(min(ans1, ans2))

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


def get_min(H, W):
    min_whole = 100000000000
    for h in range(1, H):
        S_a = h * W
        w = W // 2
        S_b = (H - h) * w
        S_c = (H - h) * (W - w)
        pat1 = max(S_a, S_b, S_c) - min(S_a, S_b, S_c)
        pat2 = 1000000000

        h_b = (H - h) // 2
        S_b = h_b * W
        S_c = (H - h - h_b) * W
        tmp = max(S_a, S_b, S_c) - min(S_a, S_b, S_c)
        if pat2 > tmp:
            pat2 = tmp
        min_loop = min(pat1, pat2)
        if min_whole > min_loop:
            min_whole = min_loop
    return min_whole


port = get_min(height, width)
land = get_min(width, height)
print(min(port, land))

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

S = H * W
ans = 0
if H * W % 3 != 0:
    ans = 10 ** 11
    for a, b in zip((H, W), (W, H)):
        for i in range(1, a):
            S1 = i * b
            S2 = (a - i) * (b // 2)
            S3 = S - S1 - S2
            S4 = ((a - i) // 2) * b
            S5 = S - S1 - S4

            ans = min(ans,
                      max(S1, S2, S3) - min(S1, S2, S3),
                      max(S1, S4, S5) - min(S1, S4, S5))

print(ans)

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

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

    h_res = H - h

    B1 = W*(h_res//2)
    C1 = W*(h_res - h_res//2)

    diff1 = max(abs(A - B1), abs(A - C1), abs(B1 - C1))

    B2 = h_res * (W//2)
    C2 = h_res * (W - W//2)
    diff2 = max(abs(A - B2), abs(A - C2), abs(B2 - C2))

    ans = min(ans, diff1, diff2)

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

    w_res = W - w

    B1 = w_res*(H//2)
    C1 = w_res*(H - H//2)

    diff1 = max(abs(A - B1), abs(A - C1), abs(B1 - C1))

    B2 = H* (w_res//2)
    C2 = H * (w_res - w_res//2)
    diff2 = max(abs(A - B2), abs(A - C2), abs(B2 - C2))

    ans = min(ans, diff1, diff2)


print(ans)

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

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

else:
    result = []
    for i in range(1, H):
        s1 = i * W
        if (H - i) % 2 == 0 or W % 2 == 0:
            s2 = (H - i) * W // 2
            result.append(max(s1, s2) - min(s1, s2))
        else:
            s2 = (H - i) // 2 * W
            s3 = (H - i) * W - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))
            s2 = (H - i) * (W // 2)
            s3 = (H - i) * W - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))

    for i in range(1, W):
        s1 = i * H
        if (W - i) % 2 == 0 or H % 2 == 0:
            s2 = (W - i) * H // 2
            result.append(max(s1, s2) - min(s1, s2))
        else:
            s2 = (W - i) // 2 * H
            s3 = (W - i) * H - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))
            s2 = (W - i) * (H // 2)
            s3 = (W - i) * H - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))

    print(min(result))

Solution (Python):
a, b = map(int, input().split())
if (a % 3 ==0) | (b % 3 ==0):
    ans = 0
else:
    anss = []
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 + 1)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 - 1) * a
    s2 = (b - (b // 3) + 1) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b // 3 + 1) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    
    t = a
    a = b
    b = t
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 + 1)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 - 1) * a
    s2 = (b - (b // 3) + 1) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b // 3 + 1) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    ans = min(anss)

print(ans)

Solution (Python):
def main():
    a, b = map(int, input().split())
    if (a % 3 ==0) | (b % 3 ==0):
        ans = 0
    else:
        anss = []
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 - 1) * a
        s2 = (b - (b // 3) + 1) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3 + 1)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        
        t = a
        a = b
        b = t
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 - 1) * a
        s2 = (b - (b // 3) + 1) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3 + 1)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        ans = min(anss)

    print(ans)
    
main()

Solution (Python):
def main():
    a, b = map(int, input().split())
    if (a % 3 ==0) | (b % 3 ==0):
        ans = 0
    else:
        anss = []
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 2) * a
        s2 = (b - (b // 3 + 2)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 2) * a
        s2 = (b - (b // 3 + 2)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))
        
        
        t = a
        a = b
        b = t
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 2) * a
        s2 = (b - (b // 3 + 2)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 2) * a
        s2 = (b - (b // 3 + 2)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        ans = min(anss)

    print(ans)
main()

Solution (Python):
import math
h, w = map(int, input().split())
if (h * w) % 3 == 0:
    print(0)
    exit()
if h % 3 == 1:
    k = h + 2
if h % 3 == 2:
    k = h + 1
if w % 3 == 1:
    v = w + 2
if w % 3 == 2:
    v = w + 1

if h % 2 == 0:
    m = h
if h % 2 == 1:
    m = h + 1
if w % 2 == 0:
    u = w
if w % 2 == 1:
    u = w + 1

s11 = k // 3 * w
s12 = (h - k // 3) * (math.floor(w / 2))
s13 = (h - k // 3) * (math.ceil(w/ 2))
s1 = max(s11, s12, s13) - min(s11, s12, s13)

t11 = (k // 3 - 1) * w
t12 = (h - k // 3 + 1) * (math.floor(w / 2))
t13 = (h - k // 3 + 1) * (math.ceil(w/ 2))
t1 = max(t11, t12, t13) - min(t11, t12, t13)

s21 = (k // 3) * w
s22 = (k // 3 - 1) * w
if h % 3 == 1:
    s23 = (k // 3 - 1) * w
else:
    s23 = (k // 3) * w
s2 = max(s21, s22, s23) - min(s21, s22, s23)

s31 = v // 3 * h
s32 = (w - v // 3) * (math.floor(h / 2))
s33 = (w - v // 3) * (math.ceil(h / 2))
s3 = max(s31, s32, s33) - min(s31, s32, s33)

t31 = (v // 3 - 1) * h
t32 = (w - v // 3 + 1) * (math.floor(h / 2))
t33 = (w - v // 3 + 1) * (math.ceil(h / 2))
t3 = max(t31, t32, t33) - min(t31, t32, t33)

s41 = (v // 3) * h
s42 = (v // 3 - 1) * h
if h % 3 == 1:
    s43 = (v // 3 - 1) * h
else:
    s43 = (v // 3) * h
s4 = max(s41, s42, s43) - min(s41, s42, s43)

print(min(s1, s2, s3, s4, t1, t3))

Solution (Python):





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


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


def try_divi(W, H):
    
    err = W
    
    w2, w3 = W // 2, W // 2
    if W % 2:
        w2 += 1
    for h1 in range(1, H // 2 + 1):
        h2 = H-h1
        S1, S2, S3 = h1*W, w2*h2, w3*h2
        new_err = max(S1, S2, S3) - min(S1, S2, S3)
        if new_err < err:
            err = new_err
    return err


print(min(try_divi(W, H), try_divi(H, W)))

Solution (Python):





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


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


def try_divi(W, H):
    
    err = W
    
    w2, w3 = W // 2, W // 2
    if W % 2:
        w2 += 1
    for h1 in range(1, H // 2 + 1):
        h2 = H-h1
        S1, S2, S3 = h1*W, w2*h2, w3*h2
        new_err = max(S1, S2, S3) - min(S1, S2, S3)
        if new_err < err:
            err = new_err
    return err


print(min(try_divi(W, H), try_divi(H, W)))

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

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

        
        wm = w // 2
        sb = (h - hi) * wm
        sc = (h - hi) * (w - wm)
        ans1 = max(sa, sb, sc) - min(sa, sb, sc)
        
        hm = (h - hi) // 2
        sb = hm * w
        sc = (h - hi - hm) * w
        ans2 = max(sa, sb, sc) - min(sa, sb, sc)

        ans = min(ans, ans1, ans2)
    return ans

print(min(divide(h, w), divide(w, h)))

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

def divide(h, w):
  ans = h*w
  for w1 in range(1, w):
    s1 = h*w1

    w2 = (w-w1)//2
    s21 = h*w2
    s31 = h*(w-w1-w2)
    ans1 = max(s1, s21, s31) - min(s1, s21, s31)

    h2 = h//2
    s22 = (w-w1)*h2
    s32 = (w-w1)*(h-h2)
    ans2 = max(s1, s22, s32) - min(s1, s22, s32)
    ans = min(ans, ans1, ans2)
  return ans

print(min(divide(h, w), divide(w, h)))

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

def divide(h, w):
  ans = h*w
  for w1 in range(1, w):
    s1 = h*w1
    
    w2 = (w-w1)//2
    s21 = h*w2
    s31 = h*(w-w1-w2)
    ans1 = max(s1, s21, s31) - min(s1, s21, s31)
    
    h2 = h//2
    s22 = (w-w1)*h2
    s32 = (w-w1)*(h-h2)
    ans2 = max(s1, s22, s32) - min(s1, s22, s32)
    ans = min(ans, ans1, ans2)
  return ans
    
print(min(divide(h, w), divide(w, h)))

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


if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
minDiff = h * w
for i in range(1, h):
    s1 = w * i
    s2 = int(w / 2) * (h - i)
    s3 = (w - int(w / 2)) * (h - i)
    if s1 > 0 and s2 > 0 and s3 > 0:
        minS = min(s1, min(s2, s3))
        maxS = max(s1, max(s2, s3))
        minDiff = min(minDiff, abs(maxS - minS))
for i in range(1, w):
    s1 = h * i
    s2 = int(h / 2) * (w - i)
    s3 = (h - int(h / 2)) * (w - i)
    if s1 > 0 and s2 > 0 and s3 > 0:
        minS = min(s1, min(s2, s3))
        maxS = max(s1, max(s2, s3))
        minDiff = min(minDiff, abs(maxS - minS))
for i in range(1, h):  
    s1 = w * i
    s2 = w * (int((h - i) / 2))
    s3 = w * (h - i - int((h - i) / 2))
    if s1 > 0 and s2 > 0 and s3 > 0:
        minS = min(s1, min(s2, s3))
        maxS = max(s1, max(s2, s3))
        minDiff = min(minDiff, abs(maxS - minS))
for i in range(1, w):  
    s1 = h * i
    s2 = h * (int((w - i) / 2))
    s3 = h * (w - i - int((w - i) / 2))
    if s1 > 0 and s2 > 0 and s3 > 0:
        minS = min(s1, min(s2, s3))
        maxS = max(s1, max(s2, s3))
        minDiff = min(minDiff, abs(maxS - minS))
print(minDiff)

Solution (Python):
h,w=map(int,input().split())
s=float('inf')
for i in range(1,h):
    a1=min(i*w,w*((h-i)//2))
    b1=max(i*w,w*(h-i-(h-i)//2))
    d1=b1-a1
    a2=min(i*w,(h-i)*(w//2))
    b2=max(i*w,(h-i)*(w-w//2))
    d2=b2-a1
    s=min(s,d1,d2)
for j in range(1,w):
    a1=min(j*h,h*((w-j)//2))
    b1=max(j*h,h*(w-j-(w-j)//2))
    d1=b1-a1
    a2=min(j*h,(w-j)*(h//2))
    b2=max(j*h,(w-j)*(h-h//2))
    d2=b2-a2
    s=min(s,d1,d2)
print(s)

Solution (Python):
def separate(a,b):
    r=[]
    tmp=[a//3*b,(a-a//3)*(b//2),(a-a//3)*(b//2+b%2)]
    r.append(max(tmp)-min(tmp))
    tmp=[(a//3+1)*b,(a-a//3-1)*(b//2),(a-a//3-1)*(b//2+b%2)]
    r.append(max(tmp)-min(tmp))
    return min(r)

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

if H%3==0 or W%3==0:
    ans=0
else:
    S=[H,W]
    S.append(separate(H,W))
    S.append(separate(W,H))
    ans=min(S)
print(ans)

Solution (Python):
import math

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

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

min_num = 100000000000
for i in range(1, H):
    a = i*W
    b = W//2 * (H-i)
    c = math.ceil(W/2) * (H-i)
    _max_num = max(a, max(b, c))
    _min_num = min(a, min(b, c))
    n = _max_num - _min_num
    if n < min_num:
        min_num = n

for i in range(1, H):
    a = i*W
    b = (H-i)//2 * W
    c = math.ceil((H-i)/2) * W
    _max_num = max(a, max(b, c))
    _min_num = min(a, min(b, c))
    n = _max_num - _min_num
    if n < min_num:
        min_num = n

for i in range(1, W):
    a = i*H
    b = H//2 * (W-i)
    c = math.ceil(H/2) * (W-i)
    _max_num = max(a, max(b, c))
    _min_num = min(a, min(b, c))
    n = _max_num - _min_num
    if n < min_num:
        min_num = n

for i in range(1, W):
    a = i*H
    b = (W-i)//2 * H
    c = math.ceil((W-i)/2) * H
    _max_num = max(a, max(b, c))
    _min_num = min(a, min(b, c))
    n = _max_num - _min_num
    if n < min_num:
        min_num = n


print(min_num)

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

def calc_ans(H,W):
    ans = float('inf')
    
    halfed_w = W//2
    for h in range(1,H):
        a  = h*W
        
        b1 = (H-h)//2*W
        c1 = (H-h-(H-h)//2)*W
        
        b2 = (H-h)*halfed_w
        c2 = (H-h)*(W-halfed_w)
        result1 = max(a,b1,c1)-min(a,b1,c1)
        result2 = max(a,b2,c2)-min(a,b2,c2)
        ans = min(ans,result1,result2)
    
    halfed_h = H//2
    for w in range(1,W):
        a = w*H
        
        b1 = (W-w)//2*H
        c1 = (W-w-(W-w)//2)*H
        
        b2 = (W-w)*halfed_h
        c2 = (W-w)*(H-halfed_h)
        result1 = max(a,b1,c1)-min(a,b1,c1)
        result2 = max(a,b2,c2)-min(a,b2,c2)
        ans = min(ans,result1,result2)
    print(ans)

calc_ans(H,W)

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

S_list = []
for h in range(1, H):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
H, W = W, H
for h in range(1, H):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
print(min(S_list))

Solution (Python):
H, W = map(int, input().split())
ans = 1e10
for w in range(1, W):
    rec1 = [H * w]
    rec1.append((W-w) * (H//2))
    rec1.append((W-w) * (H - H//2))
    dif = max(rec1) - min(rec1)
    ans = min(ans, dif)
    
    rec2 = [H*w]
    rec2.append((W-w)//2 * H)
    rec2.append( (W-w-(W-w)//2) * H )
    dif2 = max(rec2) - min(rec2)
    ans = min(ans, dif2)
    
for h in range(1, H):
    rec1 = [h*W]
    rec1.append((H-h) * (W//2))
    rec1.append((H-h) * (W - W//2))
    dif = max(rec1) - min(rec1)
    ans = min(ans, dif)
    
    rec2 = [h*W]
    rec2.append((H-h)//2 * W)
    rec2.append( (H-h-(H-h)//2) * W )
    dif2 = max(rec2) - min(rec2)
    ans = min(ans, dif2)

print(ans)

Solution (Python):

h,w=map(int,input().split())
ans=float('inf')
for i in range(1,h):
    a=i*w
    b1=(h-i)//2*w
    c1=(h-i-(h-i)//2)*w
    b2=(h-i)*(w//2)
    c2=(h-i)*(w-w//2)
    can1=max(a,b1,c1)-min(a,b1,c1)
    can2=max(a,b2,c2)-min(a,b2,c2)
    ans=min(ans,can1,can2)
for i in range(1,w):
    a=i*h
    b1=(w-i)//2*h
    c1=(w-i-(w-i)//2)*h
    b2=(w-i)*(h//2)
    c2=(w-i)*(h-h//2)
    can1=max(a,b1,c1)-min(a,b1,c1)
    can2=max(a,b2,c2)-min(a,b2,c2)
    ans=min(ans,can1,can2)
print(ans)

Solution (Python):
h,w=map(int,input().split())
ans=h*w
for x,y in ((h,w),(w,h)):
    x1=x//3
    x2=(x+2)//3
    y2=(y+1)//2
    ans=min(ans,abs(y*x2-y*x1))
    t=(y*x1,y2*(x-x1),(y-y2)*(x-x1))
    ans=min(ans,max(t)-min(t))
    t=(y*x2,y2*(x-x2),(y-y2)*(x-x2))
    ans=min(ans,max(t)-min(t))
print(ans)

Solution (Python):



h,w=map(int,input().split())
ans=h*w
if h*w%3==0:
    print(0)
else:
    for x,y in ((h,w),(w,h)):
        x1=x//3
        x2=(x+2)//3
        y1=y//2
        y2=(y+1)//2
        ans=min(ans,abs(y*x2-y*x1))
        t=(y*x1,y2*(x-x1),(y-y2)*(x-x1))
        ans=min(ans,max(t)-min(t))
        t=(y*x2,y2*(x-x2),(y-y2)*(x-x2))
        ans=min(ans,max(t)-min(t))
    print(ans)

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

if H%3==0 or W%3==0:
    print(0)
else:
    ans5=W
    ans6=H
    ans1=max(int(H/3)*W,(H-int(H/3))*int(W/2),H*W-int(H/3)*W-(H-int(H/3))*int(W/2))-min(int(H/3)*W,(H-int(H/3))*int(W/2),H*W-int(H/3)*W-(H-int(H/3))*int(W/2))
    ans2=max((int(H/3)+1)*W,(H-int(H/3)-1)*int(W/2),H*W-(int(H/3)+1)*W-(H-int(H/3)-1)*int(W/2))-min((int(H/3)+1)*W,(H-int(H/3)-1)*int(W/2),H*W-(int(H/3)+1)*W-(H-int(H/3)-1)*int(W/2))
    ans3=max(int(W/3)*H,(W-int(W/3))*int(H/2),W*H-int(W/3)*H-(W-int(W/3))*int(H/2))-min(int(W/3)*H,(W-int(W/3))*int(H/2),W*H-int(W/3)*H-(W-int(W/3))*int(H/2))
    ans4=max((int(W/3)+1)*H,(W-int(W/3)-1)*int(H/2),H*W-(int(W/3)+1)*H-(W-int(W/3)-1)*int(H/2))-min((int(W/3)+1)*H,(W-int(W/3)-1)*int(H/2),H*W-(int(W/3)+1)*H-(W-int(W/3)-1)*int(H/2))
    print(min(ans1,ans2,ans3,ans4,ans5,ans6))

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

ah = round(h / 3)
aw = w // 2
bh = h // 2
bw = round(w / 3)

ans1_area_1 = ah * w
ans1_area_2 = (h - ah) // 2 * w
ans1_area_3 = (h - (h - ah) // 2 - ah) * w
ans1 = max(ans1_area_1, ans1_area_2, ans1_area_3) - min(ans1_area_1, ans1_area_2, ans1_area_3)


ans2_area_1 = ah * w
ans2_area_2 = (h - ah) * aw
ans2_area_3 = (h - ah) * (w - aw)
ans2 = max(ans2_area_1, ans2_area_2, ans2_area_3) - min(ans2_area_1, ans2_area_2, ans2_area_3)

ans3_area_1 = bw * h
ans3_area_2 = (w - bw) // 2 * h
ans3_area_3 = (w - (w - bw) // 2 - bw) * h
ans3 = max(ans3_area_1, ans3_area_2, ans3_area_3) - min(ans3_area_1, ans3_area_2, ans3_area_3)

ans4_area_1 = bw * h
ans4_area_2 = (w - bw) * bh
ans4_area_3 = (w - bw) * (h - bh)
ans4 = max(ans4_area_1, ans4_area_2, ans4_area_3) - min(ans4_area_1, ans4_area_2, ans4_area_3)

print(min(ans1, ans2, ans3, ans4))

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

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


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



print(ans)

Solution (Python):

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

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

diff_list = [W,H]


split1 = [W//3, W//3 + 1]
split2 = [H//2, H//2 +1]

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


if H != W:
    split1 = [H//3, H//3 + 1]
    split2 = [W//2, W//2 + 1]

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


print(min(diff_list))

Solution (Python):
def 解():
    iH,iW = [int(_) for _ in input().split()]

    if iH % 3 == 0 or iW % 3 == 0:
        print(0)
    else:
        w3 = iW // 3
        w2 = iW // 2
        h3 = iH // 3
        h2 = iH // 2
        aY1 = [iH*w3,(iH-h2)*(iW-w3),h2*(iW-w3)]
        aY2 = [iH*(w3+1),(iH-h2)*(iW-w3-1),h2*(iW-w3-1)]
        aT1 = [iW*h3,(iW-w2)*(iH-h3),w2*(iH-h3)]
        aT2 = [iW*(h3+1),(iW-w2)*(iH-h3-1),w2*(iH-h3-1)]
        aH31 = [iW*h3,iW*(iH-2*h3)]
        aH32 = [iW*(h3+1),iW*(iH-2*(h3+1))]
        aW31 = [iH*w3,iH*(iW-2*w3)]
        aW32 = [iH*(w3+1),iH*(iW-2*(w3+1))]

        iMin = iH*iW
        for ea in [aY1,aY2,aT1,aT2,aH31,aH32,aW31,aW32]:
            iMin = min(iMin, max(ea) - min(ea))
        print(iMin)
解()

Solution (Python):
H, W = map(int, input().split())
S = []
def Scal(H, W):
    for i in range(1, (H//2)+1):
        S1 = W * i
        S2 = W * ((H-i)//2)
        S3 = H * W - S1 - S2
        s1 = [S1, S2, S3]
        S.append(s1)
        S2 = W//2 * (H-i)
        S3 = H * W - S1 - S2
        s2 = [S1, S2, S3]
        S.append(s2)
    
Scal(H, W)
Scal(W, H)
Sdif = []
for i in range(len(S)):
    Sdif.append(max(S[i])-min(S[i]))
print(min(Sdif))

Solution (Python):
from math import floor,ceil
H,W = map(int,input().split())
if H%3==0 or W%3==0:
    print(0)
    exit()
ans = min(H,W)
for i in range(1,W):
    M = max(i*H,floor(H/2)*(W-i),ceil(H/2)*(W-i))
    m = min(i*H,floor(H/2)*(W-i),ceil(H/2)*(W-i))
    if M-m<ans:
        ans = M-m
for i in range(1,H):
    M = max(i*W,floor(W/2)*(H-i),ceil(W/2)*(H-i))
    m = min(i*W,floor(W/2)*(H-i),ceil(W/2)*(H-i))
    if M-m<ans:
        ans = M-m
print(ans)

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

Solution (Python):
H, W = map(int, input().split())
if H%3==0 or W%3==0:
    print(0)
else:
    minH = float("inf")
    for i in range(1, H):
        A = W * i
        BH = W * ((H-i)//2)
        CH = H*W - A - BH
        BW = W//2 * (H-i)
        CW = H*W - A - BW
        Hdiff = max(A, BH, CH) - min(A, BH, CH)
        Wdiff = max(A, BW, CW) - min(A, BW, CW)
        minH = min(minH, min(Hdiff, Wdiff))

    minW = float("inf")
    for i in range(1, W):
        A = H * i
        BH = H * ((W-i)//2)
        CH = H*W - A - BH
        BW = H//2 * (W-i)
        CW = H*W - A - BW
        Hdiff = max(A, BH, CH) - min(A, BH, CH)
        Wdiff = max(A, BW, CW) - min(A, BW, CW)
        minW = min(minW, min(Hdiff, Wdiff))

    print(min(minH, minW))