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

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

    ans = h * w
    for _ in range(2):
        i = h // 3
        j = w // 2

        x = [(i - 1, h - i + 1), (i, h - i), (i + 1, h - i - 1)]
        y = [(j - 1, w - j + 1), (j, w - j), (j + 1, w - j - 1)]
        for (u, v) in x:
            
            a, b = v // 2, v - v // 2
            s = [w * u, w * a, w * b]
            d = max(s) - min(s)
            ans = min(ans, d)
            for (s, t) in y:
                
                s = [u * w, v * s, v * t]
                d = max(s) - min(s)
                ans = min(ans, d)

        w, h = h, w
    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:
    best = (h*w)/3
    
    col_num = int(round(best/h))
    remain_w = w - col_num
    h1 = h // 2
    h2 = h - h1
    w1 = remain_w // 2
    w2 = remain_w - w1
    min_area1 = min(remain_w*h1, remain_w*h2, col_num*h) 
    max_area1 = max(remain_w*h1, remain_w*h2, col_num*h)
    min_area2 = min(w2*h, w1*h, col_num*h) 
    max_area2 = max(w2*h, w1*h, col_num*h)
    num_w1 = max_area1 - min_area1
    num_w2 = max_area2 - min_area2
    
    row_num = int(round(best/w))
    remain_h = h - row_num
    w1 = w // 2
    w2 = w - w1
    h1 = remain_h // 2
    h2 = remain_h - h1
    min_area1 = min(remain_h*w1, remain_h*w2, row_num*w) 
    max_area1 = max(remain_h*w1, remain_h*w2, row_num*w)
    min_area2 = min(h2*w, h1*w, row_num*w) 
    max_area2 = max(h2*w, h1*w, row_num*w)
    num_h1 = max_area1 - min_area1
    num_h2 = max_area2 - min_area2
    print(min(num_h1,num_w1,num_h2,num_w2))

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

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

Solution (Python):
def abc062_c():
    h, w = map(int, input().split())
    
    
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        return
    
    ans = min(h, w)
    
    for x, y in [(w, h), (h, w)]:
        for i in range(1, x):
            sa = i * y
            sb = (x - i) * (y // 2)
            sc = (x - i) * (y - y // 2)
            diff = max(sa, sb, sc) - min(sa, sb, sc)
            ans = min(diff, ans)
    print(ans)

abc062_c()

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

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

if H%3==0 or W%3==0:
  print(0)
elif H==2 or W==2:
  ans0=min(H,W)
  
  H,W=min(H,W),max(H,W)
  w1=W//3
  w2=W-w1
  s1=2*w1
  s2=w2
  ans1=s2-s1
  
  w1=W//3+1
  w2=W-w1
  s1=2*w1
  s2=w2
  ans2=s1-s2
  print(min(ans0,ans1,ans2))
else:
  ans0=min(H,W)
  
  h1=H//2
  h2=H-h1
  
  w1=W//3
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans1=max(s1,s2,s3)-min(s1,s2,s3)
  
  w1=W//3+1
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans2=max(s1,s2,s3)-min(s1,s2,s3)
  
  H,W=W,H
  h1=H//2
  h2=H-h1
  
  w1=W//3
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans3=max(s1,s2,s3)-min(s1,s2,s3)
  
  w1=W//3+1
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans4=max(s1,s2,s3)-min(s1,s2,s3)
  print(min(ans0,ans1,ans2,ans3,ans4))

Solution (Python):
h, w = map(int, input().split())
min_diff = float('inf')
for i in range(1, h + 1):
    sa = i * w
    sb = (h - i) * (w // 2)
    sc = (h - i) * (w - (w // 2))
    min_diff = min(min_diff, max(sa, sb, sc) - min(sa, sb, sc))
    sb = (h - i) // 2 * w
    sc = (h - i - (h - i) // 2) * w
    min_diff = min(min_diff, max(sa, sb, sc) - min(sa, sb, sc))
h, w = w, h
for i in range(1, h + 1):
    sa = i * w
    sb = (h - i) * (w // 2)
    sc = (h - i) * (w - (w // 2))
    min_diff = min(min_diff, max(sa, sb, sc) - min(sa, sb, sc))
    sb = (h - i) // 2 * w
    sc = (h - i - (h - i) // 2) * w
    min_diff = min(min_diff, max(sa, sb, sc) - min(sa, sb, sc))
print(min_diff)

Solution (Python):
import sys
readline = sys.stdin.readline
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

def round(f, r=0):
    return Decimal(str(f)).quantize(Decimal(str(r)), rounding=ROUND_HALF_UP)

def main():
    H, W = map(int, readline().rstrip().split())
    ans = H * W
    if H >= 3:
        y1 = int(round(H/3))
        y2 = (H-y1) // 2
        y3 = H - y1 - y2
        ans = min(ans, max(W*y1, W*y2, W*y3)-min(W*y1, W*y2, W*y3))
    if W >= 3:
        x1 = int(round(W/3))
        x2 = (W-x1) // 2
        x3 = W - x1 - x2
        ans = min(ans, max(x1*H, x2*H, x3*H)-min(x1*H, x2*H, x3*H))
    
    
    x1 = int(round(W/3))
    y1 = H
    x2 = W - x1
    x3 = x2
    y2 = H // 2
    y3 = H - y2
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    
    y1 = int(round(H/3))
    x1 = W
    y2 = H - y1
    y3 = y2
    x2 = W // 2
    x3 = W - x2
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
readline = sys.stdin.readline
from math import floor, ceil
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

def round(f, r=0):
    return Decimal(str(f)).quantize(Decimal(str(r)), rounding=ROUND_HALF_UP)

def main():
    H, W = map(int, readline().rstrip().split())
    ans = H * W
    if H >= 3:
        y1 = int(round(H/3))
        y2 = (H-y1) // 2
        y3 = H - y1 - y2
        ans = min(ans, max(W*y1, W*y2, W*y3)-min(W*y1, W*y2, W*y3))
    if W >= 3:
        x1 = int(round(W/3))
        x2 = (W-x1) // 2
        x3 = W - x1 - x2
        ans = min(ans, max(x1*H, x2*H, x3*H)-min(x1*H, x2*H, x3*H))
    
    
    x1_cand = [int(round(W/3))]
    y1 = H
    for x1 in x1_cand:
        x2 = W - x1
        x3 = x2
        y2 = H // 2
        y3 = H - y2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    
    y1_cand = [int(round(H/3))]
    x1 = W
    for y1 in y1_cand:
        y2 = H - y1
        y3 = y2
        x2 = W // 2
        x3 = W - x2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
readline = sys.stdin.readline
from math import floor, ceil
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def round(f, r=0):
    return Decimal(str(f)).quantize(Decimal(str(r)), rounding=ROUND_HALF_UP)

def main():
    H, W = map(int, readline().rstrip().split())
    ans = H * W
    if H >= 3:
        y1 = int(round(H/3))
        y2 = (H-y1) // 2
        y3 = H - y1 - y2
        ans = min(ans, max(W*y1, W*y2, W*y3)-min(W*y1, W*y2, W*y3))
    if W >= 3:
        x1 = int(round(W/3))
        x2 = (W-x1) // 2
        x3 = W - x1 - x2
        ans = min(ans, max(x1*H, x2*H, x3*H)-min(x1*H, x2*H, x3*H))
    
    
    x1_cand = [floor(W/3), ceil(W/3)]
    
    y1 = H
    for x1 in x1_cand:
        x2 = W - x1
        x3 = x2
        y2 = H // 2
        y3 = H - y2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    
    y1_cand = [floor(H/3), ceil(H/3)]
    
    x1 = W
    for y1 in y1_cand:
        y2 = H - y1
        y3 = y2
        x2 = W // 2
        x3 = W - x2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
readline = sys.stdin.readline
from math import floor, ceil

def main():
    H, W = map(int, readline().rstrip().split())
    ans = H * W
    if H >= 3:
        ans = min(ans, abs(W*(H//3) - W*(H-H//3*2)))
    if W >= 3:
        ans = min(ans, abs(H*(W//3) - H*(W-W//3*2)))
    
    
    
    x1_cand = [i for i in range(1, W)]
    y1 = H
    for x1 in x1_cand:
        x2 = W - x1
        x3 = x2
        y2 = H // 2
        y3 = H - y2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

        x2 = (W-x1) // 2
        x3 = (W-x1-x2)
        ans = min(ans, max((x1*H), (x2*H), (x3*H)) - min((x1*H), (x2*H), (x3*H)))

    
    
    y1_cand = [i for i in range(1, H)]
    x1 = W
    for y1 in y1_cand:
        y2 = H - y1
        y3 = y2
        x2 = W // 2
        x3 = W - x2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

        y2 = (H-y1) // 2
        y3 = (H-y1-y2)
        ans = min(ans, max((W*y1), (W*y2), (W*y3)) - min((W*y1), (W*y2), (W*y3)))

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def calc(i, w, h):
	fixed = w * h
	a = w * i
	v_b = (h-i) // 2 * w
	v_c = fixed - a - v_b
	v_d = max(a, v_b, v_c) - min(a, v_b, v_c)
	h_b = w // 2 * (h-i)
	h_c = fixed - a - h_b
	h_d = max(a, h_b, h_c) - min(a, h_b, h_c)
	return min(v_d, h_d)

def resolve():
	h, w = map(int, input().split())
	ans = float('inf')
	for i in range(1, h):
		ans = min(ans, calc(i, w, h))
	for i in range(1, w):
		ans = min(ans, calc(i, h, w))
	print(ans)
resolve()

Solution (Python):
h , w = map(int, input().split())
mi=h*w
s=h*w
for i in range(h):
    s1=w*i
    if w%2==0 or (h-i)%2==0:
        s2=(s-s1)//2
        s3=s2
    elif h-i<w:
        s2=(h-i)*(w-1)//2
        s3=(h-i)*(w+1)//2
    else:
        s2=(h-i-1)//2*w
        s3=(h-i+1)//2*w
    d=abs(min(s1,s2,s3)-max(s1,s2,s3))
    if d<mi:
        mi=d
for i in range(w):
    s1=h*i
    if (w-i)%2==0 or h%2==0:
        s2=(s-s1)//2
        s3=s2
    elif w-i<h:
        s2=(w-i)*(h-1)//2
        s3=(w-i)*(h+1)//2
    else:
        s2=(w-i-1)//2*h
        s3=(w-i+1)//2*h
    d=abs(min(s1,s2,s3)-max(s1,s2,s3))
    if d<mi:
        mi=d
print(mi)

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


if w % 3 == 0:
    s_tate = 0
else:
    s_tate = (int(w/3)+1) * h - int(w/3) * h


if h % 3 == 0:
    s_yoko = 0
else:
    s_yoko = (int(h/3)+1) * w - int(h/3) * w


case3 = INF
if w%2 == 0:
    for i in range(1,h):
        buf = abs((int(w/2) * i) - (w * (h-i)))
        case3 = min(buf, case3)
else:
    for i in range(1,h):
        buf1 = int(w/2) * i
        buf2 = (int(w/2)+1) * i
        buf3 = w * (h - i)
        buf = [buf1, buf2, buf3]
        buf = sorted(buf)
        sa = buf[2] - buf[0]
        case3 = min(sa, case3)


case4 =INF
if h%2 == 0:
    for i in range(1,w):
        buf = abs((int(h/2) * i) - (h * (w-i)))
        case4 = min(buf, case4)
else:
    for i in range(1,w):
        buf1 = int(h/2) * i
        buf2 = (int(h/2)+1) * i
        buf3 = h * (w - i)
        buf = [buf1, buf2, buf3]
        buf = sorted(buf)
        sa = buf[2] - buf[0]
        case4 = min(sa, case4)
ans1 = min(s_tate, s_yoko)
ans2 = min(case3, case4)

ans = min(ans1, ans2)



print(ans)

Solution (Python):
def calc(w, h):
	v = float('inf')
	a = w * h
	for i in range(1, h):
		h_a = w*i
		h_b = (h - i)//2*w
		h_c = a - h_a - h_b
		h_max = max(h_a, h_b, h_c)
		h_min = min(h_a, h_b, h_c)
		v = min(v, h_max - h_min)
		v_b = w//2*(h-i)
		v_c = a - h_a - v_b
		v_max = max(h_a, v_b, v_c)
		v_min = min(h_a, v_b, v_c)
		v = min(v, v_max - v_min)
	return v

def resolve():
	h, w = map(int, input().split())
	print(min(calc(h, w), calc(w, h)))
resolve()

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

if H*W%3 == 0:
    p1 = 0
else:
    p1 = min(H,W)
p.append(p1)

for i in range(1,W+1):
    a = i*H
    if H%2 == 0:
        b = (W-i)*(H//2)
        p2 = abs(a-b)
    else:
        b = (W-i)*(H//2)
        c = (W-i)*(H//2 +1)
        p2 = max(a,b,c)-min(a,b,c)
    p.append(p2)

for i in range(1,H+1):
    a = i*W
    if W%2 == 0:
        b = (H-i)*(W//2)
        p3 = abs(a-b)
    else:
        b = (H-i)*(W//2)
        c = (H-i)*(W//2 +1)
        p3 = max(a,b,c)-min(a,b,c)
    p.append(p3)

print(min(p))

Solution (Python):
def resolve():
    H, W = list(map(int, input().split()))
    
    def _solve(H, W):
        mean_w = W // 2
        ans = float("inf")
        for h in range(1, H):
            s1 = h * W
            mean_h = (H-h) // 2
            for i in range(0, 2):
                if i == 0:
                    s2 = (H-h)*mean_w
                    s3 = (H-h)*(W-mean_w)
                if i == 1:
                    s2 = mean_h*W
                    s3 = (H-h-mean_h)*W
                ans = min(ans, max(s1, s2, s3)-min(s1, s2, s3))
        return ans

    print(min(_solve(H, W), _solve(W, H)))

if '__main__' == __name__:
    resolve()

Solution (Python):
def min_value(w, h):
	ans = float('inf')
	for i in range(1, h):
		a = w*i
		v_t = (h-i)//2 * w
		v_b = (h-i)*w - v_t
		v_re = max(a, v_t, v_b) - min(a, v_t, v_b)
		h_l = (h-i)*(w//2)
		h_r = (h-i)*w-h_l
		h_re = max(a, h_l, h_r) - min(a, h_l, h_r)
		ans = min(ans, v_re, h_re)
	return ans

def resolve():
	h, w = map(int, input().split())
	a = min_value(w, h)
	b = min_value(h, w)
	print(min(a, b))
resolve()

Solution (Python):
from math import ceil


def main():
    H, W = map(int, input().split())
    if H*W % 3 == 0:
        print(0)
        return
    ans1 = float('inf')
    ans2 = float('inf')
    ans3 = min(H, W)
    
    for h in range(1, H + 1):
        if W % 2 == 0:
            ans1 = min(ans1, abs(h*(W//2) - W*(H - h)))
        else:
            ans1 = min(
                ans1,
                max([
                    abs((h*(W//2) - W*(H - h))),
                    abs(h*((W + 1)//2) - W*(H - h)),
                    h
                ]))
    for w in range(1, W + 1):
        if H % 2 == 0:
            ans2 = min(ans2, abs(w*(H//2) - H*(W - w)))
        else:
            ans2 = min(
                ans2, max([abs((w*(H//2) - H*(W - w))), abs(w*((H + 1)//2) - H*(W - w)), w]))
    print(min([ans1, ans2, ans3]))
    return


if __name__ == "__main__":
    main()

Solution (Python):
H, W = map(int, input().split())
S = H*W
ans = 10**19
for w in range(1, W//2+1):
    s = w*H
    w2s = ((W-w)//2)*H
    h2s = (H//2)*(W-w)

    ws = [s, w2s, S-w2s-s]
    hs = [s, h2s, S-h2s-s]
    wm = max(ws)-min(ws)
    hm = max(hs)-min(hs)
    m = min(wm, hm)
    if m < ans:
        ans = m

H, W = W, H
for w in range(1, W//2+1):
    s = w*H
    w2s = ((W-w)//2)*H
    h2s = (H//2)*(W-w)

    ws = [s, w2s, S-w2s-s]
    hs = [s, h2s, S-h2s-s]
    wm = max(ws)-min(ws)
    hm = max(hs)-min(hs)
    m = min(wm, hm)
    if m < ans:
        ans = m
print(ans)

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

def sds(h,w):
    sdmin=h*w
    for hi in range(1,h):
        s1=hi*w
        s2=(h-hi)*(w//2)
        s3=(h-hi)*(w-w//2)
        sd=max(s1,s2,s3)-min(s1,s2,s3)
        sdmin=min(sdmin,sd)
    
        h2=(h-hi)//2
        s2=h2*w
        s3=(h-hi-h2)*w
        sd=max(s1,s2,s3)-min(s1,s2,s3)
        sdmin=min(sdmin,sd)

    return sdmin

print(min(sds(h,w),sds(w,h)))

Solution (Python):
h,w=map(int,input().split())
mini=h*w
for i in range(1,h):
    if mini > max(w*(h-i),(w//2+w%2)*i) - min(w*(h-i),(w//2)*i):
        mini = max(w*(h-i),(w//2+w%2)*i) - min(w*(h-i),(w//2)*i)

for i in range(1,w):
    if mini > max(h*(w-i),(h//2+h%2)*i) - min(h*(w-i),(h//2)*i):
        mini = max(h*(w-i),(h//2+h%2)*i) - min(h*(w-i),(h//2)*i)

if h%3 == 0 or w%3 == 0:
    mini=0
else:
    if mini > h:
        mini = h
    elif mini > w:
        mini = w
    else:
        pass
print(mini)

Solution (Python):
h,w = map(int, input().split())
ans = 1000000000
if max(h,w) >= 3:
  if h >= w :
    ans = w*min(1,(h%3))
  else:
    ans = h*min(1,(w%3))
if h%3 == 0:
  ans = 0
if w%3 == 0:
  ans = 0
for i in range(1,h):
  ans_ = max(i*w,(h-i)*(w+(w%2))//2) - min(i*w,(h-i)*(w-(w%2))//2)
  ans = min(ans,ans_)
for i in range(1,w):
  ans_ = max(i*h,(w-i)*(h+(h%2))//2) - min(i*h,(w-i)*(h-(h%2))//2)
  ans = min(ans,ans_)
print(ans)

Solution (Python):
def main():
    H, W = (int(_) for _ in input().split())
    INF = 10 ** 11

    def calc(h, w):
        
        w1 = w // 2
        w2 = w - w1
        ret = INF
        for hh in range(1, h):
            ret = min(ret, max(w * hh, (h-hh)*w1, (h-hh)*w2) - min(w * hh, (h-hh)*w1, (h-hh)*w2))
        
        for hh in range(1, h):
            h1 = (h-hh)//2
            h2 = h - hh - h1
            ret = min(ret, max(w*hh, w*h1, w*h2) - min(w*hh, w*h1, w*h2))
        return ret
    m1 = calc(H, W)
    m2 = calc(W, H)
    print(min(m1, m2))
    return
if __name__ == '__main__':
    main()

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


def calc(a, b):
    ret = a * b
    for i in range(1, a):
        area1 = b // 2 * i
        area2 = -(-b // 2) * i
        area3 = a * b - (area1 + area2)
        
        ret = min(ret, max(area1, area2, area3) - min(area1, area2, area3))
    return ret

if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ans = h * w
    for i in range(1, max(h, w) - 1):
        area1 = min(h, w) * i
        area2 = min(h, w) * ((max(h, w) - i) // 2)
        area3 = min(h, w) * (-(-(max(h, w) - i) // 2))
        
        ans = min(ans, max(area1, area2, area3) - min(area1, area2, area3))
    print(min(calc(h, w), calc(w, h), ans))

Solution (Python):
import sys
H, W = map(int,input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
    sys.exit()
def solve(p, q):
    r = int(p / 2)
    s = int(q / 3)
    a, b, c = p*s, r*(q-s), (p-r)*(q-s)
    ans = max(a, b, c) - min(a, b, c)
    a, b, c = p*(s+1), r*(q-s-1), (p-r)*(q-s-1)
    ans = min(ans, max(a, b, c) - min(a, b, c))
    return ans
ans = min(W, H, solve(H, W), solve(W, H))
print(ans)

Solution (Python):



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

if h%3==0 or w%3==0:
  ans=0
else:
  ans=min(h,w)
  
a=round(h/3)*w
b=(h-round(h/3))*(w//2)
c=h*w-a-b
d=max(a,b,c)-min(a,b,c)
ans=min(ans,d)
a=round(w/3)*h
b=(w-round(w/3))*(h//2)
c=h*w-a-b
d=max(a,b,c)-min(a,b,c)
ans=min(ans,d)
print(ans)

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

Solution (Python):
h,w=map(int,input().split())
ans=[]
import sys
def s(a,b):
    for i in range(a):
        s=a*b
        x=i*b
        y=((a-i)//2)*b
        z=s-x-y

        d=(a-i)*(b//2)
        e=s-x-d
        ma=max(x,y,z)
        mi=min(x,y,z)
        mc=max(x,e,d)
        mn=min(x,e,d)
        res=min((ma-mi),(mc-mn))
        ans.append(res)
    return min(ans)
        
f=s(h,w)
g=s(w,h)
print(min(f,g))

Solution (Python):
h,w = map(int,input().split())
s = [h*w] * 4

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

if w > 2:
  s[0] = h
if h > 2:
  s[1] = w
if h % 2 == 0:
  s[2] = h // 2
else:
  tmp = h*w
  for i in range(w//3, w//3+2):
    s1 = i * h
    s2 = (w-i) * (h//2)
    s3 = (w-i) * (h//2 + 1)
    tmp = min(tmp, max(s1, s2, s3) - min(s1, s2, s3))
  s[2] = tmp
if w % 2 == 0:
  s[3] = w // 2
else:
  tmp = h*w
  for i in range(h//3, h//3 + 2):
    s1 = i * w
    s2 = (h-i) * (w//2)
    s3 = (h-i) * (w//2 + 1)
    tmp = min(tmp, max(s1, s2, s3) - min(s1, s2, s3))
  s[3] = tmp

print(min(s))

Solution (Python):
h,w=map(int,input().split())
if h==2 and w==2:
  print(1)
  exit()
ans=10**10
if h>=3:
  if h%3==0:
    a=0
  else:
    a=w
  if a<ans:
    ans=a
if w>=3:
  if w%3==0:
    a=0
  else:
    a=h
  if a<ans:
    ans=a
if h>1:
  b=h//3
  x1=w*b
  x2=(h-b)*(w//2)
  x3=h*w-x1-x2
  a=max(x1,x2,x3)-min(x1,x2,x3)
  if a<ans:
    ans=a
  x1=w*(b+1)
  x2=(h-b-1)*(w//2)
  x3=h*w-x1-x2
  a=max(x1,x2,x3)-min(x1,x2,x3)
  if a<ans:
    ans=a
  b=w//3
  x1=h*b
  x2=(w-b)*(h//2)
  x3=h*w-x1-x2
  a=max(x1,x2,x3)-min(x1,x2,x3)
  if a<ans:
    ans=a
  x1=h*(b+1)
  x2=(w-b-1)*(h//2)
  x3=h*w-x1-x2
  a=max(x1,x2,x3)-min(x1,x2,x3)
  if a<ans:
    ans=a
print(ans)

Solution (Python):
from math import ceil,floor
H,W=map(int,input().split())
a=float("inf")
b=0
c=-float("inf")
mi=float("inf")
if W>2:                             
    if W%3==0:
        a=(H*W)//3
        b=(H*W)//3
        c=(H*W)//3
    elif W%3==1:
        a=H*(W//3)
        b=H*(W//3)
        c=H*((W//3)+1)
    else:
        a=H*(W//3)
        b=H*((W//3)+1)
        c=H*((W//3)+1)
    mi=min(mi,max(a,b,c)-min(a,b,c))
if H>2:                             
    if H%3==0:
        a=(H*W)//3
        b=(H*W)//3
        c=(H*W)//3
    elif H%3==1:
        a=W*(H//3)
        b=W*(H//3)
        c=W*((H//3)+1)
    else:
        a=W*(H//3)
        b=W*((H//3)+1)
        c=W*((H//3)+1)
    mi=min(mi,max(a,b,c)-min(a,b,c))

for i in range(1,H,1):
    a = float("inf")
    b = W*i
    c = -float("inf")
    H1=ceil((H-i)/2)
    H2=floor((H-i)/2)
    W1=ceil(W/2)
    W2=floor(W/2)
    mi=min(mi,max(W*H1,b,W*H2)-min(W*H1,b,W*H2),max((H-i)*W1,b,(H-i)*W2)-min((H-i)*W1,b,(H-i)*W2))

for i in range(1,W,1):
    a = float("inf")
    b = H*i
    c = -float("inf")
    W1=ceil((W-i)/2)
    W2=floor((W-i)/2)
    H1=ceil(H/2)
    H2=floor(H/2)
    mi=min(mi,max((W-i)*H1,b,(W-i)*H2)-min((W-i)*H1,b,(W-i)*H2),max(W1*H,b,W2*H)-min(W1*H,b,W2*H))
print(mi)

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

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

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

H, W = W, H

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

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

print(ans)

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

def defs(h, w):
    ans = 10 ** 12
    for i in range(1, h):
        sa = i * w
        h2 = h - i

        for j in range(2):
            if j == 0:
                sb = h2 * (w // 2)
                sc = h2 * (w - (w // 2))
            else:
                sb = (h2 // 2) * w
                sc = (h - i - (h2 // 2)) * w

            s_max = max(sa, sb, sc)
            s_min = min(sa, sb, sc)

            ans = min(ans, s_max - s_min)
    return ans

print(min(defs(h, w), defs(w, h)))

Solution (Python):
def f(x,y,z):
    return max(x,y,z)-min(x,y,z)

h, w = map(int, input().split())
l = []
ans1, ans2 = h*w, h*w
for i in range(1,h//2+1):
        s1 = i*w
        s2 = w*((h-i)//2)
        s3 = h*w-s1-s2
        s4 = (h-i)*(w//2)
        s5 = h*w-s1-s4
        ans1 = min(ans1, f(s1,s2,s3),f(s1,s4,s5))

for i in range(1,w//2+1):
        s1 = i*h
        s2 = h*((w-i)//2)
        s3 = h*w-s1-s2
        s4 = (w-i)*(h//2)
        s5 = h*w-s1-s4
        ans2 = min(ans2, f(s1,s2,s3),f(s1,s4,s5))
print(min(ans1,ans2))

Solution (Python):
import sys

def solve(h, w):
  res = [((h + 2) // 3 - h // 3) * w]
  for d in [h // 3, (h + 2) // 3]:
    cand = sorted([d * w, (h - d) * (w // 2), (h - d) * ((w + 1) // 2)])
    res.append(cand[-1] - cand[0])
  return min(res)

h, w = map(int, sys.stdin.readline().split())

def main():
  print(min(solve(h, w), solve(w, h)))

if __name__ == '__main__':
  main()

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:
    return a
  return gcd_(a,a % b) 
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  H,W = readInts()
  LIST = [H//2 + W//3 + 1, W//3 + H//2 + 1, H, W]
  if H%3 == 0 or W % 3 == 0:
    LIST += [0]
  if H % 2 == 0:
    LIST += [H//2]
  if W % 2 == 0:
    LIST += [W//2]
  print(min(LIST))
  
if __name__ == '__main__':
  main()

Solution (Python):
h,w = map(int,input().split())
if (h%3 == 0) or (w%3 == 0):
  print(0)
else:
  ans = h*w
  for i in range(1,max(h,w)//2 + 1):
    a = min(h,w)*i
    b = (max(h,w)-i)*(min(h,w)//2)
    if min(h,w)%2 != 0:
      c = (max(h,w)-i)*(min(h,w)//2 + 1)
    else:
      c = b
    kari = max(a,b,c)-min(a,b,c)
    if ans > kari:
      ans = kari
  ans3 = h*w
  for i in range(1,min(h,w)//2 + 1):
    a = max(h,w)*i
    b = (min(h,w)-i)*(max(h,w)//2)
    if max(h,w)%2 != 0:
      c = (min(h,w)-i)*(max(h,w)//2 + 1)
    else:
      c = b
    kari = max(a,b,c)-min(a,b,c)
    if ans3 > kari:
      ans3 = kari
  ans2 = ((max(h,w)//3 + 1)*min(h,w))-((max(h,w)//3)*min(h,w))
  print(min(ans,ans2,ans3))

Solution (Python):




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

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

    area = H * W / 3
    result = 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) < result:
                    result = max(areas) - min(areas)

    print(result)


if __name__ == "__main__":
    main()

Solution (Python):
S = input().split(" ")
H = int(S[0])
W = int(S[1])


def calculate(h, w):
    result = []
    for i in range(1, h):
        s1 = w * i
        s2 = (h - i) * (w // 2)
        s3 = (h - i) * w - s2

        minValue = min(min(s1, s2), s3)
        maxValue = max(max(s1, s2), s3)

        result.append(maxValue - minValue)

        if i <= h - 1:
            s1 = w * i
            s2 = ((h - i) // 2) * w
            s3 = (h - i) * w - s2
            minValue = min(min(s1,s2),s3)
            maxValue = max(max(s1,s2),s3)
            result.append(maxValue - minValue)

    for i in range(1, w):
        if i <= w - 1:
            s1 = i * h
            s2 = ((w - i) // 2) * h
            s3 = (w - i) * h - s2

            minValue = min(min(s1, s2), s3)
            maxValue = max(max(s1, s2), s3)
            result.append(maxValue - minValue)

        s1 = i * h
        s2 = (w - i) * (h //2)
        s3 = (w - i) * h - s2
        minValue = min(min(s1, s2), s3)
        maxValue = max(max(s1, s2), s3)
        result.append(maxValue - minValue)

    print(min(result))

calculate(H, W)

Solution (Python):
def solve(s1, s2, s3):
    S = [s1, s2, s3]
    return(max(S) - min(S))


H, W = map(int, input().split())
S = H * W
for j in range(1, W):
    S1 = H * j
    i = H // 2
    S2 = (W - j) * i
    S3 = (W - j) * (H - i)
    S = min(S, solve(S1, S2, S3))

for i in range(1, H):
    S1 = W * i
    j = W // 2
    S2 = (H - i) * j
    S3 = (H - i) * (W - j)
    S = min(S, solve(S1, S2, S3))

S = min(S, H * (W % 3 > 0))
S = min(S, W * (H % 3 > 0))


print(S)

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

ans=float('inf')
for i in range(1,w):
    tmp1=h*i
    tmp2=h*((w-i)//2)
    tmp3=h*(w-i-(w-i)//2)
    if tmp2!=0:
        ans=min(ans,max(tmp1,tmp2,tmp3)-min(tmp1,tmp2,tmp3))
    tmp2=(w-i)*(h//2)
    tmp3=(w-i)*(h-h//2)
    ans=min(ans,max(tmp1,tmp2,tmp3)-min(tmp1,tmp2,tmp3))

h,w=w,h
for i in range(1,w):
    tmp1=h*i
    tmp2=h*((w-i)//2)
    tmp3=h*(w-i-(w-i)//2)
    if tmp2!=0:
        ans=min(ans,max(tmp1,tmp2,tmp3)-min(tmp1,tmp2,tmp3))
    tmp2=(w-i)*(h//2)
    tmp3=(w-i)*(h-h//2)
    ans=min(ans,max(tmp1,tmp2,tmp3)-min(tmp1,tmp2,tmp3))
print(ans)

Solution (Python):
H,W=map(int,input().split())
ans=H*W
if(W%3==0):
  ans=0
else:
  ans=min(ans,H)
x=W//3
s1=x*H
s2=(W-x)*(H//2)
s3=(W-x)*H-s2
ans=min(ans,max(s1,max(s2,s3))-min(s1,min(s2,s3)))

x=W//3+1
s1=x*H
s2=(W-x)*(H//2)
s3=(W-x)*H-s2

ans=min(ans,max(s1,max(s2,s3))-min(s1,min(s2,s3)))

a=H
H=W
W=a
if(W%3==0):
  ans=0
else:
  ans=min(ans,H)
x=W//3
s1=x*H
s2=(W-x)*(H//2)
s3=(W-x)*H-s2
ans=min(ans,max(s1,max(s2,s3))-min(s1,min(s2,s3)))

x=W//3+1
s1=x*H
s2=(W-x)*(H//2)
s3=(W-x)*H-s2
ans=min(ans,max(s1,max(s2,s3))-min(s1,min(s2,s3)))

print(ans)

Solution (Python):
h, w = map(int, input().split())
def choco(H, W):
    ans = float("inf")
    a, b, c = 0, 0, 0
    for i in range(1, H):
        a = i*W
        for j in range(2):
            if j == 0:
                b = (H-i)//2 * W
                c = H*W - a - b 
            else:
                b = (H-i)*(W//2)
                c = H*W - a - b
            ans = min(ans, max(a, b, c) - min(a, b, c))
    return ans

print(min(choco(h, w), choco(w, h)))

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

if h%3==0:
    d=0
elif w%3==0:
    d=0
else:
    if h>=w:
        h,w=w,h
    d1=h
    d2min=h*w
    for ww in range(1,w):
        h1=h//2
        h2=h-h1
        d2=max(ww*h,(w-ww)*h2)-min(ww*h,(w-ww)*h1)

        d2min=min(d2,d2min)

    for hh in range(1,h):
        w1=w//2
        w2=w-w1
        d2=max(hh*w,(h-hh)*w2)-min(hh*w,(h-hh)*w1)

        d2min=min(d2,d2min)

    d=min(d1,d2min)
print(d)