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



Sample input:
3 5


Sample output:
0

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

h,w=map(int,input().split())
h2=h+2
w2=w+2
if h*w%3==0:
    print(0)
#elif h==w:
else:
    x=[abs((h2//3*w)-(h-(h2//3))*(w//2)),abs(((w2//3*h)-(w-(w2//3))*(h//2)))]

    #x=[abs((h//3*w)-(h-(h//3))*(w//2)),abs((w//3*h)-(w-(w//3))*(h//2))]
    x.sort()
    print(x[0])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h*w%3==0:
    print(0)
#elif h==w:
else:
    x=[abs((h//3*w)-(h-(h//3))*(w//2)),abs((w//3*h)-(w-(w//3))*(h//2))]
    x.sort()
    print(x[0])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h*w%3==0:
    print(0)
else:
    print(min((h//2*w)-((h+1)//2*(w//2)),(w//2*h)-((w+1)//2)*(h//2)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

h,w=map(int,input().split())
if h*w%3==0:
    print(0)
elif h==w and h%2==0:
    print(h//2)
else:
    print(min((h//2*w)-((h+1)//2*(w//2)),(w//2*h)-((w+1)//2)*(h//2)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

h,w=map(int,input().split())
if h*w%3==0:
    print(0)
elif h==w and h%2==0:
    print(h//2)
else:
    print(h)

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
    print(0)
else:
    ans5=max(int(H/3)*W,H-2*int(H/3)*W)-min(int(H/3)*W,H-2*int(H/3)*W)
    ans6=max(int(W/3)*H,W-2*int(W/3)*H)-min(int(W/3)*H,W-2*int(W/3)*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))

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
    print(0)
else:
    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))

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split(" "))
ar = []
if a % 3 == 0 or b % 3 == 0:
    ar.append(0)
elif a >= b:
    ar.append((a // 3 + 1) * b)
    if b % 2 == 0:
        ar.append((a - (a // 3 + 1)) * (b // 2))
        ar.append((a - (a // 3 + 1)) * (b // 2))
    else:
        ar.append((a - (a // 3 + 1)) * (b // 2 + 1))
        ar.append((a - (a // 3 + 1)) * (b // 2))
else:
    ar.append((b // 3 + 1) * a)
    if a % 2 == 0:
        ar.append((b - (b // 3 + 1)) * (a // 2))
        ar.append((b - (b // 3 + 1)) * (a // 2))
    else:
        ar.append((b - (b // 3 + 1)) * (a // 2 + 1))
        ar.append((b - (b // 3 + 1)) * (a // 2))
print(ar)
print(max(ar)-min(ar))

Submission status: Wrong Answer

Solution code:
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
	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)

Submission status: Wrong Answer

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

ans = 0


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

if H % 2 == 1 and W % 2 == 1:
	a_l = [((H+1)*(W+1))//4, ((H+1)*(W-1))//4, ((H-1)*W)//2]
	a_1 = max(a_l) - min(a_l)
	H, W = W, H
	a_l = [((H+1)*(W+1))//4, ((H+1)*(W-1))//4, ((H-1)*W)//2]
	a_2 = max(a_l) - min(a_l)
	ans = min(a_1,a_2)


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

print(ans)

Submission status: Wrong Answer

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

ans = 0


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

if H % 2 == 1 and W % 2 == 1:
	a_l = [((H+1)*(W+1))//4, ((H+1)*(W+1))//4, ((H-1)*W)//2]
	a_1 = max(a_l) - min(a_l)
	H, W = W, H
	a_l = [((H+1)*(W+1))//4, ((H+1)*(W+1))//4, ((H-1)*W)//2]
	a_2 = max(a_l) - min(a_l)
	ans = min(a_1,a_2)


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

print(ans)

Submission status: Wrong Answer

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

ans = 0


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

if H % 2 == 1 and W % 2 == 1:
	a_l = [((H+1)*(W+1))//4, ((H+1)*(W+1))//4, ((H-1)*W)//2]
	a_1 = max(a_l) - min(a_l)
	H, W = W, H
	a_l = [((H+1)*(W+1))//4, ((H+1)*(W+1))//4, ((H-1)*W)//2]
	a_2 = max(a_l) - min(a_l)
	ans = min(a_1,a_2)
	print(a_1,a_2)

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

print(ans)

Submission status: Wrong Answer

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

ans = 0


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

if H % 2 == 1 and W % 2 == 1:
	ans = min((H // 2) * W - (H // 2 + 1) * (W // 2), (W // 2) * H - (W // 2 + 1) * (H // 2))

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

print(ans)

Submission status: Wrong Answer

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

ans = 0

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

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

if H % 2 == 1 and W % 2 == 1:
	ans = min((H // 2) * W - (H // 2 + 1) * (W // 2), (W // 2) * H - (W // 2 + 1) * (H // 2))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
H, W = map(int, input().split())

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

diff_list = []

#縦→横
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(diff_list)
print(min(diff_list))

Submission status: Wrong Answer

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

    if iH % 3 == 0 or iW % 3 == 0:
        print(0)
    elif iH % 2 == 0 and iW > 2:
        iW3 = iW // 3
        print(min(abs(iW3*iH - (iW-iW3)*(iH//2)) ,abs((iW3+1)*iH - (iW-iW3 -1) * (iH //2))))
    elif iW % 2 == 0 and iH > 2:
        iH3 = iH // 3
        print(min(abs(iH3*iW - (iH-iH3)*(iW//2)) ,abs((iH3+1)*iW - (iH-iH3 -1) * (iW //2))))
    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)]

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

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
#smin = 1e10
l = []
if h % 3 == 0:
    print(0)
elif w % 3 == 0:
    print(0)
else:
    if h > w:
        x = w
        y = h
    else:
        x = h
        y = w
    for i in range(y-1):
        s = []
        s.append(x*(i+1))
        s.append((y-i-1)*int(x/2))
        s.append((y-i-1)*(x-int(x/2)))
        s.sort()
        #if s[2] - s[0] < smin:
         #   smin = s[2] - s[0]
        l.append(s[2]-s[0])
    
    print(min(l))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
smin = 1e10

if h % 3 == 0:
    print(0)
elif w % 3 == 0:
    print(0)
else:
    if h > w:
        x = w
        y = h
    else:
        x = h
        y = w
    for i in range(y-1):
        s = []
        s.append(x*(i+1))
        s.append((y-i-1)*int(x/2))
        s.append((y-i-1)*(x-int(x/2)))
        s.sort()
        if s[2] - s[0] < smin:
            smin = s[2] - s[0]
    print(smin)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
smin = 1e9

if h % 3 == 0:
    print(0)
elif w % 3 == 0:
    print(0)
else:
    if h > w:
        x = w
        y = h
    else:
        x = h
        y = w
    for i in range(y-1):
        s = []
        s.append(x*(i+1) )
        s.append((y - i -1)*int(x/2))
        s.append((y-i-1)*(x-int(x/2)))
        s.sort()
        if s[2] - s[0] < smin:
            smin = s[2] - s[0]
    print(smin)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
s = [h//2+w//3+1, h//3+w//2+1]
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))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def f(L,S):
    if L%3==2:
        L1s = (L+1)//3
    else:
        L1s = (L-1)//3

    A = L1s * S

    L2=max(S,L-L1s);S2=min(S,L-L1s)
    if L2%2==0 or S2%2==0:
        B = L2*S2 //2
    else:
        B = L2//2 *S2

    return int(math.fabs(A-B))

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

Submission status: Wrong Answer

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

L=max(H,W);S=min(H,W)
if L%3==2:
    L1s = (L+1)//3
else:
    L1s = (L-1)//3

A = L1s * S

L2=max(S,L-L1s);S2=min(S,L-L1s)
if L2%2==0 or S2%2==0:
    B = L2*S2 //2
else:
    B = L2//2 *S2

print(int(math.fabs(A-B)))

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    print(0)
else:
    ma = min(H, W) * (int(max(H,W)/3) + 1)
    mi = int(min(H, W)/2) * (max(H,W) - int(max(H,W)/3) - 1)
    print(ma - mi)

Submission status: Wrong Answer

Solution code:
na = lambda: list(map(int, input().split()))
h, w = na()
if (h*w) % 3 == 0:
    print(0)
else:
    l = []
    a = (max(h, w) - max(h, w) // 3) // 2
    l.append(a * min(h, w))
    b = max(h, w) - a
    c = min(h, w) // 2
    l.append(b * c)
    l.append(b * (min(h, w)-c))
    print(max(l) - min(l))

Submission status: Wrong Answer

Solution code:
H, W = map( int, input().split())
if H%3 == 0 or W%3 == 0:
    ans = 0
else:
    ans = 10**10
    for i in range(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 j in range(W+1):
        ans = min( ans, max(H*j, (W-j)*(H//2), (W-j)*(H - H//2)) - min(H*j, (W-j)*(H//2), (W-j)*(H - H//2)))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map( int, input().split())
if H%3 == 0 or W%3 == 0:
    ans = 0
else:
    ans = 10**10
    for i in range(1,H):
        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 j in range(1,W):
        ans = min( ans, max(H*j, (W-j)*(H//2), (W-j)*(H - H//2)) - min(H*j, (W-j)*(H//2), (W-j)*(H - H//2)))
print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import math

INF = float('inf')


def inpl(): return list(map(int, input().split()))


H, W = inpl()

ans = INF
for w in range(1, W):
    s1 = (W - w) * H
    s2 = math.ceil(H / 2) * w
    s3 = math.floor(H / 2) * w

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

for h in range(1, H):
    s1 = (H - h) * W
    s2 = math.ceil(W / 2) * h
    s3 = math.floor(W / 2) * h

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

ans = 1e10
for i in range(1,W):
  blk1 = i * H
  # vertical cut
  if W-i >1:
    blk2 = (W-i)//2 * H
    blk3 = (W-i- (W-i)//2) * H
    #print(blk1, blk2, blk3)
    ans = min(ans, max(blk1, blk2, blk3) - min(blk1, blk2, blk3))
  # horizontal cut
  if H>1:
    blk2 = (W-i) * (H//2)
    blk3 = (W-i) * (H - (H//2))
    #print(blk1, blk2, blk3)
    ans = min(ans, max(blk1, blk2, blk3) - min(blk1, blk2, blk3))

print(ans)

Submission status: Wrong Answer

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

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

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

ans = 1e10
for i in range(1,W):
  blk1 = i * H
  # vertical cut
  blk2 = (W-i)//2 * H
  blk3 = (W-i- (W-i)//2) * H
  #print(blk1, blk2, blk3)
  ans = min(ans, max(blk1, blk2, blk3) - min(blk1, blk2, blk3))
  # horizontal cut
  blk2 = (W-i) * (H//2)
  blk3 = (W-i) * (H - (H//2))
  #print(blk1, blk2, blk3)
  ans = min(ans, max(blk1, blk2, blk3) - min(blk1, blk2, blk3))

print(ans)

Submission status: Wrong Answer

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

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

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

ans = 1e10
for i in range(1,W):
  blk1 = i * H
  # vertical cut
  blk2 = (W-i)//2 * H
  blk3 = (W- (W-i)//2) * H
  #print(blk1, blk2, blk3)
  ans = min(ans, max(blk1, blk2, blk3) - min(blk1, blk2, blk3))
  # horizontal cut
  blk2 = (W-i) * (H//2)
  blk3 = (W-i) * (H - H//2)
  #print(blk1, blk2, blk3)
  ans = min(ans, max(blk1, blk2, blk3) - min(blk1, blk2, blk3))

print(ans)

Submission status: Wrong Answer

Solution code:
H, W=map(int, input().split())
m=float("inf")
for i in range(1,H-1):
    S1=i*W
    S2=W*((H-i)//2)
    S3=H*W-S1-S2
    if m>max(S1, S2, S3)-min(S1, S2, S3):
        m=max(S1, S2, S3)-min(S1, S2, S3)

    S2=(H-i)*(W//2)
    S3=H*W-S1-S2
    if m>max(S1, S2, S3)-min(S1, S2, S3):
        m=max(S1, S2, S3)-min(S1, S2, S3)
H,W=W,H
for i in range(1,H-1):
    S1=i*W
    S2=W*((H-i)//2)
    S3=H*W-S1-S2
    if m>max(S1, S2, S3)-min(S1, S2, S3):
        m=max(S1, S2, S3)-min(S1, S2, S3)

    S2=(H-i)*(W//2)
    S3=H*W-S1-S2
    if m>max(S1, S2, S3)-min(S1, S2, S3):
        m=max(S1, S2, S3)-min(S1, S2, S3)
print(m)

Submission status: Wrong Answer

Solution code:
H, W=map(int, input().split())
m=float("inf")
for i in range(1,H-1):
    S1=i*W
    if (H-1)%2==0:
        S2=W*((H-1)//2)
    else:
        S2=(H-i)*(W//2)
    S3=H*W-S1-S2
    if m>max(S1, S2, S3)-min(S1, S2, S3):
        m=max(S1, S2, S3)-min(S1, S2, S3)
print(m)

Submission status: Wrong Answer

Solution code:
H, W=map(int, input().split())
m=float("inf")
for i in range(1,H-1):
    S1=i*W
    if (H-1)%2==0:
        S2=W*((H-1)//2)
    else:
        S2=(H-i)*(W//2)
    S3=H*W-S1-S2
    if m>max(S1, S2, S3)-min(S1, S2, S3):
        m=max(S1, S2, S3)-min(S1, S2, S3)
        print(S1,S2,S3)
print(m)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H < W:
  H, W = W, H
if H % 3 == 0 or W % 3 == 0:
  print(0)
else:
  h = -(-H // 3)
  w = W // 2
  s1 = [h * W, (H - h) * w, (H - h) * (W - w)]
  d1 = max(s1) - min(s1)
  s2 = [(h - 1) * W, (H - h + 1) * w, (H - h + 1) * (W - w)]
  d2 = max(s2) - min(s2)
  h = H // 2
  w = -(-W // 3)
  s3 = [H * w, h * (W - w), (H - h) * (W - w)]
  d3 = max(s3) - min(s3)
  s4 = [H * (w - 1), h * (W - w + 1), (H - h) * (W - w + 1)]
  d4 = max(s4) - min(s4)
  print(min([d1, d2, d3, d4]))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H < W:
  H, W = W, H
if H % 3 == 0 or W % 3 == 0:
  print(0)
elif W % 2 == 0:
  h = -(-H // 3)
  d1 = abs(h * W - (H - h) * W // 2)
  d2 = abs((h - 1) * W - (H - h + 1) * W // 2)
  print(min(d1, d2))
elif H % 2 == 0:
  w = -(-W // 3)
  d1 = abs(H * w - H * (W - w) // 2)
  d2 = abs(H * (w - 1) - H * (W - w + 1) // 2)
  print(min(d1, d2))
else:
  h = -(-H // 3)
  w = W // 2
  s1 = [h * W, (H - h) * w, (H - h) * (w + 1)]
  d1 = max(s1) - min(s1)
  s2 = [(h - 1) * W, (H - h + 1) * w, (H - h + 1) * (w + 1)]
  d2 = max(s2) - min(s2)
  print(min(d1, d2))

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD


# --------------------------------------------

dp = None


def main():
    H, W = li_input()

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

    ###

    if H % 3 == 2 and W % 3 == 2:
        x = H // 3
        print(3 * x + 1)
        return

    ###

    if H % 2 == 1 and W % 2 == 1:
        print(min(H, W))
        return

    ###

    ans = 10**150

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

            ans = min(ans, tmpmax - tmpmin)

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

            ans = min(ans, tmpmax - tmpmin)

    large, small = max(H, W), min(H, W)

    if large % 3 == 2:
        ans = min(ans, small)

    print(ans)


main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD


# --------------------------------------------

dp = None


def main():
    H, W = li_input()

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

    if H % 3 == 2 and W % 3 == 2:
        x = H // 3
        print(3 * x + 1)
        return

    if H % 2 == 1 and W % 2 == 1:
        print(min(H, W))
        return

    if H % 2 == 0:
        even, odd = H, W
    else:
        even, odd = W, H

    ans = 10**150

    for bottom in range(1, odd + 1):
        ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))

    even, odd = odd, even

    for bottom in range(1, odd + 1):
        if even % 2 == 0:
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))
        else:
            tmpmax = max(bottom * even, (odd - bottom) * (even // 2),
                         (odd - bottom) * (even // 2 + 1))
            tmpmin = min(bottom * even, (odd - bottom) * (even // 2),
                         (odd - bottom) * (even // 2 + 1))

            ans = min(ans, tmpmax - tmpmin)

    print(ans)


main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD


# --------------------------------------------

dp = None


def main():
    H, W = li_input()

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

    if H % 3 == 2 and W % 3 == 2:
        x = H // 3
        print(3 * x + 1)
        return

    if H % 2 == 1 and W % 2 == 1:
        print(min(H, W))
        return

    if H % 2 == 0:
        even, odd = H, W
    else:
        even, odd = W, H

    if odd % 2 == 1:
        ans = 10**150

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))
    else:
        ans = 10**150

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))

        even, odd = odd, even

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))

    print(ans)


main()