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

def main():
    H, W = map(int, input().split())
    # if (H > 2 and H % 3 == 0) or (W > 2 and W % 3 == 0):
    #     print(0)
    # else:
    ans = H * W
    left = 0
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            up += 1
        elif b == min(a, b, c):
            left += 1
        else:
            break
    if H > 2:
        i = 1
        j = 2
        while 1:
            a = W * i
            b = W * j - a
            c = H * W - b - a
            ans = min(ans, max(a, b, c) - min(a, b, c))
            if a == min(a, b, c):
                i += 1
                j += 1
            elif b == min(a, b, c):
                j += 1
            else:
                break
    H, W = W, H
    left = 0
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            up += 1
        elif b == min(a, b, c):
            left += 1
        else:
            break
    if H > 2:
        i = 1
        j = 2
        while 1:
            a = W * i
            b = W * j - a
            c = H * W - b - a
            ans = min(ans, max(a, b, c) - min(a, b, c))
            if a == min(a, b, c):
                i += 1
                j += 1
            elif b == min(a, b, c):
                j += 1
            else:
                break
        
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

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

if (H > 2 and H % 3 == 0) or (W > 2 and W % 3 == 0):
    print(0)
else:
    ans = H * W
    left = 0
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            if up == H - 1 : break
            up += 1
        elif b == min(a, b, c):
            if left == W - 1 : break
            left += 1
        else:
            break;
    H, W = W, H
    left = 0
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            if up == H - 1 : break
            up += 1
        elif b == min(a, b, c):
            if left == W - 1 : break
            left += 1
        else:
            break;
    print(ans)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

import math

def calc(a, b):
    pivot = b // 3

    diff_t = a * b
    for i in range(max(0, pivot - 1), min(b, pivot + 3)):
        area_1 = i * a
        area_2 = (b - i) * math.ceil(a / 2)
        area_3 = (b - i) * math.floor(a / 2)
        diff = max(area_1, area_2, area_3) - min(area_1, area_2, area_3)
        if diff < diff_t:
            diff_t = diff

    return diff_t

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

diff_river = min(w, h)
print(min(calc(h, w), calc(w, h), diff_river))

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

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

if (H > 2 and H % 3 == 0) or (W > 2 and W % 3 == 0):
    print(0)
else:
    ans = H * W
    left = 0
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            up += 1
        elif b == min(a, b, c):
            left += 1
        else:
            break;
    H, W = W, H
    left = 0
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            up += 1
        elif b == min(a, b, c):
            left += 1
        else:
            break;
    print(ans)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

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

if (H > 2 and H % 3 == 0) or (W > 2 and W % 3 == 0):
    print(0)
else:
    ans = H * W
    left = 1
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            up += 1
        elif b == min(a, b, c):
            left += 1
        else:
            break;
    H, W = W, H
    left = 1
    up = 1
    while 1:
        a = W * up
        b = (H - up) * (left)
        c = (H - up) * (W - left)
        ans = min(ans, max(a, b, c) - min(a, b, c))
        if a == min(a, b, c):
            up += 1
        elif b == min(a, b, c):
            left += 1
        else:
            break;
    print(ans)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

import math

def calc(a, b):
    pivot = b // 3

    diff_t = a * b
    for i in range(3):
        area_1 = (pivot + i) * a
        area_2 = (b - pivot - i) * math.ceil(a / 2)
        area_3 = (b - pivot - i) * math.floor(a / 2)
        diff = max(area_1, area_2, area_3) - min(area_1, area_2, area_3)
        if diff < diff_t:
            diff_t = diff

    return diff_t

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

diff_river = min((h % 3) * w, (w % 3) * h)
print(min(calc(h, w), calc(w, h), diff_river))

Submission status: Wrong Answer

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


def calc(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    num1, num2 = float("inf"), float("inf")
    for i in range(1, h):
        a = i * w
        b = (h - i) * (w // 2)
        c = (h - i) * (w - w // 2)
        ma = max(a, max(b, c))
        mi = min(a, min(b, c))
        num1 = min(num1, ma - mi)

    for i in range(1, h):
        a = i * w
        p = (h - (h - i) // 2) * w
        q = (h - i - (h - i) // 2) * w
        mp = max(a, max(p, q))
        mq = min(a, min(p, q))
        num2 = min(num2, mp - mq)

    return min(num1, num2)


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

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    num = float("inf")
    for i in range(h):
        a = i * w
        b = (h - i) * (w // 2)
        c = (h - i) * (w + 1) // 2
        p = (h - i) * (h - i) // 2
        q = (h - i) * (h - i + 1) // 2
        num = min(num, max(a, max(b, c)) - min(a, min(b, c)))
        num = min(num, max(a, max(p, q)) - min(a, min(p, q)))
    print(num)

Submission status: Wrong Answer

Solution code:
import math

l = sorted(list(map(int, input().split())))
s = l[0]*l[1]
if(l[0]%3==0 or l[1]%3==0):
    print(0)
else:
    l1 = round(l[1]/3)
    l[1] -= l1
    a = l1*l[0]
    s -= a
    l.sort()
    if(s%2==0):
        print(abs(s//2-a))
    else:
        l2 = math.ceil(l[1]/2)
        b = l2*l[0]
        c = s - b
        print(max(a,b,c) - min(a,b,c))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python

import sys

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

ind1 = int(H*W/3)
ind2 = ind1+1

#first trial
Wp = int(round(ind1/float(H)))
p1 = H*Wp
Wresid = W-Wp
if Wresid%2 == 0: p2 = p3 = Wresid/2*H
elif H%2 == 0: p2=p3=H/2*Wresid
else:
    Ht, Wt = max([H, Wresid]), min([H, Wresid])
    Hresidspl1 = Ht/2
    Hresidspl2 = Ht-Hresidspl1
    p2 = Wresid*Hresidspl1
    p3 = Wresid*Hresidspl2
ps1 = [p1, p2, p3]
diff1 = max(ps1)-min(ps1)

#second trial
Wp = int(round(ind2/float(H)))
p1 = H*Wp
Wresid = W-Wp
if Wresid%2 == 0: p2 = p3 = Wresid/2*H
elif H%2 == 0: p2 = p3 = H/2*Wresid
else:
    Ht, Wt = max([H, Wresid]), min([H, Wresid])
    Hresidspl1 = Ht/2
    Hresidspl2 = Ht-Hresidspl1
    p2 = Wresid*Hresidspl1
    p3 = Wresid*Hresidspl2
ps2 = [p1, p2, p3]
diff2 = max(ps2)-min(ps2)

#third trial
Hp = int(round(ind1/float(W)))
p1 = W*Hp
Hresid = H-Hp
if Hresid%2 == 0: p2 = p3 = Hresid/2*W
elif W%2 == 0: p2 = p3 = W/2*Hresid
else:
    Wt, Ht = max([Hresid, W]), min([Hresid, W])
    Wresidspl1 = Wt/2
    Wresidspl2 = Wt-Wresidspl1
    p2 = Hresid*Wresidspl1
    p3 = Hresid*Wresidspl2
ps3 = [p1, p2, p3]
diff3 = max(ps3)-min(ps3)

#fourth trial
Hp = int(round(ind2/float(W)))
p1 = W*Hp
Hresid = H-Hp
if Hresid%2==0: p2 = p3 = Hresid/2*W
elif W%2 == 0: p2 = p3 = W/2*Hresid
else:
    Wt, Ht = max([Hresid, W]), min([Hresid, W])
    Wresidspl1 = Wt/2
    Wresidspl2 = Wt-Wresidspl1
    p2 = Hresid*Wresidspl1
    p3 = Hresid*Wresidspl2
ps4 = [p1, p2, p3]
diff4 = max(ps4)-min(ps4)

print min([diff1, diff2, diff3, diff4])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python

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

ind1 = int(H*W/3)
ind2 = ind1+1

#first trial
Wp = int(round(ind1/float(H)))
p1 = H*Wp
Wresid = W-Wp
if Wresid%2 == 0:
    p2 = p3 = Wresid/2*H
else:
    Ht, Wt = max([H, Wresid]), min([H, Wresid])
    Hresidspl1 = Ht/2
    Hresidspl2 = Ht-Hresidspl1
    p2 = Wresid*Hresidspl1
    p3 = Wresid*Hresidspl2
ps1 = [p1, p2, p3]
diff1 = max(ps1)-min(ps1)

#second trial
Wp = int(round(ind2/float(H)))
p1 = H*Wp
Wresid = W-Wp
if Wresid%2 == 0:
    p2 = p3 = Wresid/2*H
else:
    Ht, Wt = max([H, Wresid]), min([H, Wresid])
    Hresidspl1 = Ht/2
    Hresidspl2 = Ht-Hresidspl1
    p2 = Wresid*Hresidspl1
    p3 = Wresid*Hresidspl2
ps2 = [p1, p2, p3]
diff2 = max(ps2)-min(ps2)

#third trial
Hp = int(round(ind1/float(W)))
p1 = W*Hp
Hresid = H-Hp
if Hresid%2 == 0:
    p2 = p3 = Hresid/2*W
else:
    Wt, Ht = max([Hresid, W]), min([Hresid, W])
    Wresidspl1 = Wt/2
    Wresidspl2 = Wt-Wresidspl1
    p2 = Hresid*Wresidspl1
    p3 = Hresid*Wresidspl2
ps3 = [p1, p2, p3]
diff3 = max(ps3)-min(ps3)

#forth trial
Hp = int(round(ind2/float(W)))
p1 = W*Hp
Hresid = H-Hp
if Hresid%2==0:
    p2 = p3 = Hresid/2*W
else:
    Wt, Ht = max([Hresid, W]), min([Hresid, W])
    Wresidspl1 = Wt/2
    Wresidspl2 = Wt-Wresidspl1
    p2 = Hresid*Wresidspl1
    p3 = Hresid*Wresidspl2
ps4 = [p1, p2, p3]
diff4 = max(ps4)-min(ps4)

print min([diff1, diff2, diff3, diff4])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python

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

ind1 = int(H*W/3)
ind2 = ind1+1

#first trial
Wp = int(round(ind1/float(H)))
p1 = H*Wp
Wresid = W-Wp
if Wresid%2 == 0:
    p2 = p3 = Wresid/2*H
else:
    Hresidspl1 = H/2
    Hresidspl2 = H-Hresidspl1
    p2 = Wresid*Hresidspl1
    p3 = Wresid*Hresidspl2
ps1 = [p1, p2, p3]
diff1 = max(ps1)-min(ps1)

#second trial
Wp = int(round(ind2/float(H)))
p1 = H*Wp
Wresid = W-Wp
if Wresid%2 == 0:
    p2 = p3 = Wresid/2*H
else:
    Hresidspl1 = H/2
    Hresidspl2 = H-Hresidspl1
    p2 = Wresid*Hresidspl1
    p3 = Wresid*Hresidspl2
ps2 = [p1, p2, p3]
diff2 = max(ps2)-min(ps2)

#third trial
Hp = int(round(ind1/float(W)))
p1 = W*Hp
Hresid = H-Hp
if Hresid%2 == 0:
    p2 = p3 = Hresid/2*W
else:
    Wresidspl1 = W/2
    Wresidspl2 = W-Wresidspl1
    p2 = Hresid*Wresidspl1
    p3 = Hresid*Wresidspl2
ps3 = [p1, p2, p3]
diff3 = max(ps3)-min(ps3)

#forth trial
Hp = int(round(ind2/float(W)))
p1 = W*Hp
Hresid = H-Hp
if Hresid%2==0:
    p2 = p3 = Hresid/2*W
else:
    Wresidspl1 = W/2
    Wresidspl2 = W-Wresidspl1
    p2 = Hresid*Wresidspl1
    p3 = Hresid*Wresidspl2
ps4 = [p1, p2, p3]
diff4 = max(ps4)-min(ps4)

print min([diff1, diff2, diff3, diff4])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python

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

ind1 = int(H*W/3)
ind2 = ind1+1

#first trial
Wp = int(round(ind1/float(H)))
p1 = H*Wp
Wresid = W-Wp
Hresidspl1 = H/2
Hresidspl2 = H-Hresidspl1
p2 = Wresid*Hresidspl1
p3 = Wresid*Hresidspl2
ps1 = [p1, p2, p3]
diff1 = max(ps1)-min(ps1)

#second trial
Wp = int(round(ind2/float(H)))
p1 = H*Wp
Wresid = W-Wp
Hresidspl1 = H/2
Hresidspl2 = H-Hresidspl1
p2 = Wresid*Hresidspl1
p3 = Wresid*Hresidspl2
ps2 = [p1, p2, p3]
diff2 = max(ps2)-min(ps2)

#third trial
Hp = int(round(ind1/float(W)))
p1 = W*Hp
Hresid = H-Hp
Wresidspl1 = W/2
Wresidspl2 = W-Wresidspl1
p2 = Hresid*Wresidspl1
p3 = Hresid*Wresidspl2
ps3 = [p1, p2, p3]
diff3 = max(ps3)-min(ps3)

#forth trial
Hp = int(round(ind2/float(W)))
p1 = W*Hp
Hresid = H-Hp
Wresidspl1 = W/2
Wresidspl2 = W-Wresidspl1
p2 = Hresid*Wresidspl1
p3 = Hresid*Wresidspl2
ps4 = [p1, p2, p3]
diff4 = max(ps4)-min(ps4)

print min([diff1, diff2, diff3, diff4])

Submission status: Wrong Answer

Solution code:
length = list(map(int, input().split()))
dif_min = 0
for i in range(2):
    h, w = length[i%2], length[(i+1)%2]
    for j in range(1, h):
        s1 = j * w
        s2, s3 = (h-j) // 2 * w, ((h-j)-(h-j)//2) * w
        dif1 = max(s1, s2, s3) - min(s1, s2, s3)

        s2, s3 = (h-j)*(w//2), (h-j) * (w-w//2)
        dif2 = max(s1, s2, s3) - min(s1, s2, s3)

        if j == 1:
            dif_min = min(dif1, dif2)
        elif min(dif1, dif2) < dif_min:
            dif_min = min(dif1, dif2)

print(dif_min)

Submission status: Wrong Answer

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

if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    h, w = max(h, w), min(h, w)
    h1 = round(h/3, 0)
    s1 = h1 * w
    h -= h1
    if h % 2 == 0 or w % 2 == 0:
        s2 = s3 = h * w // 2
    else:
        h, w = max(h, w), min(h, w)
        h2 = round(h/2, 0)
        s2 = h2 * w
        h -= h2
        s3 = h * w

    print(int(max(s1, s2, s3)-min(s1, s2, s3)))

Submission status: Wrong Answer

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

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

ans = H*W

if H%2==0 and W%2==0:
    for k in range(1,W):
        ans = min(ans,max(H*k,(W-k)*(H//2))-min(H*k,(W-k)*(H//2)))
    for k in range(1,H):
        ans = min(ans,max(W*k,(H-k)*(W//2))-min(W*k,(H-k)*(W//2)))
elif H%2==1 and W%2==0:
    for k in range(1,W):
        ans = min(ans,max(H*k,(W-k)*(H//2),(W-k)*(H//2+1))-min(H*k,(W-k)*(H//2),(W-k)*(H//2+1)))
    for k in range(1,H):
        ans = min(ans,max(W*k,(H-k)*(W//2))-min(W*k,(H-k)*(W//2)))
elif H%2==0 and W%2==1:
    for k in range(1,W):
        ans = min(ans,max(H*k,(W-k)*(H//2))-min(H*k,(W-k)*(H//2)))
    for k in range(1,H):
        ans = min(ans,max(W*k,(H-k)*(W//2),(H-k)*(W//2+1))-min(W*k,(H-k)*(W//2),(H-k)*(W//2+1)))
else:
    for k in range(1,W):
        ans = min(ans,max(H*k,(W-k)*(H//2),(W-k)*(H//2+1))-min(H*k,(W-k)*(H//2),(W-k)*(H//2+1)))    
    for k in range(1,H):
        ans = min(ans,max(W*k,(H-k)*(W//2),(H-k)*(W//2+1))-min(W*k,(H-k)*(W//2),(H-k)*(W//2+1)))

print(ans)

Submission status: Wrong Answer

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

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

if W < H:
    t = W
    W = H
    H = t

ans = H*W

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

print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[]
ans_2=[]
ans_3=[]
ans_4=[]
for i in range(1,w-1):
    a=h*i
    res=w-i
    if res%2==0:
        b=h*(res//2)
        c=h*(res//2)
    else:
        b=h*(res//2)
        c=h*(res//2+1)
    ans.append(max(a,b,c)-min(a,b,c))
for i in range(1,h-1):
    a_2=i*w
    res_2=h-i
    if res_2%2==0:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2)
    else:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2+1)
    ans_2.append(max(a_2,b_2,c_2)-min(a_2,b_2,c_2))
for i in range(1,w-1):
    a_3=i*h
    res_3=w-i
    if h%2==0:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2)
    else:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2+1)
    ans_3.append(max(a_3,b_3,c_3)-min(a_3,b_3,c_3))
for i in range(1,h-1):
    a_4=w*i
    res_4=h-i
    if w%2==0:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2)
    else:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2+1)
    ans_4.append(max(a_4,b_4,c_4)-min(a_4,b_4,c_4))
if len(ans)==0:
    v=float("inf")
else:
    v=min(ans)
if len(ans_2)==0:
    x=float("inf")
else:
    x=min(ans_2)
if len(ans_3)==0:
    y=float("inf")
else:
    y=min(ans_3)
if len(ans_4)==0:
    z=float("inf")
else:
    z=min(ans_4)
print(min(v,x,y,z))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=[]
ans_2=[]
ans_3=[]
ans_4=[]
for i in range(1,w-1):
    a=h*i
    res=w-i
    if res%2==0:
        b=h*(res//2)
        c=b*(res//2)
    else:
        b=h*(res//2)
        c=h*(res//2+1)
    ans.append(max(a,b,c)-min(a,b,c))
for i in range(1,h-1):
    a_2=i*w
    res_2=h-i
    if res_2%2==0:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2)
    else:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2+1)
    ans_2.append(max(a_2,b_2,c_2)-min(a_2,b_2,c_2))
for i in range(1,w-1):
    a_3=i*h
    res_3=w-i
    if h%2==0:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2)
    else:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2+1)
    ans_3.append(max(a_3,b_3,c_3)-min(a_3,b_3,c_3))
for i in range(1,h-1):
    a_4=w*i
    res_4=h-i
    if w%2==0:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2)
    else:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2+1)
    ans_4.append(max(a_4,b_4,c_4)-min(a_4,b_4,c_4))
if len(ans)==0:
    v=float("inf")
else:
    v=min(ans)
if len(ans_2)==0:
    x=float("inf")
else:
    x=min(ans_2)
if len(ans_3)==0:
    y=float("inf")
else:
    y=min(ans_3)
if len(ans_4)==0:
    z=float("inf")
else:
    z=min(ans_4)
print(min(v,x,y,z))

Submission status: Wrong Answer

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

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

ans = H*W
if H%2==0:
    for w in range(1,W):
        s1 = w*H
        s2 = (W-w)*H//2
        ans = min(ans, abs(s1 - s2))
        #if s1 > s2: break
if W%2==0:
    for h in range(1,H):
        s1 = h*W
        s2 = (H-h)*W//2
        ans = min(ans, abs(s1 - s2))
        #if s1 > s2: break

for h in range(H//2,H//2+2):
    if h >= H: break
    for w in range(W//2,W//2+2):
        if w >= W: break
        s1 = h*W
        s2 = (H-h)*w
        s3 = (H-h)*(W-w)
        ans = min(ans, s1, s2, s3)

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

ans = H*W
if H%2==0:
    for w in range(1,W):
        s1 = w*H
        s2 = (W-w)*H//2
        ans = min(ans, abs(s1 - s2))
        if s1 > s2: break
if W%2==0:
    for h in range(1,H):
        s1 = h*W
        s2 = (H-h)*W//2
        ans = min(ans, abs(s1 - s2))
        if s1 > s2: break

for h in range(H//2,H//2+2):
    if h >= H: break
    for w in range(W//2,W//2+2):
        if w >= W: break
        s1 = h*W
        s2 = (H-h)*w
        s3 = (H-h)*(W-w)
        ans = min(ans, s1, s2, s3)

print(ans)

Submission status: Wrong Answer

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

sss = []

for i in range(h):
    for j in range(w):
        p = i * j
        q = i * (w - j)
        r = w * (h-i)
        sss.append(max(p,q,r) - min(p,q,r))
        
        s = i * j
        t = (h-i) * j
        u = j * (w-j)
        sss.append((max(s,t,u) - min(s,t,u)))
        
print(max(sss))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if (h%3)*(w%3)==0:
    print(0)
    exit()
else:
    hh=(h*w)//3//h
    ww=(h*w)//3//w
    a=max(hh*h,(w-hh)*(h//2),(w-hh)*(h-h//2))-min(hh*h,(w-hh)*(h//2),(w-hh)*(h-h//2))
    b=max(ww*w,(h-ww)*(w//2),(h-ww)*(w-w//2))-min(ww*w,(h-ww)*(w//2),(h-ww)*(w-w//2))
    c=max((hh+1)*h,(w-hh-1)*(h//2),(w-hh-1)*(h-h//2))-min((hh+1)*h,(w-hh-1)*(h//2),(w-hh-1)*(h-h//2))
    d=max((ww+1)*w,(h-ww-1)*(w//2),(h-ww-1)*(w-w//2))-min((ww+1)*w,(h-ww-1)*(w//2),(h-ww-1)*(w-w//2))
    print(min(a,b,c,d))

Submission status: Wrong Answer

Solution code:
h,w = (int(i) for i in input().split())
if h%3==0 or w%3==0:
	answer = 0
elif h==2 or w==2:
	answer = 1
else:
	h1,w1 = (h-h%3)//3,(w-w%2)//2
	x = [w*h1,(h-h1)*(w-w1),(h-h1)*w1]
	y = [w*(h1+1),(h-h1-1)*w1,(h-h1-1)*(w-w1)]
	a = min(max(x)-min(x),max(y)-min(y))
	w2,h2 = (w-w%3)//3,(h-h%2)//2
	x2 = [h*w2,(w-w2)*(h-h2),(w-w2)*h2]
	y2 = [h*(w2+1),(w-w2-1)*h2,(w-w2-1)*(h-h2)]
	b = min(max(x2)-min(x2),max(y2)-min(y2))
	answer = min(a,b)
print(answer)

Submission status: Wrong Answer

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

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

if h%3==0 or w%3==0:
    print(0)
else:
    a1=h//3
    ans=(h//3*w-(h-h//3)*(w//2))
    print(ans if ans>0 else -ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())

print(n*m%3)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
  print(0)
else:
  ans=10000000000000
  h=H//3
  w=W//2
  ptn1=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn1)-min(ptn1))
  
  h=H//3+1
  w=W//2
  ptn2=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  w=W//2+1
  ptn3=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  h=H//3
  w=W//2+1
  ptn4=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn4)-min(ptn4))
  
  h=H//3
  ptn5=[h*W,(H-h*2)*W]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  ptn6=[h*W,(H-h*2)*W]
  ans=min(ans,max(ptn3)-min(ptn3))

  H,W=W,H
  h=H//3
  w=W//2
  ptn1=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn1)-min(ptn1))
  
  h=H//3+1
  w=W//2
  ptn2=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  w=W//2+1
  ptn3=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  h=H//3
  w=W//2+1
  ptn4=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn4)-min(ptn4))
  
  h=H//3
  ptn5=[h*W,(H-h*2)*W]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  ptn6=[h*W,(H-h*2)*W]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  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=10000000000000
  h=H//3
  w=W//2
  ptn1=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn1)-min(ptn1))
  
  h=H//3+1
  w=W//2
  ptn2=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  w=W//2+1
  ptn3=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  h=H//3
  w=W//2+1
  ptn4=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn4)-min(ptn4))
  
  h=H//3+2
  w=W//2
  ptn5=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+2
  w=W//2+1
  ptn6=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))

  H,W=W,H
  h=H//3
  w=W//2
  ptn1=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn1)-min(ptn1))
  
  h=H//3+1
  w=W//2
  ptn2=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  w=W//2+1
  ptn3=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  h=H//3
  w=W//2+1
  ptn4=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn4)-min(ptn4))
  
  h=H//3+2
  w=W//2
  ptn5=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+2
  w=W//2+1
  ptn6=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  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=10000000000000
  h=H//3
  w=W//2
  ptn1=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn1)-min(ptn1))
  
  h=H//3+1
  w=W//2
  ptn2=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  w=W//2+1
  ptn3=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  h=H//3
  w=W//2+1
  ptn4=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn4)-min(ptn4))

  H,W=W,H
  h=H//3
  w=W//2
  ptn1=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn1)-min(ptn1))
  
  h=H//3+1
  w=W//2
  ptn2=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn2)-min(ptn2))
  
  h=H//3+1
  w=W//2+1
  ptn3=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn3)-min(ptn3))
  
  h=H//3
  w=W//2+1
  ptn4=[h*W,(H-h)*w,(H-h)*(W-w)]
  ans=min(ans,max(ptn4)-min(ptn4))
  
  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:
  d2=10**15
  d4=10**15
  h10=h//3+1
  w10=w//2
  s11=h10*w
  s12=w10*(h-h10)
  s13=(w-w10)*(h-h10)
  d1=max([s11,s12,s13])-min([s11,s12,s13])
  if(h!=2):
    h10-=1
    w10=w//2
    s21=h10*w
    s22=w10*(h-h10)
    s23=(w-w10)*(h-h10)
    d2=max([s21,s22,s23])-min([s21,s22,s23])
    
  w20=w//3+1
  h20=h//2
  s31=w20*h
  s32=h20*(w-w20)
  s33=(h-h20)*(w-w20)
  d3=max([s31,s32,s33])-min([s31,s32,s33])
  if(w!=2):
    w20-=1
    s41=w20*h
    s42=h20*(w-w20)
    s43=(h-h20)*(w-w20)
    d4=max([s41,s42,s43])-min([s41,s42,s43])
  print(min([d1,d2,d3,d4]))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if(h%3==0 or w%3==0):print(0)
else:
  d2=10**15
  d4=10**15
  h10=h//3+1
  w10=w//2
  s11=h10*w
  s12=w10*(h-h10)
  s13=(w-w10)*(h-h10)
  d1=max([s11,s12,s13])-min([s11,s12,s13])
  if(h!=2):
    h10-=1
    w10=w//2
    s21=h10*w
    s22=w10*(h-h10)
    s23=(w-w10)*(h-h10)
    d2=max([s21,s22,s23])-min([s21,s22,s23])
    
  w20=w//3+1
  h20=h//2
  s31=w20*h
  s32=h20*(w-w20)
  s33=(h-h20)*(w-w20)
  d3=max([s31,s32,s33])-min([s31,s32,s33])
  if(w!=2):
    w-=1
    s41=w20*h
    s42=h20*(w-w20)
    s43=(h-h20)*(w-w20)
    d4=max([s41,s42,s43])-min([s41,s42,s43])
  print(min([d1,d2,d3,d4]))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if (H*W)%3==0:
    print(0)
else:
    x=min([H, W]); y=max([H, W])
    s = 0
    if y%3==1:
        s=y//3
    else:
        s=(y//3)+1
    a=x*s
    y -=s
    if (x*y)%2==0:
        b=(x*y)//2
        c=(x*y)//2
    else:
        s=min([x,y]);t=max([x,y])
        b=s*(t//2)
        c=s*(t-(t//2))
    print(max([a,b,c])-min([a,b,c]))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
import math
if h%3==0 or w%3==0:
    print(0)
    exit()
w_i=w//2
ans=[]
for i in range(1,h):
    sa=i*w
    sb=(h-i)*w_i
    sc=(h-i)*(w-w_i)
    ans.append(max(sa,sb,sc)-min(sa,sb,sc))
v=min(ans)
ans_3=[]
for l in range(1,h):
    h_half=(h-l)//2
    sa=w*l
    sb=w*h_half
    sc=w*(h-h_half)
    ans_3.append(max(sa,sb,sc)-min(sa,sb,sc))
v_3=min(ans_3)    
ans_i=[]
h_i=h//2
for j in range(1,w):
    sa=j*h
    sb=(w-j)*(h_i)
    sc=(w-j)*(h-h_i)
    ans_i.append(max(sa,sb,sc)-min(sa,sb,sc))
v_i=min(ans_i)
ans_2=[]
for k in range(1,w):
    w_half=(w-k)//2
    sa=h*k
    sb=h*w_half
    sc=h*(w-w_half)
    ans_2.append(max(sa,sb,sc)-min(sa,sb,sc))
v_2=min(ans_2)    
print(min(v,v_i,v_2,v_3))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
import math
if h%3==0 or w%3==0:
    print(0)
    exit()
w_i=w//2
ans=[]
for i in range(1,h):
    sa=i*w
    sb=(h-i)*w_i
    sc=(h-i)*(w-w_i)
    ans.append(max(sa,sb,sc)-min(sa,sb,sc))
v=min(ans)
ans_i=[]
h_i=h//2
for j in range(1,w):
    sa=j*h
    sb=(w-j)*(h_i)
    sc=(w-j)*(h-h_i)
    ans_i.append(max(sa,sb,sc)-min(sa,sb,sc))
v_i=min(ans_i)
print(min(v,v_i))
#86 5

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
w_i=w//2
ans=[]
for i in range(1,h):
    sa=i*w
    sb=(h-i)*w_i
    sc=(h-i)*(w-w_i)
    ans.append(max(sa,sb,sc)-min(sa,sb,sc))
v=min(ans)
ans_i=[]
h_i=h//2
for j in range(1,w):
    sa=j*h
    sb=(w-j)*(h_i)
    sc=(w-j)*(h-h_i)
    ans_i.append(max(sa,sb,sc)-min(sa,sb,sc))
v_i=min(ans_i)
print(min(v,v_i))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
w_i=w//2
ans=[]
for i in range(1,h):
    sa=i*w
    sb=(h-i)*w_i
    sc=(h-i)*(w-w_i)
    ans.append(max(sa,sb,sc)-min(sa,sb,sc))
v=min(ans)
ans_i=[]
h_i=h//2
for j in range(1,w):
    sa=j*h
    sb=(w-j)*(h_i)
    sc=(w-j)*(h-h_i)
    ans_i.append(max(sa,sb,sc)-min(sa,sb,sc))
v_i=min(ans_i)
print(min(v,v_i))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
w_i=w//2
ans=[]
for i in range(h+1):
    sa=i*w
    sb=(h-i)*w_i
    sc=(h-i)*(w-w_i)
    ans.append(max(sa,sb,sc)-min(sa,sb,sc))
v=min(ans)

ans_i=[]
h_i=h//2
for j in range(w+1):
    sa=j*h
    sb=(w-j)*(h_i)
    sc=(w-j)*(h-h_i)
    ans_i.append(max(sa,sb,sc)-min(sa,sb,sc))
v_i=min(ans_i)
print(min(v,v_i))

Submission status: Wrong Answer

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

ans_i=[]
h_i=h//2
for j in range(1,w):
    sa=j*h
    sb=(w-j)*(h_i)
    sc=(w-j)*(h-h_i)
    ans_i.append(max(sa,sb,sc)-min(sa,sb,sc))
v_i=min(ans_i)
print(min(v,v_i))

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<w:
    h,w=w,h
w_i=w//2
ans=[]
for i in range(1,h):
    sa=i*w
    sb=(h-i)*w_i
    sc=(h-i)*(w-w_i)
    ans.append(max(sa,sb,sc)-min(sa,sb,sc))
print(min(ans))

Submission status: Wrong Answer

Solution code:
import math

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

A=0
B=0
C=0
ans=10**10
mem=0

if h>=w:

    for i in range(1,h+1):
        A=i*w
        B=(h-i)*math.ceil(w/2)
        C=(h-i)*(w-math.ceil(w/2))
        mem=max(A,B,C)-min(A,B,C)

        if mem<ans:
            ans=mem

if w>h:

    for i in range(1,w+1):
        A=i*h
        B=(w-i)*math.ceil(h/2)
        C=(w-i)*(w-math.ceil(w/2))
        mem=max(A,B,C)-min(A,B,C)

        if mem<ans:
            ans=mem

print(ans)

Submission status: Wrong Answer

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

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

st = (H*W//3)
candi_h = []
candi_w = []
candi_h.append(st - (st % H))
candi_h.append(st-(st % H) + H)
candi_h.append(st-(st % H) - H)
candi_w.append(st - (st % W))
candi_w.append(st - (st % W) + W)
candi_w.append(st - (st % W) - W)

ans = float('inf')
for sp in candi_h:
	if sp <= 0:
		continue
	other = (W - (sp // H)) * (H // 2)
	div = [sp,other,H*W-sp-other]
	ans = min(ans,max(div)-min(div))

for sp in candi_w:
	if sp <= 0:
		continue
	other = (H - (sp // W)) * (W // 2)
	div = [sp,other,H*W-sp-other]
	ans = min(ans,max(div)-min(div))

print(ans)

Submission status: Wrong Answer

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

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

st = (H*W//3)
candi_h = []
candi_w = []
candi_h.append(st - (st % H))
candi_h.append(st-(st % H) + H)
candi_w.append(st - (st % W))
candi_w.append(st - (st % W) + W)

ans = float('inf')
for sp in candi_h:
	other = (W - (sp // H)) * (H // 2)
	div = [sp,other,H*W-sp-other]
	ans = min(ans,max(div)-min(div))

for sp in candi_w:
	other = (H - (sp // W)) * (W // 2)
	div = [sp,other,H*W-sp-other]
	ans = min(ans,max(div)-min(div))

print(ans)

Submission status: Wrong Answer

Solution code:
H,W = [int(i) for i in input().split()]
h=H%3
w=W%3
if h==0 or w==0:
  print(0)
elif h==1 or w==1:
  print(2)
elif h==2 or w==2:
  print(4)