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:
h, w = map(int, input().split())
h, w = min(h, w), max(h, w)
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    a = w // 3
    s1 = a * h
    b = h // 2
    s3 = (w - a) * b
    s2 = h * w - s3 - s1
    ans1 = max(s1, s2, s3) - min(s1, s2, s3)
    a = w // 3 + 1
    s1 = a * h
    b = h // 2
    s3 = (w - a) * b
    s2 = h * w - s3 - s1
    ans2 = max(s1, s2, s3) - min(s1, s2, s3)
    a = w // 3 + 1
    s1 = a * h
    b = h // 2 + 1
    s3 = (w - a) * b
    s2 = h * w - s3 - s1
    ans3 = max(s1, s2, s3) - min(s1, s2, s3)
    a = w // 3
    s1 = a * h
    b = h // 2 + 1
    s3 = (w - a) * b
    s2 = h * w - s3 - s1
    ans4 = max(s1, s2, s3) - min(s1, s2, s3)
    print(min(ans1, ans2, ans3, ans4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
h, w = min(h, w), max(h, w)
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    a = w // 3
    s1 = a * h
    b = h // 2
    s3 = (w - a) * b
    s2 = h * w - s3 - s1
    ans1 = max(s1, s2, s3) - min(s1, s2, s3)
    a = w // 3 + 1
    s1 = a * h
    b = h // 2
    s3 = (w - a) * b
    s2 = h * w - s3 - s1
    ans2 = max(s1, s2, s3) - min(s1, s2, s3)
    print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline

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)))

    # 横にスライス + 残りに縦にスライス
    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)))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
import math

def main():
    H, W = map(int, readline().rstrip().split())
    ans = H * W
    # 横にスライスする場合
    if H >= 3:
        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)))
    
    # 縦にスライス + 残りに横にスライス
    y1 = H
    y2 = H//2
    y3 = H - H//2
    x3 = math.floor((H*W) / (H+y3))
    x1 = W - x3
    x2 = x3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    x3 = math.ceil((H*W) / (H+y3))
    x1 = W - x3
    x2 = x3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    # 横にスライス + 残りに縦にスライス
    x1 = W
    x2 = W//2
    x3 = W - W//2
    y3 = math.floor((H*W) / (W+x3))
    y1 = H - y3
    y2 = y3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    y3 = math.ceil((H*W) / (W+x3))
    y1 = H - y3
    y2 = y3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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:
        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)))
    
    # 縦にスライス + 残りに横にスライス
    y1 = H
    y2 = H//2
    y3 = H - H//2
    x3 = int(round((H*W) / (H+y3)))
    x1 = W - x3
    x2 = x3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    # 横にスライス + 残りに縦にスライス
    x1 = W
    x2 = W//2
    x3 = W - W//2
    y3 = int(round((H*W) / (W+x3)))
    y1 = H - y3
    y2 = y3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

SH1=sorted([(H//3)*W,(H-(H//3))*(W//2),(H-(H//3))*(W-W//2)])

SH2=sorted([(H//3+1)*W,(H-(H//3+1))*(W//2),(H-(H//3+1))*(W-W//2)])


SW1=sorted([(W//3)*H,(W-(W//3))*(H//2),(W-(W//3))*(H-H//2)])
SW2=sorted([(W//3+1)*H,(W-(W//3+1))*(H//2),(W-(W//3+1))*(H-H//2)])

ans1=SH1[2]-SH1[0]
ans2=SH2[2]-SH2[0]
ans3=SW1[2]-SW1[0]
ans4=SW2[2]-SW2[0]

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

Submission status: Wrong Answer

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

SH1=sorted([(H//3)*W,(H-(H//3))*(W//2),(H-(H//3))*(W-W//2)])

SH2=sorted([(H//3+1)*W,(H-(H//3+1))*(W//2),(H-(H//3+1))*(W-W//2)])


SW1=sorted([(W//3)*H,(W-(W//3))*(H//2),(W-(W//3))*(H-H//2)])
SW2=sorted([(W//3+1)*H,(W-(W//3+1))*(H//2),(W-(W//3+1))*(H-H//2)])

ans1=SH1[2]-SH1[0]
ans2=SH2[2]-SH2[0]
ans3=SW1[2]-SW1[0]
ans4=SW2[2]-SW2[0]

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

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h%3 == 0 or w%3 == 0:
    print(0)
elif h%2 == 0 and w%2 == 0:
    mx = max(h,w)
    mn = min(h,w)
    ans = float('inf')
    for i in range(1,mx//2+1):
        smax = mn*i
        smin = (mx-i)*mn//2
        ans = min(ans, abs(smax-smin))
    print(ans)
elif h%2 == 0:
    ans = float('inf')
    for i in range(1,w//2+1):
        smax = i*h
        smin = (w-i)*(h//2)
        ans = min(ans, abs(smax - smin))
    print(ans)
elif w%2 == 0:
    ans = float('inf')
    for i in range(1,h//2+1):
        smax = i*w
        smin = (h - i)*(w//2)
        ans = min(ans, abs(smax - smin))
    print(ans)
else:
    ans = float('inf')
    if h > w:
        h,w = w,h  
    for i in range(1,w//2+1):
        s1 = i*h
        s2 = (w-i)*(h//2+1)
        s3 = (w-i)*(h//2)
        smax = max(s1,s2,s3)
        smin = min(s1,s2,s3)
        ans = min(ans, abs(smax-smin))
    print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h%3 == 0 or w%3 == 0:
    print(0)
elif h%2 == 0 and w%2 == 0:
    mx = max(h,w)
    mn = min(h,w)
    ans = float('inf')
    for i in range(1,mx//2+1):
        smax = mn*i
        smin = (mx-i)*mn//2
        ans = min(ans, abs(smax-smin))
    print(ans)
elif h%2 == 0:
    ans = float('inf')
    for i in range(1,w//2+1):
        smax = i*h
        smin = (w-i)*(h//2)
        ans = min(ans, abs(smax - smin))
    print(ans)
elif w%2 == 0:
    ans = float('inf')
    for i in range(1,h//2+1):
        smax = i*w
        smin = (h - i)*(w//2)
        ans = min(ans, abs(smax - smin))
    print(ans)
else:
    ans = float('inf')
    for i in range(1,w//2+1):
        s1 = i*h
        s2 = (w-i)*(h//2+1)
        s3 = (w-i)*(h//2)
        smax = max(s1,s2,s3)
        smin = min(s1,s2,s3)
        ans = min(ans, abs(smax-smin))
    print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h%3 == 0 or w%3 == 0:
    print(0)
elif h%2 == 0:
    ans = float('inf')
    for i in range(1,w//2+1):
        smax = i*h
        smin = (w-i)*(h//2)
        ans = min(ans, abs(smax - smin))
    print(ans)
elif w%2 == 0:
    ans = float('inf')
    for i in range(1,h//2+1):
        smax = i*w
        smin = (h - i)*(w//2)
        ans = min(ans, abs(smax - smin))
    print(ans)
else:
    ans = float('inf')
    for i in range(1,w//2+1):
        s1 = i*h
        s2 = (w-i)*(h//2+1)
        s3 = (w-i)*(h//2)
        smax = max(s1,s2,s3)
        smin = min(s1,s2,s3)
        ans = min(ans, abs(smax-smin))
    print(ans)

Submission status: Wrong Answer

Solution code:
def calc(w, h):
	v = float('inf')
	a = w * h
	for i in range(h-1):
		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()

Submission status: Wrong Answer

Solution code:
def calc(w, h):
	v = float('inf')
	a = w * h
	for i in range(1, h-1):
		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()

Submission status: Wrong Answer

Solution code:
def min_value(w, h):
	ans = float('inf')
	for i in range(1, h-1):
		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()

Submission status: Wrong Answer

Solution code:
def low(w, h):
	a = w//2 * h
	b = h//2 * w
	return max(a, b)

def resolve():
	h, w = map(int, input().split())
	a = abs((w//2 * h) - low((w - w//2), h))
	b = abs((h//2 * w) - low(w, h - h//2))
	print(min(a, b))
resolve()

Submission status: Wrong Answer

Solution code:
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')
    #h == Tの上
    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))
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
elif h%2==0 or w%2==0:
    mini=h*w
    for i in range(1,w):
        if max(i*h//2,(w-i)*h) - min(i*h//2,(w-i)*h) < mini:
            mini = max(i*h//2,(w-i)*h) - min(i*h//2,(w-i)*h)
    for i in range(1,h):
        if max(i*w//2,(h-i)*w) - min(i*w//2,(h-i)*w) < mini:
            mini = max(i*w//2,(h-i)*w) - min(i*w//2,(h-i)*w)
    if h < mini:
        mini = h
    if w < mini:
        mini = w
    print(mini)
else:
    mini=h*w
    for i in range(1,w):
        if max(i*(h+1)//2,h*(w-i)) - min(i*(h-1)//2,h*(w-i)) < mini:
            mini = max(i*(h+1)//2,h*(w-i)) - min(i*(h-1)//2,h*(w-i))
    for i in range(1,h):
        if max(i*(w+1)//2,w*(h-i)) - min(i*(w-1)//2,w*(h-i)) < mini:
            mini = max(i*(w+1)//2,w*(h-i)) - min(i*(w-1)//2,w*(h-i))
    if h < mini:
        mini = h
    if w < mini:
        mini = w
    print(mini)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
elif h%2==0:
    mini=h*w
    for i in range(1,w):
        if max(i*h//2,(w-i)*h) - min(i*h//2,(w-i)*h) < mini:
            mini = max(i*h//2,(w-i)*h) - min(i*h//2,(w-i)*h)
    print(mini)
elif w%2==0:
    mini=h*w
    for i in range(1,h):
        if max(i*w//2,(h-i)*w) - min(i*w//2,(h-i)*w) < mini:
            mini = max(i*w//2,(h-i)*w) - min(i*w//2,(h-i)*w)
    if h < mini:
        mini = h
    if w < mini:
        mini = w
    print(mini)
else:
    mini=h*w
    for i in range(1,w):
        if max(i*(h+1)//2,h*(w-i)) - min(i*(h-1)//2,h*(w-i)) < mini:
            mini = max(i*(h+1)//2,h*(w-i)) - min(i*(h-1)//2,h*(w-i))
    for i in range(1,h):
        if max(i*(w+1)//2,w*(h-i)) - min(i*(w-1)//2,w*(h-i)) < mini:
            mini = max(i*(w+1)//2,w*(h-i)) - min(i*(w-1)//2,w*(h-i))
    if h < mini:
        mini = h
    if w < mini:
        mini = w
    print(mini)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
elif h%2==0:
    mini=h*w
    for i in range(1,w):
        if max(i*h//2,(w-i)*h) - min(i*h//2,(w-i)*h) < mini:
            mini = max(i*h//2,(w-i)*h) - min(i*h//2,(w-i)*h)
    print(mini)
elif w%2==0:
    mini=h*w
    for i in range(1,h):
        if max(i*w//2,(h-i)*w) - min(i*w//2,(h-i)*w) < mini:
            mini = max(i*w//2,(h-i)*w) - min(i*w//2,(h-i)*w)
    print(mini)
else:
    mini=h*w
    for i in range(1,w):
        if max(i*(h+1)//2,h*(w-i)) - min(i*(h-1)//2,h*(w-i)) < mini:
            mini = max(i*(h+1)//2,h*(w-i)) - min(i*(h-1)//2,h*(w-i))
    for i in range(1,h):
        if max(i*(w+1)//2,w*(h-i)) - min(i*(w-1)//2,w*(h-i)) < mini:
            mini = max(i*(w+1)//2,w*(h-i)) - min(i*(w-1)//2,w*(h-i))        
    print(mini)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    H, W = (int(_) for _ in input().split())
    INF = 10 ** 11
    if H%3 == 0 or W%3 == 0:
        print(0)
        return
    if (H * W) % 2 == 0:
        if H%2 != 0 or W%2 != 0:
            if H%2 != 0:
                H, W = W, H
            ret = INF
            for w in range(1, W):
                ret = min(ret, abs(H * (W - w) - w * (H//2)))
            print(ret)
            return
        else:
            ret = INF
            for w in range(1, W):
                ret = min(ret, abs(H * (W - w) - w * (H//2)))
            H, W = W, H
            for w in range(1, W):
                ret = min(ret, abs(H * (W - w) - w * (H//2)))
            print(ret)
            return
    else:
        w1 = W//2
        w2 = W - w1
        ret = INF
        for h in range(1, H):
            ret = min(max(h * w1, h * w2, W * (H - h)) - min(h * w1, h * w2, W * (H - h)), ret)
        H, W = W, H
        w1 = W//2
        w2 = W - w1
        for h in range(1, H):
            ret = min(max(h * w1, h * w2, W * (H - h)) - min(h * w1, h * w2, W * (H - h)), ret)
        print(ret)
        return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    H, W = (int(_) for _ in input().split())
    INF = 10 ** 11
    if H%3 == 0 or W%3 == 0:
        print(0)
        return
    if (H * W) % 2 == 0:
        if H%2 != 0 or W%2 != 0:
            if H%2 != 0:
                H, W = W, H
            ret = INF
            for w in range(1, W):
                ret = min(ret, abs(H * (W - w) - w * (H//2)))
            print(ret)
            return
        else:
            ret = INF
            for w in range(1, W):
                ret = min(ret, abs(H * (W - w) - w * (H//2)))
            H, W = W, H
            for w in range(1, W):
                ret = min(ret, abs(H * (W - w) - w * (H//2)))
            print(ret)
            return
    else:
        w1 = W//2
        w2 = W - w1
        ret = INF
        for h in range(1, H):
            ret = min(max(h * w1, h * w2, W * (H - h)) - min(h * w1, h * w2, W * (H - h)), ret)
        H, W = W, H
        w1 = W//2
        w2 = W - w1
        ret = INF
        for h in range(1, H):
            ret = min(max(h * w1, h * w2, W * (H - h)) - min(h * w1, h * w2, W * (H - h)), ret)
        print(ret)
        return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = float("inf")

for i in range(1, H):
    #areaAが上
    a = i * W  #areaA h = i 
    h = H - i  #remaining h = H-i

    #下を横に分割
    bH = h // 2
    b = bH * W
    cH = h - bH
    c = cH * W
    temp = max(a, b, c) - min(a, b, c)
    if ans > temp:
        ans = temp

    #下を縦に分割
    bW = W // 2
    b = h * bW
    cW = W - bW
    c = h * cW
    temp = max(a, b, c) - min(a, b, c)
    if ans > temp:
        ans = temp

    #areaAが左
    a = H * i  #areaA w = i 
    w = W - i  #remaining w = W-i

    #右を縦に分割
    bW = w // 2
    b = bW * h
    cW = w - bW
    c = cW * H
    temp = max(a, b, c) - min(a, b, c)
    if ans > temp:
        ans = temp

    #右を横に分割
    bH = H // 2
    b = bH * w
    cH = H - bH
    c = cH * w
    temp = max(a, b, c) - min(a, b, c)
    if ans > temp:
        ans = temp

    if ans <= 0:
        break

print(ans)

Submission status: Wrong Answer

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

if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    area1 = min(h, w) // 2
    area2 = (max(h, w) - 1) * min(h, w)
    area3 = -(-min(h, w) // 2)
    ans1 = max(area1, area2, area3) - min(area1, area2, area3)
    #print(area1, area2, area3)
    for i in range(1, max(h, w) - 1):
        area1 += min(h, w) // 2
        area2 -= min(h, w)
        area3 += -(-min(h, w) // 2)
        #print(area1, area2, area3)
        ans1 = min(ans1, max(area1, area2, area3) - min(area1, area2, area3))
    ans2 = h * w
    #print("ans2")
    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))
        #print(area1, area2, area3)
        ans2 = min(ans2, max(area1, area2, area3) - min(area1, area2, area3))
    print(min(ans1, ans2))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
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))
    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))
    d = (H*(W//3),(H*W-H*(W//3))//2,H*W-H*(W//3)-(H*W-H*(W//3))//2)
    e = (W*(H//3),(H*W-W*(H//3))//2,H*W-W*(H//3)-(H*W-W*(H//3))//2)
    ans = min(ans,max(d)-min(d))
    ans = min(ans,max(e)-min(e))
    print(ans)

Submission status: Wrong Answer

Solution code:
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))
    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))
    d = (H*(W//3),H*W-H*(W//3)*2)
    e = (W*(H//3),H*W-W*(H//3)*2)
    ans = min(ans,max(d)-min(d))
    ans = min(ans,max(e)-min(e))
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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)
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))
    for i in range(1,H):
        c = (W*i,W//2*(H-i),H*W-W*i-H//2*(W-i))
        ans = min(ans,max(c)-min(c))
    print(ans)

Submission status: Wrong Answer

Solution code:
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-1, w//3+3):
    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-1, h//3 + 3):
    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))

Submission status: Wrong Answer

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

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:
    if h%3==1:
        a=(h//3)*w
        b=(h-(h//3))*(w//2)
        s=h*w
        c=s-a-b
        ma=max(a,b,c)
        mi=min(a,b,c)
        print(ma-mi)
    elif h%3==2:
        a=((h//3)+1)*w
        b=(h-((h//3)+1))*(w//2)
        s=h*w
        c=s-b-a
        ma=max(a,b,c)
        mi=min(a,b,c)
        print(ma-mi)

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:
    if h%3==1:
        a=(h//3)*w
        b=(h-(h//3))*(w//2)
        print(abs(a-b))
    elif h%3==2:
        a=((h//3)+1)*w
        b=(h-((h//3)+1))*(w//2)
        print(abs(a-b))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
ans=1000000007
for i in range(1,H-1):
  sm=sx=i*W
  for j in range(i+1,H):
    s1=(j-i)*W
    s2=(H-j)*W
    sm=min(sm,min(s1,s2))
    sx=max(sx,max(s1,s2))
    ans=min(ans,sx-sm)
for i in range(1,W-1):
  sm=sx=i*H
  for j in range(i+1,W):
    s1=(j-i)*H
    s2=(W-j)*H
    sm=min(sm,min(s1,s2))
    sx=max(sx,max(s1,s2))
    ans=min(ans,sx-sm)
for i in range(1,H):
  sm1=sx1=i*W
  sm2=sx2=(H-i)*W
  for j in range(1,W):
    s11=(H-i)*j
    s12=(H-i)*(W-j)
    s21=i*j
    s22=i*(W-j)
    sm1=min(sm1,min(s11,s12))
    sx1=max(sx1,max(s11,s12))
    sm2=min(sm2,min(s21,s22))
    sx2=max(sx2,max(s21,s22))
    ans=min(ans,min(sx1-sm1,sx2-sm2))
for i in range(1,W):
  sm1=sx1=i*H
  sm2=sx2=(W-i)*H
  for j in range(1,H):
    s11=(W-i)*j
    s12=(W-i)*(H-j)
    s21=i*j
    s22=i*(H-j)
    sm1=min(sm1,min(s11,s12))
    sx1=max(sx1,max(s11,s12))
    sm2=min(sm2,min(s21,s22))
    sx2=max(sx2,max(s21,s22))
    ans=min(ans,min(sx1-sm1,sx2-sm2))
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
long = max(h, w)
short = min(h, w)
if h%3==0 or w%3==0:
    print(0)
    exit(0)
else:
    if long%3==1:
        s1 = long//3*short
        s2 = (long-long//3)*(short//2)
    elif long%3==2:
        s1 = (long//3+1)*short
        s2 = (long-(long//3+1))*(short//2)
s3 = h*w - s1 - s2
print(max(s1,s2,s3)-min(s1,s2,s3))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
long = max(h, w)
short = min(h, w)
if h%3==0 or w%3==0:
    print(0)
    exit(0)
else:
    if long%3==1:
        s_max = long//3*short
        s_min = (long-long//3)*(short//2)
    elif long%3==2:
        s_max = (long//3+1)*short
        s_min = (long-(long//3+1))*(short//2)
print(abs(s_max-s_min))

Submission status: Wrong Answer

Solution code:
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
  ans2 = ((max(h,w)//3 + 1)*min(h,w))-((max(h,w)//3)*min(h,w))
  print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
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
  ans2 = ((max(h,w)//3 + 1)*min(h,w))-((max(h,w)//3)*min(h,w))
  ans3 = ((min(h,w)//3 + 1)*max(h,w))-((min(h,w)//3)*max(h,w))
  print(min(ans,ans2,ans3))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
block = H*W
if block%3 == 0:
  print(0)
else: 
  if H*(W//2) < W*(H//2):
    max_bl = H*(W//2)
    W -= (W//2)
  else:
    max_bl = W*(H//2)
    H -= (H//2)
  block -= max_bl
  print(max_bl)
  min_bl = max(H*(W//2),W*(H//2))
  print(min_bl)
  print(max_bl-min_bl)

Submission status: Wrong Answer

Solution code:
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
    if min(h,w)%2 != 0:
      c = (max(h,w)-i)*(min(h,w)//2 + 1)
    else:
      c = (max(h,w)-i)*(min(h,w)//2)
    kari = max(a,c)-min(a,c)
    if ans > kari:
      ans = kari
  ans2 = ((max(h,w)//3 + 1)*min(h,w))-((max(h,w)//3)*min(h,w))
  print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
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):
    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
  ans2 = ((max(h,w)//3 + 1)*min(h,w))-((max(h,w)//3)*min(h,w))
  print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
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)):
    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
  ans2 = ((max(h,w)//3 + 1)*min(h,w))-((max(h,w)//3)*min(h,w))
  print(min(ans,ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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