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 = (int(i) for i in input().split())
sel_row1 = 10000
sel_row2 = 10000
sel_col1 = 10000
sel_col2 = 10000
sel_row3 = 10000
sel_col3 = 10000
if h&1:
    if h%3 == 0:
        sel_row1 = h/3
        sel_row2 = h/3
    else:
        sel_row1 = h//3
        sel_row2 = sel_row1 +1
else:
    sel_row1 = h//3 if h//3 != 0 else 10000
    sel_row2 = sel_row1 + 1
    sel_row3 = h//2
if w&1:
    if w%3 == 0:
        sel_col1 = w/3
        sel_col2 = w/3
    else:
        sel_col1 = w//3
        sel_col2 = sel_col1 + 1
else:
    sel_col1 = w//3 if w//3 != 0 else 10000
    sel_col2 = sel_col1 + 1
    sel_col3 = w//2

ans = set()
if h%2 == 0 and w%2 == 0:
    ans.add(abs((sel_row3 * sel_col3) - (h * sel_col3)))
    ans.add(abs((h*sel_col1) - (h*sel_col2)))
    ans.add(abs((sel_row1*w) - (sel_row2*w))) 
    ans.add(abs((sel_row3*(w-sel_col1) - h*sel_col1)))
    ans.add(abs((sel_row3*(w-sel_col2) - h*sel_col2)))
    ans.add(abs((sel_col3*(h-sel_row1) - w*sel_row1)))
    ans.add(abs((sel_col3*(h-sel_row2) - w*sel_row2)))
    print(min(ans))
elif h%2 == 0:
    ans1 = abs(sel_col1*h - (w-sel_col1)*sel_row1)
    ans2 = abs(sel_col2*h - (w-sel_col2)*sel_row1)
    print(min(ans1,ans2))
elif w%2 == 0:
    ans1 = abs(sel_row1*w - (h-sel_row1)*sel_col1)
    ans2 = abs(sel_row2*w - (h-sel_row2)*sel_col1)
    print(min(ans1,ans2))
else:
    ans1 = abs(sel_row1*w - (h-sel_row2)*w)
    ans2 = abs(sel_col1*h - (w-sel_col2)*h)
    ans3_set = {sel_row1*(w-sel_col1),(h-sel_row1)*(w-sel_col1),h*sel_col1}
    ans3 = max(ans3_set) - min(ans3_set)
    ans4_set = {sel_row1*(w-sel_col2),(h-sel_row1)*(w-sel_col2),h*sel_col2}
    ans4 = max(ans4_set) - min(ans4_set)
    ans5_set = {sel_row2*(w-sel_col1),(h-sel_row2)*(w-sel_col1),h*sel_col1}
    ans5 = max(ans5_set) - min(ans5_set)
    ans6_set = {sel_row2*(w-sel_col2),(h-sel_row2)*(w-sel_col2),h*sel_col2}
    ans6 = max(ans6_set) - min(ans6_set)
    print(int(min(ans1,ans2,ans3,ans4,ans5,ans6)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=h*w
if h>2:
    t=[h//3*w,(h-(h//3))//2*w,(h-(h//3)+1)//2*w]
    t.sort()
    ans=min(ans,t[2]-t[0])
if w>2:
    t=[w//3*h,(w-(w//3))//2*h,(w-(w//3)+1)//2*h]
    t.sort()
    ans=min(ans,t[2]-t[0])
a,b=h,w
for i in range(max(1,a//3-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
a,b=w,h
for i in range(max(1,a//3-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
print(ans)

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

ans = float("inf")

if h%2==0:
    a = h//2
    b = h//2
else:
    a = h//2
    b = h//2+1

for i in range(1, w):
    l = [a*i, b*i, h*w-a*i-b*i]
    ans = min(ans, max(l)-min(l))


if w%2==0:
    a = w//2
    b = w//2
else:
    a = w//2
    b = w//2+1

for i in range(1, h):
    l = [a*i, b*i, h*w-a*i-b*i]
    ans = min(ans, max(l)-min(l))

print(ans)

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

ans = float("inf")
if h%2==0:
    ans = min(ans, abs(h//2*round(w*2/3)-h*round(w/3)))
else:
    for i in range(1, w):
        a = h//2*i
        b = (h//2+1)*i
        l = [a, b, w*h-a-b]
        ans = min(ans, max(l)-min(l))

if w%2==0:
    ans = min(ans, abs(w//2*round(w*2/3)-w*round(w/3)))
else:
    for i in range(1, h):
        a = w//2*i
        b = (w//2+1)*i
        l = [a, b, w*h-a-b]
        ans = min(ans, max(l)-min(l))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
h, w = [int(i) for i in sys.stdin.readline().split()]
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    _min = min(h,w)
    _max = max(h,w)
    s = h * w
    res = 1e9
    for x in [_max // 3, _max//3+1]:
        if (_max-x)%2==0:
            ls = [_min * x, _min*(_max - x)//2]
        elif _min % 2 == 0:
            ls = [_min * x, (s - _min * x)//2]
        else:
            __max = max(_max-x, _min)
            ls = [_min*x, (_max - x)*(__max//2), (_max-x)*(__max - __max//2)]
        res = min(res, max(ls)-min(ls))
    print(res)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split(' '))
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    #たて
    res = h * w
    for i in range(1, w):
        cuttedw = [h*i, h*((w-i)//2), h*(w-(w-i)//2)]
        res = min(res, max(cuttedw)-min(cuttedw))
        cuttedwh = [h*i, (h//2)*(w-i), (h-h//2)*(w-i)]
        res = min(res, max(cuttedwh)-min(cuttedwh))
    #よこ
    for i in range(1, h):
        cuttedh = [w*i, w*((h-i)//2), w*(h-(h-i)//2)]
        res = min(res, max(cuttedh)-min(cuttedh))
        cuttedhw = [w*i, (w//2)*(h-i), (w-w//2)*(h-i)]
        res = min(res, max(cuttedhw)-min(cuttedhw))
    print(res)

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    print(0)
elif (H * W) % 2 == 0:
    A, B = max(H, W), min(H, W)
    #a = -(-A // 3)
    a = A // 3
    print(a * B - ((A - a) * B) // 2)
    
else:
    A, B = max(H, W), min(H, W)
    a = -(-A // 3)
    #a = A // 3
    C, D = max(A - a, B), min(A - a, B)
    print(a * B - C // 2 * D)

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    print(0)
elif (H * W) % 2 == 0:
    A, B = max(H, W), min(H, W)
    #a = -(-A // 3)
    a = A // 3
    print(a * B - ((A - a) * B) // 2)
    
else:
    A, B = max(H, W), min(H, W)
    a = -(-A // 3)
    a = A // 3
    C, D = max(A - a, B), min(A - a, B)
    print(a * B - C // 2 * D)

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    print(0)
elif (H * W) % 2 == 0:
    A, B = max(H, W), min(H, W)
    #a = -(-A // 3)
    a = A // 3
    print(a * B - ((A - a) * B) // 2)
    
else:
    A, B = max(H, W), min(H, W)
    #a = -(-A // 3)
    a = A // 3
    C, D = max(A - a, B), min(A - a, B)
    print(a * B - C // 2 * D)

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    print(0)
elif (H * W) % 2 == 0:
    A, B = max(H, W), min(H, W)
    a = -(-A // 3)
    print(a * B - (A - a) * B // 2)
else:
    A, B = max(H, W), min(H, W)
    a = -(-A // 3)
    C, D = max(A - a, B), min(A - a, B)
    print(a * B - C // 2 * D)

Submission status: Wrong Answer

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

if (H * W) % 6 == 0:
    print(0)
elif (H * W) % 3 == 0:
    if H % 3 == 0:
        print(H // 3 * W - max(H * 2 // 3, W) // 2 * min(H * 2 // 3, W))
    else:
        print(W // 3 * H - max(W * 2 // 3, H) // 2 * min(W * 2 // 3, H))
elif (H * W) % 2 == 0:
    A, B = max(H, W), min(H, W)
    a = -(-A // 3)
    print(a * B - (A - a) * B // 2)
else:
    A, B = max(H, W), min(H, W)
    a = -(-A // 3)
    C, D = max(A - a, B), min(A - a, B)
    print(a * B - C // 2 * D)

Submission status: Wrong Answer

Solution code:
import math
H, W = map(int, input().split())
mi = H*W
for h in range(1, H):
    sa = h*W
    w = int(W/2)
    sb = (H-h)*w
    sc = (H-h)*(W-w)
    smax = max(sa, sb, sc)
    smin = min(sa, sb, sc)
    if smax - smin < mi:
        mi = smax - smin
    sb = int((H-h)/2*W)
    if (H-h)/2*W%1 != 0:
        sc = H*W-sa-sb-2
    else:
        sc = H*W-sa-sb
    smax = max(sa, sb, sc)
    smin = min(sa, sb, sc)
    if smax - smin < mi:
        mi = smax - smin
print(mi)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H > W:
    H, W = W, H

if H%3 == 0 or W%3 == 0:
    print(0)
elif H%2 == 0:
    H_div = H//2
    W_div = round(W/3)
    print(abs(H_div*(W-W_div) - H*W_div))
elif W%2 == 0:
    W_div = W//2
    H_div = round(H/3)
    print(abs(W_div*(H-H_div) - W*H_div))
else:
    H_div = round(H/3)
    W_div = W//2
    S = [H_div*W, (H-H_div)*W_div, (H-H_div)*(W-W_div)]
    H_div2 = H//2
    W_div2 = round(W/3)
    S2 = [W_div2*H, (W-W_div2)*H_div, (W-W_div2)*(H-H_div2)]
    print(min(max(S)-min(S), max(S2)- min(S2)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
ans=10**5
t=w//2
for i in range(1,h):
    m=max(i*w,(h-i)*t,(h-i)*(w-t))-min(i*w,(h-i)*t,(h-i)*(w-t))
    ans=min(ans,m)
t=h//2
for i in range(1,w):
    m=max(i*h,(w-i)*t,(w-i)*(h-t))-min(i*h,(w-i)*t,(w-i)*(h-t))
    ans=min(ans,m)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H > W:
    H, W = W, H

if H%3 == 0 or W%3 == 0:
    print(0)
elif H%2 == 0:
    H_div = H//2
    W_div = round(W/3)
    print(abs(H_div*(W-W_div) - H*W_div))
elif W%2 == 0:
    W_div = W//2
    H_div = round(H/3)
    print(abs(W_div*(H-H_div) - W*H_div))
else:
    H_div = round(H/3)
    W_div = W//2
    H_div2 = H//2
    W_div2 = round(W/3)
    print(min(abs(H_div*W - (H-H_div)*W_div), abs(W_div2*H - (W-W_div2)*H_div2)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H > W:
    H, W = W, H

if H%3 == 0 or W%3 == 0:
    print(0)
else:
    H_div = round(H/3)
    W_div = W//2
    H_div2 = H//2
    W_div2 = round(W/3)
    print(min(abs(H_div*W - (H-H_div)*W_div), abs(W_div*H - (W-W_div)*H_div)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H > W:
    H, W = W, H

if H%3 == 0 or W%3 == 0:
    print(0)
elif H%2 == 0:
    H_div = H//2
    W_div = round(W/3)
    print(abs(H_div*(W-W_div) - H*W_div))
else:
    H_div = round(H/3)
    W_div = W//2
    H_div2 = H//2
    W_div2 = round(W/3)
    print(min(abs(H_div*W - (H-H_div)*W_div), abs(W_div*H - (W-W_div)*H_div)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H > W:
    H, W = W, H

if H%3 == 0 or W%3 == 0:
    print(0)
elif H%2 == 0:
    H_div = H//2
    W_div = round(W/3)
    print(abs(H_div*(W-W_div) - H*W_div))
elif W%2 == 0:
    W_div = W//2
    H_div = round(H/3)
    print(abs(W_div*(H-H_div) - W*H_div))
else:
    H_div = round(H/3)
    W_div = W//2
    H_div2 = H//2
    W_div2 = round(W/3)
    print(min(abs(H_div*W - (H-H_div)*W_div), abs(W_div*H - (W-W_div)*H_div)))

Submission status: Wrong Answer

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

min_res = 10**9
for i in range(1, H):
    a = i * W
    other = H*W - a
    if other % 2 == 0:
        b = other // 2
        res = abs(a - b)
        if res < min_res:
            min_res = res
    else:
        b_a = other - ((W // 2)+1) * (H - i)
        b_b = other - ((H-i) // 2 +1) * W
        res = abs(a-b_a)
        if res < min_res:
            min_res = res
        res = abs(a - b_b)
        if res < min_res:
            min_res = res


for j in range(1, W):
    a = j * H
    other = H * W - a
    if other % 2 == 0:
        b = other // 2
        res = abs(a - b)
        if res < min_res:
            min_res = res
    else:
        b_a = other - ((H // 2) + 1) * (W - j)
        b_b = other - (((W-j) // 2) + 1) * H

        res = abs(a - b_a)
        if res < min_res:
            min_res = res

        res = abs(a - b_b)
        if res < min_res:
            min_res = res


print(min_res)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
print([0,min(abs(((w+2)//3*h)-((w-(w+2)//3)*(h//2))),abs(((h+2)//3*w)-((h-((h+2)//3))*(w//2))))][h%3!=0 and w%3!=0])

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

diff = 10 ** 10
for i in range(1, max(H, W)):
  first_rect = i * min(H, W)
  second_rect = (max(H, W) - i) * (math.ceil(min(H, W) / 2))
  third_rect = (max(H, W) - i) * (min(H, W) // 2)
  max_rect = max(first_rect, second_rect, third_rect)
  min_rect = min(first_rect, second_rect, third_rect)
  if abs(max_rect - min_rect) < diff:
    diff = abs(max_rect - min_rect)
    
min_rect = max(H, W) // 3 * min(H, W)
max_rect = math.ceil(max(H, W) / 3) * min(H, W)
if diff > max_rect - min_rect:
  diff = max_rect - min_rect
print(diff)

Submission status: Wrong Answer

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

min_res = 10**9
for i in range(1, H):
    a = i * W
    other = H*W - a
    if other % 2 == 0:
        b = other // 2
        res = abs(a - b)
        if res < min_res:
            min_res = res
    else:
        b_a = other - ((W // 2)+1) * (H - i)
        b_b = other - ((H-i) // 2 +1) * W
        res = abs(a-b_a)
        if res < min_res:
            min_res = res
        res = abs(a - b_b)
        if res < min_res:
            min_res = res


for j in range(1, W):
    a = j * H
    other = H * W - a
    if other % 2 == 0:
        b = other // 2
        res = abs(a - b)
        if res < min_res:
            min_res = res
    else:
        b_a = other - ((H // 2) + 1) * (H - i)
        b_b = other - (((W-j) // 2) + 1) * H

        res = abs(a - b_a)
        if res < min_res:
            min_res = res

        res = abs(a - b_b)
        if res < min_res:
            min_res = res


print(min_res)

Submission status: Wrong Answer

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

min_res = 10**9
for i in range(1, H):
    a = i * W
    other = H*W - a
    if other % 2 == 0:
        b = other // 2
        res = abs(a - b)
        if res < min_res:
            min_res = res
    else:
        b_a = other - (W // 2)+1 * (H - i)
        b_b = other - ((H-i) // 2)+1 * W
        res = abs(a-b_a)
        if res < min_res:
            min_res = res
        res = abs(a - b_b)
        if res < min_res:
            min_res = res
    

for j in range(1, W):
    a = j * H
    other = H * W - a
    if other % 2 == 0:
        b = other // 2
        res = abs(a - b)
        if res < min_res:
            min_res = res
    else:
        b_a = other - (H // 2) + 1 * (H - i)
        b_b = other - ((W-j) // 2) + 1 * H
    
        res = abs(a - b_a)
        if res < min_res:
            min_res = res

        res = abs(a - b_b)
        if res < min_res:
            min_res = res

  
print(min_res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H > W:
    H, W = W, H

if H%3 == 0 or W%3 == 0:
    print(0)
elif H%2 == 0:
    H_div = H//2
    W_div = round(W/3)
    print(abs(H_div*(W-W_div) - H*W_div))
elif W%2 == 0:
    W_div = W//2
    H_div = round(H/3)
    print(abs(W_div*(H-H_div) - W*H_div))
else:
    H_div = round(H/3)
    W_div = W//2
    print(abs(H_div*W - (H-H_div)*W_div))

Submission status: Wrong Answer

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

min_res = 10**9
for i in range(1, H):
    a = i * W
    other = H*W - a
    if other % 2 == 0:
        b = other // 2
    else:
        b_a = other - (W // 2)+1 * (H - i)
        b_b = other - ((H-i) // 2)+1 * W
        b = min(b_a, b_b)
    res = abs(a - b)
    if res < min_res:
        min_res = res

for j in range(1, W):
    a = j * H
    other = H * W - a
    if other % 2 == 0:
        b = other // 2
    else:
        b_a = other - (H // 2) + 1 * (H - i)
        b_b = other - ((W-j) // 2) + 1 * H
        b = min(b_a, b_b)

    res = abs(a - b)
    if res < min_res:
        min_res = res
print(min_res)

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

diff = 10 ** 10
for i in range(1, max(H, W)):
  first_rect = i * min(H, W)
  second_rect = (max(H, W) - i) * (math.ceil(min(H, W) / 2))
  third_rect = (max(H, W) - i) * (min(H, W) // 2)
  max_rect = max(first_rect, second_rect, third_rect)
  min_rect = min(first_rect, second_rect, third_rect)
  if abs(max_rect - min_rect) < diff:
    diff = abs(max_rect - min_rect)
print(diff)

Submission status: Wrong Answer

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

min_res = 10**9
for i in range(1, H):
    a = i * W
    other = H*W - a
    if other % 2 == 0:
        b = other // 2
    else:
        b = other - (W // 2)+1 * (H - i)
    res = abs(a - b)
    if res < min_res:
        min_res = res

for j in range(1, W):
    a = j * H
    other = H * W - a
    if other % 2 == 0:
        b = other // 2
    else:
        b = other - (H // 2) + 1 * (H - i)
    res = abs(a - b)
    if res < min_res:
        min_res = res
print(min_res)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import deque
import sys
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

H,W = IL()

if (H%3 == 0) or (W%3 == 0):
  print(0)
else:
  tate_m = H//2
  yoko_m = W//2

  ans = MAX_INT
  # 縦に切っていく
  for w in range(1,yoko_m+1):
    Area1 = H*w
    Area2 = tate_m*(W-w)
    Area3 = (H-tate_m)*(W-w)
    ans = min(ans, max(Area1,Area2,Area3)-min(Area1,Area2,Area3))

  # 横に切っていく
  for h in range(1,tate_m+1):
    Area1 = h*W
    Area2 = (H-h)*yoko_m
    Area3 = (H-h)*(W-yoko_m)
    ans = min(ans, max(Area1,Area2,Area3)-min(Area1,Area2,Area3))

  print(ans)

Submission status: Wrong Answer

Solution code:
def threeArea(w, h, ans):
    for i in range(1, h):
        recA = w * i
        width = w//2
        recB = (h-i) * width
        recC = (h-i) * (w - width)
        diff = max(recA, recB, recC) - min(recA, recB, recC)
        if ans > diff:
            ans = diff
    return ans

H, W = (int(i) for i in input().split())
if (H % 3 == 0) or (W % 3 == 0):
    print(0)
else:
    ans = 10 ** 11
    ans = threeArea(W, H,ans)
    ans = threeArea(H, W,ans)

    print(ans)

Submission status: Wrong Answer

Solution code:
def threeArea(nagai, mijikai, ans):
    for i in range(1, nagai):
        first = mijikai * i
        second = (mijikai//2) * (nagai - i)
        third = (nagai * mijikai) - (first + second)
        diff = max(first, second, third) - min(first, second, third)
        if ans > diff:
            ans = diff
    return ans

H, W = (int(i) for i in input().split())
if (H % 3 == 0) or (W % 3 == 0):
    print(0)
else:
    nagai = max(H,W)
    mijikai = min(H,W)
    ans = 10 ** 10
    ans = threeArea(nagai,mijikai,ans)
    ans = threeArea(mijikai,nagai,ans)

    print(ans)

Submission status: Wrong Answer

Solution code:
import math
H, W = (int(i) for i in input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    nagai = max(H,W)
    mijikai = min(H,W)
    ans = 10 ** 10
    for i in range(1, nagai):
        first = mijikai * i
        second = math.ceil(mijikai/2) * (nagai - i)
        third = math.floor(mijikai/2) * (nagai - i)
        diff = max(first, second) - min(first, third)
        if ans > diff:
            ans = diff
    for i in range(1, mijikai):
        first = nagai * i
        second = math.ceil(nagai / 2) * (mijikai - i)
        third = math.floor(nagai / 2) * (mijikai - i)
        diff = max(first, second) - min(first, third)
        if ans > diff:
            ans = diff

    print(ans)

Submission status: Wrong Answer

Solution code:
import math
H, W = (int(i) for i in input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    nagai = max(H,W)
    mijikai = min(H,W)
    ans = 10 ** 10
    for i in range(1, nagai):
        first = mijikai * i
        second = math.ceil(mijikai/2) * (nagai - i)
        third = math.floor(mijikai/2) * (nagai - i)
        diff = max(first, second, third) - min(first, second, third)
        if ans > diff:
            ans = diff
    print(ans)

Submission status: Wrong Answer

Solution code:
def calc(h, w):
    ans = 10**9
    for i in range(1, h):
        s1 = w * i
        s2 = (h - i) * (w // 2)
        s3 = (h - i) * (w - w // 2)
        s = [s1, s2, s3]
        ds = max(s) - min(s)
        ans = min([ans, ds])
    return ans


h, w = map(int, input().split())
if (h * w) % 3 == 0:
    print(0)
else:
    print(min([calc(h, w), calc(w, h)]))

Submission status: Wrong Answer

Solution code:
from math import ceil, floor

h,w = map(int, input().split())
if h % 3 == 0:
    print(h//3 * w)
elif w % 3 == 0:
    print(w//3 * h)
else:
    x,y = max(h,w), min(h,w)
    l = x // 3
    a = l * y
    b = (x-l) * floor(y/2)
    c = (x-l) * ceil(y/2)
    print(max(a,b,c) - min(a,b,c))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if (h*w)%3==0:
  print(0)
else:
  ans = w
  for i in range(1,h):
    s1 = w*i
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w-w//2)
    s = [s1,s2,s3]
    ds = max(s)-min(s)
    ans = min([ans,ds])
  print (ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h<w:
  h,w = w,h #縦長に一般化する
if (h*w)%3==0:
  print(0)
else:
  ans = w
  for i in range(1,h):
    s1 = w*i
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w-w//2)
    s = [s1,s2,s3]
    ds = max(s)-min(s)
    ans = min([ans,ds])
  print (ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h<w:
  h,w = w,h #縦長に一般化する
if (h*w)%3==0:
  print(0)
elif (h*w) == 4:
  print(1)
else:
  ans = w
  for i in range(1,h-1):
    s1 = w*i
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w-w//2)
    s = [s1,s2,s3]
    ds = max(s)-min(s)
    ans = min([ans,ds])
  print (ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h<w:
  h,w = w,h #縦長に一般化する
if (h*w)%3==0:
  print(0)
else:
  ans = w
  for i in range(1,h-1):
    s1 = w*i
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w-w//2)
    s = [s1,s2,s3]
    ds = max(s)-min(s)
    ans = min([ans,ds])
  print (ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    H,W = inputMap()
    
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        sys.exit()
        
    if H <= 2 and W <= 2:
        print(1)
        sys.exit()
        
    if H <= W:
        a = -(-W // 3)
        b = -(-H // 2)
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        ans = min(ans1,ans2,ans3)
        
        tmp = W // 3
        tmp2 = (W - tmp) // 2
        s1 = tmp * H
        s2 = tmp2 * H
        s3 = (W - tmp- tmp2) * H
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp += 1
        s1 = tmp * H
        s2 = tmp2 * H
        s3 = (W - tmp- tmp2) * H
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp -= 2
        s1 = tmp * H
        s2 = tmp2 * H
        s3 = (W - tmp- tmp2) * H
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans,ans1,ans2,ans3))
        
    else:
        a = -(-H // 3)
        b = -(-W // 2)
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        ans = min(ans1,ans2,ans3)
        
        tmp = H // 3
        tmp2 = (H - tmp) // 2
        s1 = tmp * W
        s2 = tmp2 * W
        s3 = (H - tmp- tmp2) * W
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp += 1
        s1 = tmp * W
        s2 = tmp2 * W
        s3 = (H - tmp- tmp2) * W
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp -= 2
        s1 = tmp * W
        s2 = tmp2 * W
        s3 = (H - tmp- tmp2) * W
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans,ans1,ans2,ans3))
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    H,W = inputMap()
    
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        sys.exit()
        
    if H <= 2 and W <= 2:
        print(1)
        sys.exit()
        
    if H <= W:
        a = -(-W // 3)
        b = -(-H // 2)
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 1
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans4 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 4
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans5 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans1,ans2,ans3,ans4,ans5))
        
    else:
        a = -(-H // 3)
        b = -(-W // 2)
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 1
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans4 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 4
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans5 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans1,ans2,ans3,ans4,ans5))
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    H,W = inputMap()
    
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        sys.exit()
        
    if H <= 2 and W <= 2:
        print(1)
        sys.exit()
        
    if H <= W:
        a = -(-W // 3)
        b = -(-H // 2)
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans1,ans2,ans3))
    else:
        a = -(-H // 3)
        b = -(-W // 2)
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans1,ans2,ans3))
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import math
H, W = map(int, input().split())
ans = 10**10
for h in range(1, H-1):
    A = h * W
    #残りを横に割る
    B = W*math.ceil((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    #残りを縦に割る
    B = (H-h)*math.ceil(W/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)

# 縦横入れ替え
H, W = W, H
for h in range(1, H-1):
    A = h * W
    #残りを横に割る
    B = W*math.ceil((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    #残りを縦に割る
    B = (H-h)*math.ceil(W/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
H, W = map(int, input().split())
ans = 10**10
for h in range(1, H-1):
    A = h * W
    #残りを横に割る
    B = W*math.floor((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    #残りを縦に割る
    B = (H-h)*math.floor(W/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)

# 縦横入れ替え
H, W = W, H
for h in range(1, H-1):
    A = h * W
    #残りを横に割る
    B = W*math.floor((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    #残りを縦に割る
    B = (H-h)*math.floor(W/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
print(ans)