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

if H%3 == 0 or W % 3 == 0:
    print(0)
else:
    ans = 10 ** 11
    for i in range(H):
        if i == 0:
            j = (H//3)
            k = (H-j)//2
            s1 = W * j
            s2 = W * k
            s3 = W * (H-j-k)
            M = max(s1,s2,s3) - min(s1,s2,s3)
            ans = min(ans,M)
            continue
        s1 = i * H
        j = H //2
        s2 = (W-i) * j
        s3 = (W-i) * (H-j)
        M = max(s1,s2,s3) - min(s1,s2,s3)
        ans = min(ans,M)
        
    tmp = W
    W = H
    H = tmp
    
    for i in range(H):
        if i == 0:
            j = (H//3)
            k = (H-j)//2
            s1 = W * j
            s2 = W * k
            s3 = W * (H-j-k)
            M = max(s1,s2,s3) - min(s1,s2,s3)
            ans = min(ans,M)
            continue
        s1 = i * H
        j = H //2
        s2 = (W-i) * j
        s3 = (W-i) * (H-j)
        M = max(s1,s2,s3) - min(s1,s2,s3)
        ans = min(ans,M)
        
    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:
    ans = 10 ** 11
    for i in range(H):
        if i == 0:
            j = (H//3)
            k = (H-j)//2
            s1 = W * j
            s2 = W * k
            s3 = W * (H-j-k)
            M = max(s1,s2,s3) - min(s1,s2,s3)
            ans = min(ans,M)
            continue
        s1 = i * W
        j = H //2
        s2 = (W-i) * j
        s3 = (W-i) * (H-j)
        M = max(s1,s2,s3) - min(s1,s2,s3)
        ans = min(ans,M)
        
    tmp = W
    W = H
    H = tmp
    
    for i in range(H):
        if i == 0:
            j = (H//3)
            k = (H-j)//2
            s1 = W * j
            s2 = W * k
            s3 = W * (H-j-k)
            M = max(s1,s2,s3) - min(s1,s2,s3)
            ans = min(ans,M)
            continue
        s1 = i * W
        j = H //2
        s2 = (W-i) * j
        s3 = (W-i) * (H-j)
        M = max(s1,s2,s3) - min(s1,s2,s3)
        ans = min(ans,M)
        
    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:
    ans = 10 ** 11
    for i in range(H):
        s1 = i * W
        j = H //2
        s2 = (W-i) * j
        s3 = (W-i) * (H-j)
        M = max(s1,s2,s3) - min(s1,s2,s3)
        ans = min(ans,M)
        
    tmp = W
    W = H
    H = tmp
    
    for i in range(H):
        s1 = i * W
        j = H //2
        s2 = (W-i) * j
        s3 = (W-i) * (H-j)
        M = max(s1,s2,s3) - min(s1,s2,s3)
        ans = min(ans,M)
        
    print(ans)

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    print(0)
else:
    ans = [0, 0]
    for i in range(2):
        H_left = H - (H // 3 + i)
        first = (H // 3 + i) * W
        if (H_left * W) % 2 == 0:
            second = third = H_left * W / 2
        else:
            second = (H_left * W) - (max(H_left, W) // 2) * min(H_left, W)
            third = (H_left * W) - second
        ans[i] = max(first, second, third) - min(first, second, third)
    print(int(min(ans)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def f(h,w):
    if h%3==0:
        return 0
    rt=h*w
    for i in range(1,h):
        rt=min(rt,max(i*w,(h-i)*(w//2),(h-i)*(w-(w//2))) - min(i*w,(h-i)*(w//2),(h-i)*(w-(w//2))))
        rt=min(rt,max(i*w,(h//2)*w,(h-(h//2))*w) - min(i*w,(h//2)*w,(h-(h//2))*w))
    return rt

print(min(f(h,w),f(w,h)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def f(h,w):
    if h%3==0:
        return 0
    rt=h*w
    for i in range(1,h):
        rt=min(rt,max(i*w,(h-i)*(w//2),(h-i)*(w-(w//2))) - min(i*w,(h-i)*(w//2),(h-i)*(w-(w//2))))
        rt=min(rt,max(i*w,(h//2)*w,(h-h//2)*w) - min(i*w,(h//2)*w,(h-h//2)*w))
    return rt

print(min(f(h,w),f(w,h)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def f(h,w):
    if h%3==0:
        return 0
    rt=h*w
    for i in range(1,h):
        rt=min(rt,max(i*w,(h-i)*(w//2),(h-i)*(w-(w//2))) - min(i*w,(h-i)*(w//2),(h-i)*(w-(w//2))))
    return rt

print(min(f(h,w),f(w,h)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def f(h,w):
    if h%3==0:
        return 0
    rt=h*w
    for i in range(1,h):
        rt=min(rt,max(i*w,(h-i)*(w//2),(h-i)*(w-w//2)) - min(i*w,(h-i)*(w//2),(h-i)*(w-w//2)))
    return rt

print(min(f(h,w),f(w,h)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def f(h,w):
    rt=h*w
    for i in range(1,h):
        rt=min(rt,max(i*w,(h-i)*(w//2),(h-i)*(w-w//2)) - min(i*w,(h-i)*(w//2),(h-i)*(w-w//2)))
    return rt

print(min(f(h,w),f(w,h)))

Submission status: Wrong Answer

Solution code:
from math import floor, ceil
from itertools import chain

# 入力
H, W = map(int, input().split())

# ある1ピースを i*W または j*W となるように切ったときのS_max - S_minをすべて求める
ans = min(
    max(a, b, c) - min(a, b, c)
    for a, b, c in chain(
        (
            (i * W, (H - i) * floor(W / 2), (H - i) * ceil(W / 2))
            for i in range(1, H)
        ),
        (
            (i * W, floor((H - i) / 2) * W, ceil((H - i) / 2) * W)
            for i in range(1, H)
        ),
        (
            (j * H, (W - j) * floor(H / 2), (W - j) * ceil(H / 2))
            for j in range(1, W)
        ),
        (
            (j * W, floor((W - j) / 2) * H, ceil((W - j) / 2) * H)
            for j in range(1, W)
        ),
    )
)

# 出力
print(ans)

Submission status: Wrong Answer

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


for i in range(2):
    if i == 0:
        ver, hori = H, W
    else:
        ver, hori = W, H
    for ha in range(1, ver // 2):
        sa = ha * hori
        h = ver - ha
        
        # Case1 横二本の区切り線
        if h % 2 == 0:
            hb, hc = h // 2, h // 2
        else:
            hb, hc = h // 2, h // 2 + 1
        sb1, sc1 = hb * hori, hc * hori
        diff1 = max(sa, sb1, sc1) - min(sa, sb1, sc1)
        
        # Case2 横一本、縦一本区切り線
        if hori % 2 == 0:
            wb, wc = hori // 2, hori // 2
        else:
            wb, wc = hori // 2, hori // 2 + 1
        sb2, sc2 = h * wb, h * wc
        diff2 = max(sa, sb2, sc2) - min(sa, sb2, sc2)
        
        tmp_min =  min(diff1, diff2)
        ans = min(ans, tmp_min)


print(ans)

Submission status: Wrong Answer

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

S = H * W
diff = 10 ** 12
for i in range(1, H-1):
    S_up = i * W
    S_under = W * ((H - i) // 2)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W-1):
    S_up = i * H
    S_under = (W - i) // 2
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, H+1):
    S_up = i * W
    S_under = (W // 2) * (H - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W+1):
    S_up = i * H
    S_under = (H // 2) * (W - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

print(diff)

Submission status: Wrong Answer

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

S = H * W
diff = 10 ** 12
for i in range(H):
    S_up = i * W
    S_under = W * ((H - i) // 2)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(W):
    S_up = i * H
    S_under = (W - i) // 2
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(H):
    S_up = i * W
    S_under = (W // 2) * (H - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(W):
    S_up = i * H
    S_under = (H // 2) * (W - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

print(diff)

Submission status: Wrong Answer

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

S = H * W
diff = 10 ** 12
for i in range(1, H):
    S_up = i * W
    S_under = W * ((H - i) // 2)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W):
    S_up = i * H
    S_under = (W - i) // 2
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, H):
    S_up = i * W
    S_under = (W // 2) * (H - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W):
    S_up = i * H
    S_under = (H // 2) * (W - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

print(diff)

Submission status: Wrong Answer

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

S = H * W
diff = 10 ** 12
for i in range(1, H-1):
    S_up = i * W
    S_under = W * ((H - i) // 2)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W-1):
    S_up = i * H
    S_under = (W - i) // 2
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, H):
    S_up = i * W
    S_under = (W // 2) * (H - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W):
    S_up = i * H
    S_under = (H // 2) * (W - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

print(diff)

Submission status: Wrong Answer

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

S = H * W
diff = 10 ** 12
for i in range(1, H-1):
    S_up = i * W
    S_under = W * ((H - i) // 2)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W-1):
    S_up = i * H
    S_under = (W - i) // 2
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, H-1):
    S_up = i * W
    S_under = (W // 2) * (H - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

for i in range(1, W-1):
    S_up = i * H
    S_under = (H // 2) * (W - i)
    S_middle = S - S_up - S_under
    S_max = max(S_up, S_under, S_middle)
    S_min = min(S_up, S_under, S_middle)
    diff = min(diff, abs(S_max - S_min))

print(diff)

Submission status: Wrong Answer

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

if H == 1:
    print(W % 3)
elif W == 1:
    print(H % 3)
else:
    if (H * W) % 3 == 0:
        print(0)
    else:
        ans = 9999999999
        # pattern1
        for i in range(1, H - 1):
            l = [W*(H-i), i*(int(W/2)), i*(W-int(W/2))]
            l.sort()
            ans = min(ans, l[-1] - l[0])

        # pattern2
        for i in range(1, W - 1):
            l = [H * (W - i), i * (int(H / 2)), i * (H - int(H / 2))]
            l.sort()
            ans = min(ans, l[-1] - l[0])

        # pattern3
        ans = min(ans, H)

        # pattern4
        ans = min(ans, W)

        print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = H * W
longer = max(H, W)
shorter = min(H,W)


if S % 3 == 0:
    ans = 0
else:
    if longer % 2 == 0:
        longer_side_harf = longer // 2
    else:
        longer_side_harf = longer // 2 + 1
    if shorter % 2 == 0:
        shorter_side_harf = longer // 2
    else:
        shorter_side_harf = longer // 2 + 1
    s1 = (longer - longer_side_harf) * shorter
    s2 = longer_side_harf * shorter_side_harf
    s3 = longer_side_harf * (shorter - shorter_side_harf)
    ans = max(s1, s2, s3) - min(s1, s2, s3)


print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
print((h*w)%3)

Submission status: Wrong Answer

Solution code:
def f(a):
	q=a[:n];s=sum(q);heapify(q);yield s
	for e in a[n:2*n]:s+=e-heappushpop(q,e);yield s
from heapq import*;n,*a=map(int,open(0).read().split());print(max(map(sum,zip(f(a),list(f([-e for e in a[::-1]]))[::-1]))))

Submission status: Wrong Answer

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

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


def c(H, W):
    r = 1000000000000000000000000000000
    for y in range(1, H):
        l = [W * y, (W // 2) * (H - y), (W - W // 2) * (H - y)]
        r = min(r, max(l) - min(l))
    return r


print(min(c(h, w), c(w, h)), h, w)

Submission status: Wrong Answer

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

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



def c(a, b):
    r=1000000000000000000000000000000
    for i in range(1,a):
        l=[b*i,(b//2)*(a-i),(b-b//2)*(a-i)]
        r=min(r,max(l)-min(l))
    return r

print(min(c(h,w),c(w,h)))

Submission status: Wrong Answer

Solution code:
#ABC074C
H,W = map(int, input().split())
ans=100000000000
if H%3==0 or W%3==0:
    print(0)
else:
    if H>=4:
        a=H//3
        b=H//3+1
        c=H-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)
    if W>=4:
        a=W//3
        b=W//3+1
        c=W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)

    for i in range(1,W+1):
        a=i*H
        b=(W-i)*(H//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)

    for i in range(1,H+1): 
        a=i*W
        b=(H-i)*(W//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
#ABC074C
H,W = map(int, input().split())
if H%3==0 or W%3==0:
    print(0)
else:
    ans = 100000000000
    a=0
    b=0
    c=0
    abc=[]
    for i in range(1,W+1):
        a=i*H
        b=(W-i)*(H//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)

    for i in range(1,H+1): 
        a=i*W
        b=(H-i)*(W//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
#ABC074C
H,W = map(int, input().split())
if H%3==0 or W%3==0:
    print(0)
else:
    ans = 100000000000
    a=0
    b=0
    c=0
    abc=[]
    for i in range(1,W+1):
        a=i*H
        b=(W-i)*(H//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)
        
        a=i*W
        b=(H-i)*(W//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
#ABC074C
H,W = map(int, input().split())
if H%3==0 or W%3==0:
    print(0)
else:
    ans = 100000000000
    a=0
    b=0
    c=0
    abc=[]
    for i in range(1,W+1):
        a=i*H
        b=(W-i)*(H//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
#ABC074C
H,W = map(int, input().split())
if H%3==0 or W%3==0:
    print(0)
else:
    ans = 100
    a=0
    b=0
    c=0
    abc=[]
    for i in range(1,W+1):
        a=i*H
        b=(W-i)*(H//2) 
        c=H*W-(a+b)
        abc=[a,b,c]
        ans=min(max(abc)-min(abc),ans)
    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:
    longer = max(H,W)
    shorter = min(H,W)
    minimum = (longer - longer//2)*(shorter//2)
    maximum = (longer//2)*shorter
    print(maximum-minimum)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = h * w
if s % 3 == 0:
    ans = 0
else:
    c = []
    for i in range(1, h):
        sa = i * w
        sb = (h - i) * (w // 2)
        sc = (h - i) * (w - (w // 2))
        min_s = min(sa, min(sb, sc))
        max_s = max(sa, max(sb, sc))
        c.append(max_s - min_s)
    for i in range(1, w):
        sa = i * h
        sb = (w - i) * (h // 2)
        sc = (w - i) * (h - (h // 2))
        min_s = min(sa, min(sb, sc))
        max_s = max(sa, max(sb, sc))
        c.append(max_s - min_s)
    ans = min(c)
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = h * w
if (h == 2) & (w == 2):
    ans = 1
if s % 3 == 0:
    ans = 0
else:
    c = []
    for i in range(1, h):
        sa = i * w
        sb = (h - i) * (w // 2)
        sc = (h - i) * (w - (w // 2))
        min_s = min(sa, min(sb, sc))
        max_s = max(sa, max(sb, sc))
        c.append(max_s - min_s)
    for i in range(1, w):
        sa = i * h
        sb = (w - i) * (h // 2)
        sc = (w - i) * (h - (h // 2))
        min_s = min(sa, min(sb, sc))
        max_s = max(sa, max(sb, sc))
        c.append(max_s - min_s)
    ans = min(c)
print(ans)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
if a%3==0 or b%3==0:
  print(0)
else:
  c=a//3+((a/3-a//3)>.5)
  d=a-c
  e=b//3+((b/3-b//3)>.5)
  f=b-e
  g=a//2
  h=a-g
  i=b//2
  j=b-i
  m=[b*c,d*i,d*j]
  n=[a*e,f*g,f*h]
  print(min(max(m)-min(m),max(n)-min(n)))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
if a%3==0 or b%3==0:
  print(0)
else:
  c=a//3+(a%3>1)
  d=a-c
  e=b//3+(b%3>1)
  f=b-e
  g=a//2
  h=a-g
  i=b//2
  j=b-i
  m=[b*c,d*i,d*j]
  n=[a*e,f*g,f*h]
  print(min(max(m)-min(m),max(n)-min(n)))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
if a%3==0 or b%3==0:
  print(0)
else:
  c=a//3
  d=a-c
  e=b//3
  f=b-e
  g=a//2
  h=a-g
  i=b//2
  j=b-i
  m=[b*c,d*i,d*j]
  n=[a*e,f*g,f*h]
  print(min(max(m)-min(m),max(n)-min(n)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=0
M=max(h,w)
m=min(h,w)
a=[m*(M-1)//3,(m+1)*(2*M+1)//6,(m-1)*(2*M+1)//6]
b=[m*(M+1)//3,(m+1)*(2*M-1)//6,(m-1)*(2*M-1)//6]
if h%3==0 or w%3==0:
    ans=0
elif h==w:
    if h%2==0:
        ans=h//2
    elif h%3==1:
        ans=(5*h+1)//6
    elif h%3==2:
        ans=(5*h-1)//6
else:
    if h%2==w%2==0:
        ans=min(h,w)//2
    elif h%2==0 and w%2==1:
        ans=h//2
    elif h%2==1 and w%2==0:
        ans=w//2
    else:
        if M%3==1:
            ans=min(max(a)-min(a),m)
        else:
            ans=min(max(b)-min(b),m)
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=0
M=max(h,w)
m=min(h,w)
a=[m*(M-1)//3,(m+1)*(2*M+1)//6,(m-1)*(2*M+1)//6]
b=[m*(M+1)//3,(m+1)*(2*M-1)//6,(m-1)*(2*M-1)//6]
if h%3==0 or w%3==0:
    ans=0
elif h==w:
    if h%2==0:
        ans=h//2
    elif h%3==1:
        ans=(5*h+1)//6
    elif h%3==2:
        ans=(5*h-1)//6
else:
    if h%2==w%2==0:
        ans=min(h,w)//2
    elif h%2==0 and w%2==1:
        ans=h//2
    elif h%2==1 and w%2==0:
        ans=w//2
    else:
        if M%3==1:
            ans=max(a)-min(a)
        else:
            ans=max(b)-min(b)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

h, w = nm()
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
minn = 10 ** 9
for i in range(1, w):
    if h % 2 == 0:
        minn = min(minn, max(i * h, (w - i) * h // 2)
                - min(i * h, (w - i) * h // 2))
    else:
        minn = min(minn, max(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1))
                - min(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1)))
for i in range(1, h):
    if w % 2 == 0:
        minn = min(minn, max(i * w, (h - i) * w // 2)
                - min(i * w, (h - i) * w // 2))
    else:
        minn = min(minn, max(i * w, (h - i) * (w // 2), (h - i) * (w // 2 + 1))
                - min(i * w, (h - i) * (w // 2), (h - i) * (w // 2 + 1)))
minn = min(minn, (h // 3 + 1) * w - (h // 3) * w)
print(minn)

Submission status: Wrong Answer

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

h, w = nm()
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
h, w = min(h, w), max(h, w)
if w % 2 == 0 and h % 2 != 0:
    h, w = w, h
minn = 10 ** 9
for i in range(1, w):
    if h % 2 == 0:
        minn = min(minn, max(i * h, (w - i) * h // 2)
                - min(i * h, (w - i) * h // 2))
    else:
        minn = min(minn, max(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1))
                - min(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1)))
minn = min(minn, (w // 3 + 1) * h - (w // 3) * h)
print(minn)

Submission status: Wrong Answer

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

h, w = nm()
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
h, w = min(h, w), max(h, w)
if w % 2 == 0 and h % 2 != 0:
    h, w = w, h
print(h, w)
minn = 10 ** 9
for i in range(1, w):
    if h % 2 == 0:
        minn = min(minn, max(i * h, (w - i) * h // 2)
                - min(i * h, (w - i) * h // 2))
    else:
        minn = min(minn, max(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1))
                - min(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1)))
minn = min(minn, (w // 3 + 1) * h - (w // 3) * h)
print(minn)

Submission status: Wrong Answer

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

h, w = nm()
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
h, w = min(h, w), max(h, w)
minn = 10 ** 9
for i in range(1, w):
    if h % 2 == 0:
        minn = min(minn, max(i * h, (w - i) * h // 2)
                - min(i * h, (w - i) * h // 2))
    else:
        minn = min(minn, max(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1))
                - min(i * h, (w - i) * (h // 2), (w - i) * (h // 2 + 1)))
minn = min(minn, (w // 3 + 1) * h - (w // 3) * h)
print(minn)

Submission status: Wrong Answer

Solution code:
a,b = map(int, input().split())
if a%3 == 0 or b%3 == 0:
    print(0)
else:
    #二通り考えられる
    x = a // 2
    tmp = a*b
    for i in range(b):
        s = a * (i+1)
        t = (b-i-1)*x
        u = a*b-s-t
        tmp = min(tmp,max(s,t,u)-min(s,t,u))
    y = b // 2
    tmpa = a*b
    for i in range(a):
        s = b * (i+1)
        t = (a-i-1)*y
        u = a*b-s-t
        tmpa = min(tmpa,max(s,t,u)-min(s,t,u))
    print(min(tmp,tmpa))

Submission status: Wrong Answer

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    H,W = (int(_) for _ in input().split())

    ans = INF
    for h in range(1,H):
        if W%2 == 0:
            a= h*W//2
            b= h*W//2
        else:
            a= h*(W-1)//2
            b= h*(W+1)//2
        c= (H-h)*W
        s_max = max(a,b,c)
        s_min = min(a,b,c)
        ans = min(ans,s_max - s_min)

        a= h*W
        if (H-h)%2 == 0:
            b= (H-h)//2*W
            c= (H-h)//2*W
        else:
            b= (H-h-1)*W
            c= (H-h+1)*W
        s_max = max(a,b,c)
        s_min = min(a,b,c)
        ans = min(ans,s_max - s_min)
        
    for w in range(1,W):
        if H%2 == 0:
            a= w*H//2
            b= w*H//2
        else:
            a= w*(H-1)//2
            b= w*(H+1)//2
        c= (W-w)*H
        s_max = max(a,b,c)
        s_min = min(a,b,c)
        ans = min(ans,s_max - s_min)

        a= w*H
        if (W-w)%2 == 0:
            b= (w-W)//2*H
            c= (w-W)//2*H
        else:
            b= (w-W-1)//2*H
            c= (w-W+1)//2*H
        s_max = max(a,b,c)
        s_min = min(a,b,c)
        ans = min(ans,s_max - s_min)
        
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

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

# yy yt ty tt
# yy
a = math.ceil(H/3) * W
b = (H - math.ceil(H/3)) // 2 * W
c = H * W - a - b
ans = max(a, b, c) - min(a, b, c)

# yt
b = b = (H - math.ceil(H/3)) * (W // 2)
c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

# ty
a = math.ceil(W/3) * H
b = (W - math.ceil(W/3)) // 2 * H
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

# tt
b = (W - math.ceil(W/3)) * (H // 2)
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))


# yy
a = (H//3) * W
b = (H - (H//3)) // 2 * W
c = H * W - a - b
ans = max(a, b, c) - min(a, b, c)

# yt
b = (H - (H//3)) * (W // 2)
c = H * W - a - b
t = max(a, b, c) - min(a, b, c)
ans = min(ans, max(a, b, c) - min(a, b, c))

# ty
a = (W//3) * H
b = (W - (W//3)) // 2 * H
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

# tt
b = (W - (W//3)) * (H // 2)
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

# yy yt ty tt
# yy
a = math.ceil(H/3) * W
b = (H - math.ceil(H/3)) // 2 * W
c = H * W - a - b
ans = max(a, b, c) - min(a, b, c)

# yt
b = b = (H - math.ceil(H/3)) + (W // 2)
c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

# ty
a = math.ceil(W/3) * H
b = (W - math.ceil(W/3)) // 2 * H
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

# tt
b = (W - math.ceil(W/3)) * (H // 2)
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))


# yy
a = (H//3) * W
b = (H - (H//3)) // 2 * W
c = H * W - a - b
ans = max(a, b, c) - min(a, b, c)

# yt
b = b = (H - (H//3)) + (W // 2)
c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

# ty
a = (W//3) * H
b = (W - (W//3)) // 2 * H
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

# tt
b = (W - (W//3)) * (H // 2)
c = c = H * W - a - b
ans = min(ans, max(a, b, c) - min(a, b, c))

print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = 10**9
ans = min(ans,(h//3+h%3)*w-(h//3)*w)
ans = min(ans,(w//3+w%3)*h-(w//3)*h)
for i in range(1,h-1):
    s1 = i*w
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w//2+w%2)
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
for i in range(1,w-1):
    s1 = i*h
    s2 = (w-i)*(h//2)
    s3 = (w-i)*(h//2+h%2)
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = 10**9
ans = min(ans,(h//3+h%3)*w-(h//3)*w)
ans = min(ans,(w//3+w%3)*h-(w//3)*h)
for i in range(1,h//2+1):
    s1 = i*w
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w//2+w%2)
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
for i in range(1,w//2+1):
    s1 = i*h
    s2 = (w-i)*(h//2)
    s3 = (w-i)*(h//2+h%2)
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = 10**9
ans = min(ans,(h//3+h%3)*w-(h//3)*w)
ans = min(ans,(w//3+w%3)*h-(w//3)*h)
for i in range(1,h//2+1):
    s1 = i*w
    s2 = i*(w//2)
    s3 = i*(w//2+w%2)
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
for i in range(1,w//2+1):
    s1 = i*h
    s2 = i*(h//2)
    s3 = i*(h//2+h%2)
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
s1 = (h//3+h%3)*w - (h//3)*3
s2 = (w//3+w%3)*h - (w//3)*3
s3 = max((h//2)*w,(h//2+h%2)*(w//2+w%2))-min((h//2)*w,(h//2+h%2)*(w//2))
s4 = max((w//2)*h,(w//2+w%2)*(h//2+h%2))-min((w//2)*h,(w//2+w%2)*(h//2))
print(min(s1,s2,s3,s4))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
ans = []
for h in range(1,H+1):
    head = h*W
    middleW = W//2
    middleH = (H-h)//2
    under1 = (H-h)*(middleW)
    under2 = (H-h)*(W-middleW)
    under3 = (middleH)*W
    under4 = ((H-h)-middleH)*W
    ans.append(max(head,under1,under2)-min(head,under1,under2))
    ans.append(max(head,under3,under4)-min(head,under3,under4))
for w in range(1,W+1):
    W,h = H,w 
    head = h*W
    middleW = W//2
    middleH = (H-h)//2
    under1 = (H-h)*(middleW)
    under2 = (H-h)*(W-middleW)
    under3 = (middleH)*W
    under4 = ((H-h)-middleH)*W
    ans.append(max(head,under1,under2)-min(head,under1,under2))
    ans.append(max(head,under3,under4)-min(head,under3,under4))
print(min(ans))