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:
x, y = map(int, input().split())

h, w = min(x, y), max(x, y)

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

ans = h
k = h//2
if h % 2:
    for i in range(1, w):
        l = w - i
        s1 = h * i
        s2 = k * l
        s3 = (k+1)*l
        mn = min(s1, s2, s3)
        mx = max(s1, s2, s3)
        ans = min(ans, mx-mn)

else:
    for i in range(1, w):
        l = w - i
        s1 = h*i
        s2 = k*l
        mn = min(s1, s2)
        mx = max(s1, s2)
        ans = min(ans, mx-mn)

print(ans)

Submission status: Wrong Answer

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

S = H * W
ans = 0
if H * W % 3 != 0:
    ans = 10 ** 11
    for a, b in zip((H, W), (W, H)):
        for i in range(1, (a+1)//2):
            S1 = i * b
            S2 = (a - i) * (b // 2)
            S3 = S - S1 - S2
            S4 = ((a - i) // 2) * b
            S5 = S - S1 - S4

            ans = min(ans,
                      max(S1, S2, S3) - min(S1, S2, S3),
                      max(S1, S4, S5) - min(S1, S4, S5))

print(ans)

Submission status: Wrong Answer

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

S = H * W
ans = 0
if H * W % 3 != 0:
    ans = 10 ** 10 + 1
    for a, b in zip((H, W), (W, H)):
        for i in range(1, (a+1)//2):
            S1 = i * b
            S2 = (a - i) * (b // 2)
            S3 = S - S1 - S2
            S4 = ((a - i) // 2) * b
            S5 = S - S1 - S4

            ans = min(ans,
                      max(S1, S2, S3) - min(S1, S2, S3),
                      max(S1, S4, S5) - min(S1, S4, S5)
                  )

print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S1 = [0, 0, 0]
S2 = [0, 0, 0]

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

    for i in range(1, (W+1)//2):
        S2[0] = i * H
        S2[1] = (H // 2) * (W - i)
        S2[2] = H * W - S2[0] - S2[1]
    w = max(S2) - min(S2)

    ans = min(h, w)

print(ans)

Submission status: Wrong Answer

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

ans = H*W
for h in range(1,H):
    A = W*h

    h_res = H - h

    B1 = W*(h_res//2)
    C1 = W*(h_res - h_res//2)
    diff1 = max(abs(A - B1), abs(A - C1))

    B2 = h_res * (W//2)
    C2 = h_res * (W - W//2)
    diff2 = max(abs(A - B2), abs(A - C2))

    ans = min(ans, max(diff1, abs(B1 - C1)), max(diff2, abs(B2 - C2)))

print(ans)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
if (a % 3 ==0) | (b % 3 ==0):
    ans = 0
else:
    anss = []
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 +1)) * (a//2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 ) * a
    s2 = (b -  b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - b // 3 -1) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))
    
    # b a
    t = a
    a = b
    b = t
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 + 1)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - b // 3 - 1) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))
    
    


    ans = min(anss)

print(ans)

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split()))
a = min(l)
b = max(l)
if (a % 3 ==0) | (b % 3 ==0):
    ans = 0
else:
    anss = []
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 +1)) * (a//2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 ) * a
    s2 = (b -  b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - b // 3 -1) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))


    ans = min(anss)

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    l = list(map(int, input().split()))
    a = min(l)
    b = max(l)
    if (a % 3 ==0) | (b % 3 ==0):
        ans = 0
    else:
        anss = []
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 +1)) * (a//2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 ) * a
        s2 = (b -  b // 3) * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - b // 3) * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        
        b = min(l);
        a = max(l);
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - b // 3) * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - b // 3) * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))


        ans = min(anss)

    print(ans)
main()

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split()))
a = min(l)
b = max(l)
if (a % 3 ==0) | (b % 3 ==0):
    ans = 0
else:
    anss = []
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 +1)) * (a//2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 ) * a
    s2 = (b -  b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - b // 3) * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    ans = min(anss)

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    l = list(map(int, input().split()))
    a = min(l)
    b = max(l)
    if (a % 3 ==0) | (b % 3 ==0):
        ans = 0
    else:
        anss = []
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 +1)) * (a//2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b -  b // 3) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3 + 1)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        ans = min(anss)

    print(ans)
main()

Submission status: Wrong Answer

Solution code:
def main():
    l = list(map(int, input().split()))
    a = min(l)
    b = max(l)
    if (a % 3 ==0) | (b % 3 ==0):
        ans = 0
    else:
        anss = []
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 +1)) * (a//2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 2) * a
        s2 = (b - (b // 3 + 2)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 2) * a
        s2 = (b - (b // 3 + 2)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        ans = min(anss)

    print(ans)

main()

Submission status: Wrong Answer

Solution code:
def main():
    l = list(map(int, input().split()))
    a = min(l)
    b = max(l)
    if (a % 3 ==0) | (b % 3 ==0):
        ans = 0
    else:
        anss = []
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 +1)) * (a//2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 2) * a
        s2 = (b - (b // 3 + 2)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3) - 1) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        ans = min(anss)

    print(ans)

main()

Submission status: Wrong Answer

Solution code:
def main():
    l = list(map(int, input().split()))
    a = min(l)
    b = max(l)
    if (a % 3 ==0) | (b % 3 ==0):
        ans = 0
    else:
        anss = []
        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 +1)) * (a//2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3 + 1)) * (a // 2)
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3) * a
        s2 = (b - (b // 3)) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        s1 = (b // 3 + 1) * a
        s2 = (b - (b // 3) - 1) // 2 * a
        s3 = a * b - s1 - s2
        anss.append(max(s1, s2, s3) - min(s1, s2, s3))

        ans = min(anss)

    print(ans)
main()

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split()))
a = min(l)
b = max(l)
if (a % 3 ==0) | (b % 3 ==0):
    ans = 0
else:
    anss = []
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 +1)) * (a//2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 + 1)) * (a // 2)
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) // 2 * a
    s3 = a * b - s1 - s2
    anss.append(max(s1, s2, s3) - min(s1, s2, s3))

    ans = min(anss)


print(ans)

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split()))
a = min(l)
b = max(l)
if (a % 3 ==0) | (b % 3 ==0):
    ans = 0
else:
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 +1)) * (a//2)
    s3 = a * b - s1 - s2
    ans1 = max(s1, s2, s3) - min(s1, s2, s3)

    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    ans2 = max(s1, s2, s3) - min(s1, s2, s3)

    ans = min(ans1, ans2)


print(ans)

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split()))
a = min(l)
b = max(l)
if (a % 3 ==0) | (b % 3 ==0):
    ans = 0
elif(b % 3 ==2):
    s1 = (b // 3 + 1) * a
    s2 = (b - (b // 3 +1)) * (a//2)
    s3 = a * b - s1 - s2
    ans = max(s1, s2, s3) - min(s1, s2, s3)

else:
    s1 = (b // 3) * a
    s2 = (b - (b // 3)) * (a // 2)
    s3 = a * b - s1 - s2
    ans = max(s1, s2, s3) - min(s1, s2, s3)


print(ans)

Submission status: Wrong Answer

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

# 3の倍数の場合
# if H * W % 3 == 0:
#     print(0)
# else:

# 縦長にする
H, W = max(H, W), min(H, W)

# |-パターン
BCh = 0
Bw, Cw = W // 2, W // 2
if W % 2 == 1:
    Bw += 1


def get_abs(BCh):
    """
    最大最小の面積の差
    """
    S_B = BCh * Bw
    S_C = BCh * Cw
    S_A = W * (H - BCh)  # max
    # print(S_A)
    return abs(max([S_A, S_B, S_C]) - min([S_A, S_B, S_C]))


# print(Bw)
err = get_abs(BCh)
# print(err)
for h in range(1, H):
    new_err = get_abs(h)
    if new_err < err:
        err = new_err
        # print(h, err)
    else:
        break

    # else:
    #     break

# ||パターン
if H > 2:
    Hmin = H // 3
    Hmax = H // 3 + 1

    err2 = (Hmax - Hmin) * W
else:
    err2 = 1

if err2 < err:
    err = err2
print(err)

Submission status: Wrong Answer

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

# 3の倍数の場合
if H * W % 3 == 0:
    print(0)
else:

    # 縦長にする
    H, W = max(H, W), min(H, W)

    # |-パターン
    BCh = 0
    Bw, Cw = W // 2, W // 2
    if W % 2 == 1:
        Bw += 1

    def get_abs(BCh):
        """
        最大最小の面積の差
        """
        S_B = BCh * Bw
        S_C = BCh * Cw
        S_A = W * (H - BCh)  # max
        # print(S_A)
        return abs(max([S_A, S_B, S_C]) - min([S_A, S_B, S_C]))

    # print(Bw)
    err = get_abs(BCh)
    # print(err)
    for h in range(1, H):
        new_err = get_abs(h)
        if new_err < err:
            err = new_err
            # print(h, err)
        else:
            break

        # else:
        #     break

    # ||パターン
    if H > 2:
        Hmin = H // 3
        Hmax = H // 3 + 1

        err2 = (Hmax - Hmin) * W
    else:
        err2 = 1

    if err2 < err:
        err = err2
    print(err)

Submission status: Wrong Answer

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

# 3の倍数の場合
if H * W % 3 == 0:
    print(0)
else:

    # 縦長にする
    H, W = max(H, W), min(H, W)

    # |-パターン
    BCh = 0
    Bw, Cw = W // 2, W // 2
    if W % 2 == 1:
        Bw += 1

    def get_abs(BCh):
        """
        最大最小の面積の差
        """
        S_B = BCh * Bw
        S_C = BCh * Cw
        S_A = W * (H - BCh)  # max
        # print(S_A)
        return abs(max([S_A, S_B, S_C]) - min([S_A, S_B, S_C]))

    # print(Bw)
    err = get_abs(BCh)
    # print(err)
    for h in range(1, H):
        new_err = get_abs(h)
        if new_err < err:
            err = new_err
            # print(h, err)
        else:
            break

        # else:
        #     break

    # ||パターン
    Hmin = H // 3
    Hmax = H // 3 + 1

    err2 = (Hmax - Hmin) * W
    if err2 < err:
        err = err2

    print(err)

Submission status: Wrong Answer

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

# 3の倍数の場合
if H * W % 3 == 0:
    print(0)
else:

    # 縦長にする
    H, W = max(H, W), min(H, W)

    # |-パターン
    BCh = 0
    Bw, Cw = W // 2, W // 2
    if W % 2 == 1:
        Bw += 1

    def get_abs(BCh):
        """
        最大最小の面積の差
        """
        S_B = BCh * Bw
        S_C = BCh * Cw
        S_A = W * (H - BCh)  # max
        # print(S_A)
        return abs(max([S_A, S_B, S_C]) - min([S_A, S_B, S_C]))

    # print(Bw)
    err = get_abs(BCh)
    # print(err)
    for h in range(1, H):
        new_err = get_abs(h)
        if new_err < err:
            err = new_err
            # print(h, err)
        else:
            break

        # else:
        #     break

    # ||パターン
    Hmin = H // 3
    Hmax = H // 3 + H % 3
    err2 = (Hmax - Hmin) * W
    if err2 < err:
        err = err2

    print(err)

Submission status: Wrong Answer

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

def bunkatsu(h, w):
    sa, sb, sc = 0, 0, 0
    wm = w // 2
    result = h * w

    for i in range(1, H):
        hm = (h-i) // 2
        sa = w * i

        sb = hm * w
        sc = (h - i - hm) * w
        smax = max(sa, sb, sc)
        smin = min(sa, sb, sc)
        result = min(result, smax - smin)

        sb = wm * (h - i)
        sc = (w - wm) * (h - i)
        smax = max(sa, sb, sc)
        smin = min(sa, sb, sc)
        result = min(result, smax - smin)

    return result

print(min(bunkatsu(H, W), bunkatsu(W, H)))

Submission status: Wrong Answer

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

def bunkatsu(h, w):
    sa, sb, sc = 0, 0, 0
    wm = w // 2 #小さい方
    result = 999999999999999999

    for i in range(1, H):
        hm = (h-i) // 2 #小さい方
        sa = w * i

        sb = hm * w
        sc = (h - i - hm) * w
        smax = max(sa, sb, sc)
        smin = min(sa, sb, sc)
        result = min(result, smax - smin)

        sb = wm * (h - i)
        sc = (w - wm) * (h - i)
        smax = max(sa, sb, sc)
        smin = min(sa, sb, sc)
        result = min(result, smax - smin)

    return result

print(min(bunkatsu(H, W), bunkatsu(W, H)))

Submission status: Wrong Answer

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

# 3の倍数の場合
if H * W % 3 == 0:
    print(0)
else:

    # 縦長にする
    H, W = max(H, W), min(H, W)

    BCh = 0
    Bw, Cw = W // 2, W // 2
    if W % 2 == 1:
        Bw += 1

    def get_abs(BCh):
        """
        最大最小の面積の差
        """
        S_B = BCh * Bw
        S_C = BCh * Cw
        S_A = W * (H - BCh)  # max
        # print(S_A)
        return abs(max([S_A, S_B, S_C]) - min([S_A, S_B, S_C]))

    # print(Bw)
    err = get_abs(BCh)
    # print(err)
    for h in range(1, H):
        new_err = get_abs(h)
        if new_err < err:
            err = new_err
            # print(h, err)
        else:
            break

        # else:
        #     break

    print(err)

Submission status: Wrong Answer

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

# 3の倍数の場合
if H * W % 3 == 0:
    print(0)
else:

    # 縦長にする
    H, W = max(H, W), min(H, W)

    BCh = 0
    Bw, Cw = W // 2, W // 2
    if W % 2 == 1:
        Bw += 1

    def get_abs(BCh):
        """
        最大最小の面積の差
        """
        S_B = BCh * Bw
        S_C = BCh * Cw
        S_A = W * (H - BCh)  # max
        # print(S_A)
        return abs(max([S_A, S_B, S_C]) - min([S_A, S_B, S_C]))

    # print(Bw)
    err = get_abs(BCh)
    # print(err)
    for h in range(H):
        new_err = get_abs(h)
        if new_err < err:
            err = new_err
            # print(h, err)

        # else:
        #     break

    print(err)

Submission status: Wrong Answer

Solution code:
h, w = [int(i) for i in input().split()]
# h%3=0 or w%3=0なら出力0
# それ以外の場合では
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
minDiff = h*w
for i in range(1, h):
    s1 = w*i
    s2 = int(w/2)*(h-i)
    s3 = (w-int(w/2))*(h-i)
    if s1 > 0 and s2 > 0 and s3 > 0:
        minS = min(s1, min(s2, s3))
        maxS = max(s1, max(s2, s3))
        minDiff = min(minDiff, abs(maxS - minS))
for i in range(1, w):
    s1 = h*i
    s2 = int(h/2)*(w-i)
    s3 = (h-int(h/2))*(w-i)
    if s1 > 0 and s2 > 0 and s3 > 0:
        minS = min(s1, min(s2, s3))
        maxS = max(s1, max(s2, s3))
        minDiff = min(minDiff, abs(maxS - minS))
print(minDiff)

Submission status: Wrong Answer

Solution code:
h, w = [int(i) for i in input().split()]
# h%3=0 or w%3=0なら出力0
# それ以外の場合では
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
minDiff = h*w
for i in range(1, h):
    s1 = w*i
    s2 = int(w/2)*(h-i)
    s3 = (w-int(w/2))*(h-i)
    minS = min(s1, min(s2, s3))
    maxS = max(s1, max(s2, s3))
    minDiff = min(minDiff, abs(maxS - minS))
for i in range(1, w):
    s1 = h*i
    s2 = int(h/2)*(w-i)
    s3 = (h-int(h/2))*(w-i)
    minS = min(s1, min(s2, s3))
    maxS = max(s1, max(s2, s3))
    minDiff = min(minDiff, abs(maxS - minS))
print(minDiff)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(1,h):
    a=max(i*w,(h-i)*((w+1)//2))
    b=min(i*w,(h-i)*(w//2))
    s.append(abs(a-b))
    a=max(i*w,w*(h-i+1)//2)
    b=min(i*w,w*(h-i)//2)
    s.append(abs(a-b))
for j in range(1,w):
    a=max(h*j,(h+1)//2*(w-j))
    b=min(h*j,(h//2)*(w-j))
    s.append(abs(a-b))
    a=max(h*j,h*(w-j+1)//2)
    b=min(h*j,h*(w-j)//2)
print(min(s))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(1,h):
    a=max(i*w,(h-i)*(w//2))
    b=min(i*w,(h-i)*(w//2))
    s.append(abs(a-b))
    a=max(i*w,w*(h-i)//2)
    b=min(i*w,w*(h-i)//2)
    s.append(abs(a-b))
for j in range(1,w):
    a=max(h*j,(h//2)*(w-j))
    b=min(h*j,(h//2)*(w-j))
    s.append(abs(a-b))
    a=max(h*j,h*(w-j)//2)
    b=min(h*j,h*(w-j)//2)
print(min(s))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(1,h):
    a=max(i*w,((h-i)*(w//2)),w*(h-i)//2)
    b=min(i*w,((h-i)*(w//2)),w*(h-i)//2)
    s.append((abs(a-b)))
for j in range(1,w):
    a=max(h*j,(h//2)*(w-j),h*((w-j)//2))
    b=min(h*j,(h//2)*(w-j),h*((w-j)//2))
    s.append(abs(a-b))
print(min(s))

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==1 and w%2==1:
    a=max(h,w)//3
    b=min(h,w)
    print(min(abs((a+1)*b-(b//2)*(max(h,w)-a-1)),abs((max(a*b,(b//2+1)*(max(h,w)-a)))-(b//2)*(max(h,w)-a))))
elif h%2==0 and w%2==0:
    a=max(h,w)//3
    b=min(h,w)
    print(min(abs((a+1)*b-(b//2)*(max(h,w)-a-1)),abs((a*b)-(b//2)*(max(h,w)-a))))
elif h%2==0 and h<w:
    a=w//3
    b=h
    print(min(abs((a+1)*b-(b//2)*(max(h,w)-a-1)),abs((a*b)-(b//2)*(max(h,w)-a))))
else:
    a=h//3
    b=w
    print(min(abs((a+1)*b-(b//2)*(h-a-1)),abs((a*b)-(b//2)*(max(h,w)-a))))

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

Submission status: Wrong Answer

Solution code:
import math

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

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

min_num = 100000000000
for i in range(1, H):
    a = i*W
    b = W//2 * (H-i)
    c = math.ceil(W/2) * (H-i)
    _max_num = max(a, max(b, c))
    _min_num = min(a, min(b, c))
    n = _max_num - _min_num
    if n < min_num:
        min_num = n

for i in range(1, W):
    a = i*H
    b = H//2 * (W-i)
    c = math.ceil(H/2) * (W-i)
    _max_num = max(a, max(b, c))
    _min_num = min(a, min(b, c))
    n = _max_num - _min_num
    if n < min_num:
        min_num = n

print(min_num)

Submission status: Wrong Answer

Solution code:
import time

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

def calc_ans(H,W):
    ans = float('inf')
    #ブロックAを横に分割
    halfed_w = W//2
    for h in range(1,H):
        a  = h*W
        #横分け
        b1 = (H-h)//2*W
        c1 = (H-h-(H-h)//2)*W
        #縦で分けた面積
        b2 = (H-h)*halfed_w
        c2 = (H-h)*(W-halfed_w)
        result1 = max(a,b1,c1)-min(a,b1,c1)
        result2 = max(a,b2,c2)-min(a,b2,c2)
        ans = min(ans,result1,result2)
    #ブロックAを縦に分割
    halfed_h = H//2
    for w in range(1,W):
        a = w*H
        #横分け
        b1 = (W-w)//2*H
        c1 = (W-w-(W-w)//2)*H
        #縦分け
        b2 = (W-w)*halfed_h
        c2 = (W-w)*(H-halfed_h)
        result1 = max(a,b1,c1)-min(a,b1,c1)
        result2 = max(a,b2,c2)-min(a,b2,c2)
        ans = min(ans,result1,result2)
    print(ans)

start = time.time()
calc_ans(H,W)

elapsed_time = time.time()-start
print('elapsed_time:{0}' .format(elapsed_time)+'[sec]')

Submission status: Wrong Answer

Solution code:
def area(H,W):
    
    ans1 = H*W
    ans2 = H*W
    for i in range(1,H):
        A = i*W
        B1 = ((H-i)//2) * W
        B2 = (H-i)*(W//2)
        
        if B1 != 0:
            area1 = abs(A-B1)
        else:
            continue
        
        if B2 != 0:
            area2 = abs(A-B2)
        else:
            continue
        
        if area1 < ans1:
            ans1 = area1
        if area2 < ans2:
            ans2 = area2
        
    
    return min(ans1,ans2)


H,W = (int(i) for i in input().split())

ans = min(area(H,W),area(W,H))

print(ans)

Submission status: Wrong Answer

Solution code:
#062-C
h,w=map(int,input().split())
ans=float('inf')
for i in range(1,h+1):
    a=i*w
    b1=(h-i)//2*w
    c1=(h-i-(h-i)//2)*w
    b2=(h-i)*w//2
    c2=(h-i)*(w-w//2)
    can1=max(a,b1,c1)-min(a,b1,c1)
    can2=max(a,b2,c2)-min(a,b2,c2)
    ans=min(ans,can1,can2)
    
for i in range(1,w+1):
    a=i*h
    b1=(w-i)//2*h
    c1=(w-i-(w-i)//2)*h
    b2=(w-i)*h//2
    c2=(w-i)*(h-h//2)
    can1=max(a,b1,c1)-min(a,b1,c1)
    can2=max(a,b2,c2)-min(a,b2,c2)
    ans=min(ans,can1,can2)
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=float('inf')
if h%3==0 or w%3==0:
    ans=0
elif h%2==0 and w%2==0:
    ans=min(h//2,w//2)
elif w%2==0:
    ans=w//2
elif h%2==0:
    ans=h//2
else:
    ans=min(abs(h//2*w-w//2*(h-h//2)),abs(w//2*h-h//2*(w-w//2)))
print(ans)

Submission status: Wrong Answer

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

h,w=map(int,input().split())
ans=h*w
if h*w%3==0:
    print(0)
else:
    for x,y in ((h,w),(w,h)):
        x1=x//3
        x2=(x+2)//3
        y1=y//2
        y2=(y+1)//2
        ans=min(ans,abs(y*x2-y*x1))
        ans=min(ans,abs(y*x2-(x-x2)*(y-y2)))
        ans=min(ans,abs(y*x1-(x-x1)*(y-y2)))
    print(ans)

Submission status: Wrong Answer

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

h,w=map(int,input().split())
ans=h*w
if h*w%3==0:
    print(0)
else:
    for x,y in ((h,w),(w,h)):
        x1=x//3
        x2=(x+2)//3
        y1=y//2
        y2=(y+1)//2
        ans=min(ans,abs(y*x2-y*x1))
        ans=min(ans,abs(y*x2-(x-x2)*(y-y2)))
        ans=min(ans,abs(y*x1-(x-x1)*(y-y2)))
print(ans)

Submission status: Wrong Answer

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

h,w=map(int,input().split())
ans=h*w
if h*w%3==0:
    print(0)
else:
    x=[h*(w//3),h//2*(w-(w//3)),(h-(h//2))*(w-(w//3))]
    ans=min(ans,max(x)-min(x))

    x=[w*(h//3),w//2*(h-(h//3)),(w-(w//2))*(h-(h//3))]
    ans=min(ans,max(x)-min(x))

    x=[h*(w-(w//3)),h//2*(w//3),(h-(h//2))*(w//3)]
    ans=min(ans,max(x)-min(x))

    x=[w*(h-(h//3)),w//2*(h//3),(w-(w//2))*(h//3)]
    ans=min(ans,max(x)-min(x))
    #x=[abs((h2//3*w)-(h-(h2//3))*(w//2)),abs(((w2//3*h)-(w-(w2//3))*(h//2)))]

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

Submission status: Wrong Answer

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

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

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