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


def Choco(H,W,ans):
    if H%3==0 or W%3==0:
        print("0")
        exit()
    else:
        for i in range(H):
            if i==0:
                pass
            else:
                a=W*i
                if (H-i)%2==0:
                    b=(H-i)*W/2
                    dif=abs(a-b)
                else:
                    w1=W//2
                    b=(H-i)*w1
                    c=(H-i)*(W-w1)
                    M=max(a,b,c)
                    m=min(a,b,c)
                    dif=M-m
                ans=min(dif,ans)
    return ans

ans=10**5
ans=Choco(H,W,ans)
ans=Choco(W,H,ans)

print(ans)

Submission status: Wrong Answer

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


def Choco(H,W,ans):
    if H%3==0 or W%3==0:
        print("0")
        exit()
    else:
        for i in range(H):
            a=W*i
            if (H-i)%2==0:
                b=(H-i)*W/2
                dif=abs(a-b)
            else:
                w1=W//2
                b=(H-i)*w1
                c=(H-i)*(W-w1)
                M=max(a,b,c)
                m=min(a,b,c)
                dif=M-m
            ans=min(dif,ans)
    return ans

ans=10**5
ans=Choco(H,W,ans)
ans=Choco(W,H,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
l =[]
m =[]
if H%3==0 or W%3==0:
    print(0)
else:
  for i in range(H):
          a1 = W//2*i
          b1 = (W+1)//2*i
          c1 = W*(H-i)
          mx1= max(a1,b1,c1)
          mn1= min(a1,b1,c1)
          d1 = mx1-mn1
          l.append(d1)
  for j in range(W):
          a2 = H//2*j
          b2 = (H+1)//2*j
          c2 = H*(W-j)
          mx2 = max(a2,b2,c2)
          mn2 = min(a2,b2,c2)
          d2 = mx2-mn2
          m.append(d2)
  print(min(min(l),min(m)))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
l =[]
import math

if H%3==0:
    print(0)
elif W%3==0:
    print(0)
else:
  for i in range(H):
          a1 = math.floor(W/2)*i
          b1 = math.ceil(W/2)*i
          c1 = W*(H-i)
          mx1= max(a1,b1,c1)
          mn1= min(a1,b1,c1)
          d1 = mx1-mn1
          l.append(d1)
  for j in range(W):
          a2 = math.floor(H/2)*j
          b2 = math.ceil(H/2)*j
          c2 = H*(W-j)
          mx2 = max(a2,b2,c2)
          mn2 = min(a2,b2,c2)
          d2 = mx2-mn2
          l.append(d2)
  print(min(l))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
l =[]
import math

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

else:
  for i in range(H):
          a1 = math.floor(W/2)*i
          b1 = math.ceil(W/2)*i
          c1 = W*(H-i)
          mx1= max(a1,b1,c1)
          mn1= min(a1,b1,c1)
          l.append(mx1-mn1)
  for i in range(W):
          a2 = math.floor(H/2)*i
          b2 = math.ceil(H/2)*i
          c2 = H*(W-i)
          mx2 = max(a2,b2,c2)
          mn2 = min(a2,b2,c2)
          l.append(mx2-mn2)
  print(min(l))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

SAIDAI = 2*100000

# 横３分割
def yoko3(h, w):
    ans = SAIDAI
    for i in range(1, h-1):
        h1 = i
        h2 = (h-h1)/2.0
        h3 = (h-h1)/2.0
        if (h-h1) % 2 != 0:
            h2 = h2 + 0.5
            h3 = h3 - 0.5
        sfc = [h1*w, h2*w, h3*w]
        sa = max(sfc) - min(sfc)

        if sa < ans:
            ans = sa
    return int(ans)



# 横２分割、縦１分割
def yoko2(h, w):
    ans = SAIDAI
    for i in range(1, h-1):
        h1 = i
        h2 =h-h1

        w1 = w/2.0
        w2 = w/2.0
        if (w % 2) != 0:
            w1 = w1 + 0.5
            w2 = w2 - 0.5
        sfc = [h1*w, h2*w1, h2*w2]
        sa = max(sfc) - min(sfc)

        if sa < ans:
            ans = sa
    return int(ans)

h, w = map(int, raw_input().split())
sa_list = [yoko3(h,w), yoko2(h,w), yoko3(w,h), yoko2(w,h)]
print min(sa_list)

Submission status: Wrong Answer

Solution code:
lim = 100000


def is_prime(x):
    if x < 2:
        return False
    for i in range(2, x):
        if x % i == 0:
            return False
    return True


def check_rect(x, height, width):
    return True if (is_prime(x) and (x < height or x < width)) or not is_prime(x) else False


def main(h, w):
    s = h * w
    peace = [int(s / 3) for i in range(3)]
    mod = s % 3

    if mod == 2:
        peace[0] += 1
        peace[1] += 1
    elif mod == 1:
        peace[0] += 1


    lim_2 = int(lim / 2)
    a = lim_2
    b = 0
    for hi in range(h, s, h):
        if abs(peace[0] - hi) < a:
            a = abs(peace[0] - hi)
            b = hi
    for wi in range(w, s, w):
        if abs(peace[0] - wi) < a:
            a = abs(peace[0] - wi)
            b = wi

    peace[0] = b
    ss = s - b
    if ss % 2 == 0:
        peace[1], peace[2] = [int(ss / 2) for i in range(2)]
    else:
        peace[1] = int(ss / 2) + 1
        peace[2] = int(ss / 2) - 1

    return max(peace) - min(peace)


foo, bar = list(map(int, input().split()))
print(main(foo, bar))

Submission status: Wrong Answer

Solution code:
import math

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

def func(h,w,ans):
    for i in range(1,h+1):
        a = i * w 
        w1 = int(w/2)
        b = (h-i)*w1
        c = (h-i)*(w-w1)
        M = max(a,b,c)
        m = min(a,b,c)
        s = M-m
        ans = min(s,ans)

        h1 = int(h/3)
        a = h1*w
        c = (h-2*h1)*w
        s = abs(a-c)
        ans = min(s,ans)
    return ans

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

ans = float('inf')

ans = func(H,W,ans)
ans = func(W,H,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

def func(h,w,ans):
    for i in range(1,h+1):
        a = i * w 
        w1 = int(w/2)
        b = (h-i)*w1
        c = (h-i)*(w-w1)
        M = max(a,b,c)
        m = min(a,b,c)
        s = M-m
        ans = min(s,ans) 
    return ans

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

ans = float('inf')

ans = func(H,W,ans)
ans = func(W,H,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

def func(h,w,ans):
    for i in range(1,h+1):
        a = i * w 
        w1 = int(w/2)
        b = (H-i)*w1
        c = (H-i)*(w-w1)
        M = max(a,b,c)
        m = min(a,b,c)
        s = M-m
        ans = min(s,ans) 
    return ans

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

ans = float('inf')

ans = func(H,W,ans)
ans = func(W,H,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import os
import sys
import math

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

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

ans = float('inf')

for i in range(1, w + 1):
    first = h * i

    if (w - i) % 2 == 0:
        second = h * ((w - i) // 2)
        s = abs(first - second)
        ans = min(s, ans)
        # print(ans)        
    else:
        second = h * ((w - i) // 2)
        third = h * (w - i - (w - i) // 2)
        s = max(abs(first - second), abs(second - third), abs(third - first))
        ans = min(s, ans)
        # print(ans)        

    if h % 2 == 0:
        second = (h // 2) * (w - i)
        s = abs(first - second)
        ans = min(s, ans)
        # print(ans)        
        
    else:
        second = (h // 2) * (w - i)
        third = (h - (h // 2)) * (w - i)
        s = max(abs(first - second), abs(second - third), abs(third - first))
        ans = min(s, ans)
        # print(ans)        


if h > w:
    h, w = w, h

for i in range(1, w + 1):
    first = h * i

    if (w - i) % 2 == 0:
        second = h * ((w - i) // 2)
        s = abs(first - second)
        ans = min(s, ans)
        # print(ans)                
    else:
        second = h * ((w - i) // 2)
        third = h * (w - i - (w - i) // 2)
        s = max(abs(first - second), abs(second - third), abs(third - first))
        ans = min(s, ans)
        # print(ans)                

    if h % 2 == 0:
        second = (h // 2) * (w - i)
        s = abs(first - second)
        ans = min(s, ans)
        # print(ans)                
    else:
        second = (h // 2) * (w - i)
        third = (h - (h // 2)) * (w - i)
        s = max(abs(first - second), abs(second - third), abs(third - first))
        ans = min(s, ans)
        # print(ans)        
        
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys
 
T = list(map(int, input().split()))
S = T[0] * T[1]
 
if ((T[0] % 3 == 0) or (T[1] % 3 == 0)):
    print(0)
    sys.exit()
 
minS = 1000000000000
for i in range(1, math.ceil(T[0]/2)):
    A = i * T[1]
    F = math.floor(T[1] / 2) * (T[0] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[1]
    F = math.floor((T[0] - i) / 2) * T[1]
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
 
for i in range(1, math.ceil(T[1]/2)):
    A = i * T[0]
    F = math.floor(T[0] / 2) * (T[1] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[0]
    F = math.floor((T[1] - i) / 2) * T[0]
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
 
print(minS)

Submission status: Wrong Answer

Solution code:
import math
import sys

T = list(map(int, input().split()))
S = T[0] * T[1]

if ((T[0] % 3 == 0) or (T[1] % 3 == 0)):
    print(0)
    sys.exit()

minS = 100000000000
#T[0]からせめる
for i in range(1, math.ceil(T[0]/2)):
    A = i * T[1]
    F = math.floor(T[1] / 2) * (T[0] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[1]
    F = math.floor((T[0] - i) / 2) * T[1]
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))

for i in range(1, math.ceil(T[1]/2)):
    A = i * T[0]
    F = math.floor(T[0] / 2) * (T[1] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[0]
    F = math.floor((T[1] - i) / 2) * T[0]
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))

print(minS)

Submission status: Wrong Answer

Solution code:
import math
import sys
 
T = list(map(int, input().split()))
S = T[0] * T[1]
 
if ((T[0] % 3 == 0) or (T[1] % 3 == 0)):
    print(0)
    sys.exit()
 
minS = 100000000000
#T[0]からせめる
for i in range(1, math.ceil(T[0]/2)):
    A = i * T[1]
    F = math.floor(T[1] / 2) * (T[0] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[0]
    F = math.floor(T[0] / 2) * (T[1] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
 
for i in range(1, math.ceil(T[1]/2)):
    A = i * T[1]
    F = math.floor(T[1] / 2) * (T[0] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[0]
    F = math.floor(T[0] / 2) * (T[1] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
 
print(minS)

Submission status: Wrong Answer

Solution code:
import math
import sys
 
T = list(map(int, input().split()))
S = T[0] * T[1]
 
if ((T[0] % 3 == 0) or (T[1] % 3 == 0)):
    print(0)
    sys.exit()
 
minS = 100000000
#T[0]からせめる
for i in range(1, math.ceil(T[0]/2)):
    A = i * T[1]
    F = math.floor(T[1] / 2) * (T[0] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[0]
    F = math.floor(T[0] / 2) * (T[1] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
 
for i in range(1, math.ceil(T[1]/2)):
    A = i * T[1]
    F = math.floor(T[1] / 2) * (T[0] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
    A = i * T[0]
    F = math.floor(T[0] / 2) * (T[1] - i)
    C = S - (A + F)
    if (F == C):
        minS = min(minS, abs(A - F))
        continue
    else:
        if (A > C):
            minS = min(minS, abs(A - F))
        elif(C >= A and A >= F):
            minS = min(minS, abs(C - F))
        else:
            minS = min(minS, abs(C - A))
 
print(minS)

Submission status: Wrong Answer

Solution code:
def check1(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * W1
    res2 = H1 * W2
    res3 = H2 * (W1 + W2)
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check2(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * (W1 + W2)
    res2 = H2 * (W1)
    res3 = H2 * W2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check3(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W1 * (H1 + H2)
    res2 = W2 * H1
    res3 = W2 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check4(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W2 * (H1 + H2)
    res2 = W1 * H1
    res3 = W1 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check_s(tmp1, tmp2, tmp3):
    S_min = 2 ** 50
    S_max = 0
    S_min = min(S_min, tmp1, tmp2, tmp3)
    S_max = max(S_max, tmp1, tmp2, tmp3)
    return S_max - S_min

if __name__ == "__main__":
    H,W = map(int, input().split())
    if (H > W):
        H,W = W,H

    H1 = H // 2
    H2 = H - H1
    W1 = W // 2
    W2 = W - W1
    result = 2 ** 50
    result = min(result, check1(H1, H2, W1, W2))
    result = min(result, check2(H1, H2, W1, W2))
    result = min(result, check3(H1, H2, W1, W2))
    result = min(result, check4(H1, H2, W1, W2))

    for x in range(1, W + 1):
        H1 = H // 2
        H2 = H - H1

        W1 = x
        W2 = W - W1

        W3 = W2 // 2
        W4 = W2 - W3

        tmp1 = W1 * H
        tmp2 = W3 * H
        tmp3 = W4 * H

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = W1 * H
        tmp2 = W2 * H1
        tmp3 = W2 * H2
        result = min(result, check_s(tmp1, tmp2, tmp3))

    for y in range(1,  H+ 1):
        H1 = y
        H2 = H - H1

        H3 = H2 // 2
        H4 = H3 - H2

        W1 = W // 2
        W2 = W - W1


        tmp1 = H1 * W
        tmp2 = H3 * W
        tmp3 = H4 * W

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = H1 * W
        tmp2 = H2 * W1
        tmp3 = H2 * W2
        result = min(result, check_s(tmp1, tmp2, tmp3))


    print (result)

Submission status: Wrong Answer

Solution code:
def check1(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * W1
    res2 = H1 * W2
    res3 = H2 * (W1 + W2)
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check2(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * (W1 + W2)
    res2 = H2 * (W1)
    res3 = H2 * W2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check3(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W1 * (H1 + H2)
    res2 = W2 * H1
    res3 = W2 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check4(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W2 * (H1 + H2)
    res2 = W1 * H1
    res3 = W1 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check_s(tmp1, tmp2, tmp3):
    S_min = 2 ** 50
    S_max = 0
    S_min = min(S_min, tmp1, tmp2, tmp3)
    S_max = max(S_max, tmp1, tmp2, tmp3)
    return S_max - S_min

if __name__ == "__main__":
    H,W = map(int, input().split())
    if (H > W):
        H,W = W,H
    if (H % 3 == 0 or W % 3 == 0):
        print (0)
        exit()
    if (H == 2 and W == 2):
        print (1)
    H1 = H // 2
    H2 = H - H1
    W1 = W // 2
    W2 = W - W1
    result = 2 ** 50
    result = min(result, check1(H1, H2, W1, W2))
    result = min(result, check2(H1, H2, W1, W2))
    result = min(result, check3(H1, H2, W1, W2))
    result = min(result, check4(H1, H2, W1, W2))

    for x in range(1, W + 1):
        H1 = H // 2
        H2 = H - H1

        W1 = x
        W2 = W - W1

        W3 = W2 // 2
        W4 = W2 - W3

        tmp1 = W1 * H
        tmp2 = W3 * H
        tmp3 = W4 * H

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = W1 * H
        tmp2 = W2 * H1
        tmp3 = W2 * H2
        result = min(result, check_s(tmp1, tmp2, tmp3))

    for y in range(1,  H+ 1):
        H1 = y
        H2 = H - H1

        H3 = H2 // 2
        H4 = H3 - H2

        W1 = W // 2
        W2 = W - W1


        tmp1 = H1 * W
        tmp2 = H3 * W
        tmp3 = H4 * W

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = H1 * W
        tmp2 = H2 * W1
        tmp3 = H2 * W2
        result = min(result, check_s(tmp1, tmp2, tmp3))


    print (result)

Submission status: Wrong Answer

Solution code:
def check1(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * W1
    res2 = H1 * W2
    res3 = H2 * (W1 + W2)
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check2(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * (W1 + W2)
    res2 = H2 * (W1)
    res3 = H2 * W2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check3(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W1 * (H1 + H2)
    res2 = W2 * H1
    res3 = W2 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check4(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W2 * (H1 + H2)
    res2 = W1 * H1
    res3 = W1 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check_s(tmp1, tmp2, tmp3):
    S_min = 2 ** 50
    S_max = 0
    S_min = min(S_min, tmp1, tmp2, tmp3)
    S_max = max(S_max, tmp1, tmp2, tmp3)
    return S_max - S_min

if __name__ == "__main__":
    H,W = map(int, input().split())
    if (H > W):
        H,W = W,H
    if (H % 3 == 0 or W % 3 == 0):
        print (0)
        exit()
    if (H == 2 and W == 2):
        print (1)
    H1 = H // 2
    H2 = H - H1
    W1 = W // 2
    W2 = W - W1
    result = 2 ** 50
    result = min(result, check1(H1, H2, W1, W2))
    result = min(result, check2(H1, H2, W1, W2))
    result = min(result, check3(H1, H2, W1, W2))
    result = min(result, check4(H1, H2, W1, W2))

    for x in range(1, W + 1):
        H1 = H // 2
        H2 = H - H1

        W1 = x
        W2 = W - W1

        W3 = W2 // 2
        W4 = W2 - W3

        tmp1 = W1 * H
        tmp2 = W3 * H
        tmp3 = W4 * H

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = W1 * H
        tmp2 = W2 * H1
        tmp3 = W2 * H2
        result = min(result, check_s(tmp1, tmp2, tmp3))

    for y in range(1,  H+ 1):
        H1 = y
        H2 = H - H1

        H3 = H2 // 2
        H4 = H3 - H2

        W1 = W // 2
        W2 = W - W1


        tmp1 = H1 * W
        tmp2 = H3 * W
        tmp3 = H4 * W

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = W1 * H
        tmp2 = W2 * H1
        tmp3 = W2 * H2
        result = min(result, check_s(tmp1, tmp2, tmp3))


    print (result)

Submission status: Wrong Answer

Solution code:
def check1(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * W1
    res2 = H1 * W2
    res3 = H2 * (W1 + W2)
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check2(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * (W1 + W2)
    res2 = H2 * (W1)
    res3 = H2 * W2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check3(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W1 * (H1 + H2)
    res2 = W2 * H1
    res3 = W2 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check4(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W2 * (H1 + H2)
    res2 = W1 * H1
    res3 = W1 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check_s(tmp1, tmp2, tmp3):
    S_min = 2 ** 50
    S_max = 0
    S_min = min(S_min, tmp1, tmp2, tmp3)
    S_max = max(S_max, tmp1, tmp2, tmp3)
    return S_max - S_min

if __name__ == "__main__":
    H,W = map(int, input().split())
    if (H > W):
        H,W = W,H
    result = 2 ** 50
    for x in range(1, W + 1):
        H1 = H // 2
        H2 = H - H1

        W1 = x
        W2 = W - W1

        W3 = W2 // 2
        W4 = W2 - W3

        tmp1 = W1 * H
        tmp2 = W3 * H
        tmp3 = W4 * H

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = W1 * H
        tmp2 = W2 * H1
        tmp3 = W2 * H2
        result = min(result, check_s(tmp1, tmp2, tmp3))

    

    print (result)

Submission status: Wrong Answer

Solution code:
def check1(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * W1
    res2 = H1 * W2
    res3 = H2 * (W1 + W2)
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check2(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = H1 * (W1 + W2)
    res2 = H2 * (W1)
    res3 = H2 * W2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check3(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W1 * (H1 + H2)
    res2 = W2 * H1
    res3 = W2 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check4(H1, H2, W1, W2):
    S_min = 2 ** 50
    S_max = 0
    res1 = W2 * (H1 + H2)
    res2 = W1 * H1
    res3 = W1 * H2
    S_min = min(S_min, res1)
    S_min = min(S_min, res2)
    S_min = min(S_min, res3)
    S_max = max(S_max, res1)
    S_max = max(S_max, res2)
    S_max = max(S_max, res3)
    return S_max - S_min

def check_s(tmp1, tmp2, tmp3):
    S_min = 2 ** 50
    S_max = 0
    S_min = min(S_min, tmp1, tmp2, tmp3)
    S_max = max(S_max, tmp1, tmp2, tmp3)
    return S_max - S_min

if __name__ == "__main__":
    H,W = map(int, input().split())
    if (H > W):
        H,W = W,H
    if (H % 3 == 0 or W % 3 == 0):
        print (0)
        exit()
    if (H == 2 and W == 2):
        print (1)
    H1 = H // 2
    H2 = H - H1
    W1 = W // 2
    W2 = W - W1
    result = 2 ** 50
    result = min(result, check1(H1, H2, W1, W2))
    result = min(result, check2(H1, H2, W1, W2))
    result = min(result, check3(H1, H2, W1, W2))
    result = min(result, check4(H1, H2, W1, W2))

    for x in range(1, W + 1):
        H1 = H // 2
        H2 = H - H1

        W1 = x
        W2 = W - W1

        W3 = W2 // 2
        W4 = W2 - W3

        tmp1 = W1 * H
        tmp2 = W3 * H
        tmp3 = W4 * H

        result = min(result, check_s(tmp1, tmp2, tmp3))
        tmp1 = W1 * H
        tmp2 = W2 * H1
        tmp3 = W2 * H2
        result = min(result, check_s(tmp1, tmp2, tmp3))

    print (result)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
#横長パターンと縦長パターンのみ
ans=H*W
if H%3==0 or W%3==0:
  ans=0

#横長パターン
for h in range(H):
  A=h*W
  if W%2==0:
    B=(H-h)*(W/2)
    C=(H-h)*(W/2)
  else:
    B=(H-h)*(W+1)/2
    C=(H-h)*(W-1)/2
  sa=abs(A-B)
  sa=max(sa,abs(B-C),abs(A-C))
  ans=min(ans, sa)
#縦長パターン
for w in range(W):
  A=w*H
  if H%2==0:
    B=(W-w)*(H/2)
    C=(W-w)*(H/2)
  else:
    B=(W-w)*(H+1)/2
    C=(W-w)*(H-1)/2
  sa=abs(A-B)
  sa=max(sa,abs(B-C),abs(A-C))
  ans=min(ans, sa)
print(int(ans))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
#横長パターンと縦長パターンのみ
ans=H*W
if H%3==0 or W%3==0:
  ans=0

#横長パターン
for h in range(H):
  A=h*W
  if W%2==0:
    B=(H-h)*(W/2)
    C=(H-h)*(W/2)
  else:
    B=(H-h)*(W+1)/2
    C=(H-h)*(W-1)/2
  sa=abs(A-B)
  sa=max(sa,abs(B-C),abs(A-C))
  ans=min(ans, sa)
#縦長パターン
for w in range(W):
  A=H*w
  if H%2==0:
    B=(W-w)*(h/2)
    C=(W-w)*(h/2)
  else:
    B=(W-w)*(H+1)/2
    C=(W-w)*(H-1)/2
  sa=abs(A-B)
  sa=max(sa,abs(B-C),abs(A-C))
  ans=min(ans, sa)
print(int(ans))

Submission status: Wrong Answer

Solution code:
h, w = [int(n) for n in input().split()]
i = min(h, w)
j = max(h, w)
area = i * j
if i % 3 == 0 or j % 3 == 0:
    print(0)
else:
    lst = []
    for r, s in [(i, j), (j, i)]:
        p = s // 3 + 1
        q = s // 3 
        a1 = r * p
        a2 = r * q
        b1 = r // 2 * (s - p)
        b2 = r // 2 * (s - q)
#         c1 = (s - p) // 2 * r
#         c2 = (s - q) // 2 * r
        tup = [(a1, b1), (a2, b2)
#                , (a1, c1), (a2, c2)
              ]
        lst.extend([abs(max(a, b, area - a - b) - min(a, b, area - a - b))
                    for a, b in tup])
    print(min(lst))

Submission status: Wrong Answer

Solution code:
h, w = [int(n) for n in input().split()]


def max_diff(list):
    l0 = list[0]
    l1 = list[1]
    l2 = list[2]
    max_diff = max([abs(l0 - l1), abs(l0 - l2), abs(l1 - l2)])
    return max_diff


def min_square(h, w):
    w3 = w // 3
    w2 = w // 2
    h3 = h // 3
    h2 = h // 2
    w_3 = [w3, w3 + 1]
    w_2 = [w2, w2 + 1]
    h_3 = [h3, h3 + 1]
    h_2 = [h2, h2 + 1]
    min_list = []
    for k in w_3:
        for l in h_2:
            if 0 < k < w and 0 < l < h:
                min_list.append(max_diff([h * k, l * (w - k), (h - l) * (w - k)]))
    for k in w_2:
        for l in h_3:
            if 0 < k < w and 0 < l < h:
                min_list.append(max_diff([w * l, k * (h - l), (w - k) * (h - l)]))

    return min(min_list)


if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    print(min_square(h, w))