Problem:


Score : 400 points

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min}.

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

Print the minimum possible value of S_{max} - S_{min}.

Sample Input 1

3 5

Sample Output 1

0

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000



Sample input:
3 5


Sample output:
0

Submission status: Wrong Answer

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

def solve(H: int, W: int):
    if H%3==0 or W%3==0:
        print(0)
        return 
    
    ## 小さい方をHにする
    H,W = min(H,W),max(H,W)
    answer = 10**10
    for w in range(1,W+1):
        first = H*w
        h,remain_w = min(W-w,H),max(W-w,H)
        if h%2==0 or remain_w%2==0:
            third = int(h*remain_w/2)
            answer = min(answer,abs(first-third))
        else:
            third = h*(remain_w//2)
            second = h*remain_w-third

            answer = min(answer,max(abs(third-second),abs(first-third)))

    for h in range(1,H+1):
        first = W*h
        remain_h,w = min(H-h,W),max(H-h,W)
        if remain_h%2==0 or w%2==0:
            third = int(remain_h*w/2)
            answer = min(answer,abs(first-third))
        else:
            third = remain_h*(w//2)
            second = remain_h*w-third

            answer = min(answer,max(abs(third-second),abs(first-third)))
    print(answer)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def solve(H: int, W: int):
    if H%3==0 or W%3==0:
        print(0)
        return 
    
    ## 小さい方をHにする
    H,W = min(H,W),max(H,W)
    answer = 10**10
    for w in range(1,W//2+1):
        first = H*w
        h,remain_w = min(W-w,H),max(W-w,H)
        if h%2==0 or remain_w%2==0:
            third = int(h*remain_w/2)
            answer = min(answer,abs(first-third))
        else:
            third = h*(remain_w//2)
            second = h*remain_w-third

            answer = min(answer,max(abs(third-second),abs(first-third)))

    
    for h in range(1,H//2+1):
        first = W*h
        remain_h,w = min(H-h,W),max(H-h,W)
        if remain_h%2==0 or w%2==0:
            third = int(remain_h*w/2)
            answer = min(answer,abs(first-third))
        else:
            third = remain_h*(w//2)
            second = remain_h*w-third

            answer = min(answer,max(abs(third-second),abs(first-third)))
    print(answer)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def solve(H: int, W: int):
    if H%3==0 or W%3==0:
        print(0)
        return 
    
    ## 小さい方をHにする
    H,W = min(H,W),max(H,W)
    answer = 10**10
    for w in range(1,W//2+1):
        first = H*w
        h,remain_w = min(W-w,H),max(W-w,H)
        if h%2==0 or remain_w%2==0:
            third = int(h*remain_w/2)
            answer = min(answer,abs(first-third))
        else:
            third = h*(remain_w//2)
            second = h*remain_w-third

            answer = min(answer,max(abs(third-second),abs(first-third)))

    H,W = W,H
    for w in range(1,W//2+1):
        first = H*w
        h,remain_w = min(W-w,H),max(W-w,H)
        if h%2==0 or remain_w%2==0:
            third = int(h*remain_w/2)
            answer = min(answer,abs(first-third))
        else:
            third = h*(remain_w//2)
            second = h*remain_w-third

            answer = min(answer,max(abs(third-second),abs(first-third)))
    print(answer)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def solve(H: int, W: int):
    if H%3==0 or W%3==0:
        print(0)
        return 
    
    ## 小さい方をHにする
    H,W = min(H,W),max(H,W)
    answer = 10**10
    for w in range(1,W//2+1):
        first = H*w
        h,remain_w = min(W-w,H),max(W-w,H)
        if h%2==0 or remain_w%2==0:
            third = int(h*remain_w/2)
            answer = min(answer,abs(first-third))
        else:
            third = h*(remain_w//2)
            second = h*remain_w-third

            answer = min(answer,max(abs(third-second),abs(first-third)))

    print(answer)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def solve(H: int, W: int):
    if H%3==0 or W%3==0:
        print(0)
        return 
    
    ## 小さい方をHにする
    H,W = min(H,W),max(H,W)

    ##一つ目の長方形の面積
    first_S = H*(W//3)

    h,w = min(H,W-(W//3)), max(H,W-(W//3))
    minimum_S = 0
    if h%2==0 or w%2==0:   
        minimum_S = int(h*w/2)
    else:
        minimum_S = h*(w//2)

    answer = abs(minimum_S-first_S)

    first_S = H*(W//3+1)
    h,w = min(H,W-(W//3+1)), max(H,W-(W//3+1))
    minimum_S = 0
    if h%2==0 or w%2==0:   
        minimum_S = int(h*w/2)
    else:
        minimum_S = h*(w//2)

    answer = min(answer,abs(minimum_S-first_S))
    print(answer)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H*W%3==0:
    ans=0
else:
    # case1:
    W1=(W+1)//2;W2=W//2
    # case1-1:
    H1=(H+2)//3;H2=H-H1
    S11=[H1*W,H2*W1,H2*W2]
    ans11=max(S11)-min(S11)
    # case1-2:
    H1=max(H//3,1);H2=H-H1
    S12=[H1*W,H2*W1,H2*W2]
    ans12=max(S12)-min(S12)
    # case2:
    H1=(H+1)//2;H2=H//2
    # case2-1:
    W1=(W+2)//3;W2=W-W1
    S21=[H*W1,H1*W2,H2*W2]
    ans21=max(S21)-min(S21)
    # case2-2:
    W1=max(W//3,1);W2=W-W1
    S22=[H*W1,H1*W2,H2*W2]
    ans22=max(S22)-min(S21)
    ans=min(ans11,ans12,ans21,ans22)
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC062 C - Chocolate Bar
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

h,w = nm()

s = h*w
s1 = 0
s2 = 0
s3 = 0
wm = w//2
hm = h//2
s_max = 0
s_min = 0
ans_1 = 10**10
ans_2 = 10**10
ans_3 = 10**10
ans_4 = 10**10

for i in range(1,h-1):
    s1 = i*w
    s2 = ((h-i)//2)*w
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_1 = min(ans_1,s_max-s_min)

for i in range(1,h-1):
    s1 = i*w
    s2 = (h-i)*wm
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_2 = min(ans_2,s_max-s_min)

for i in range(1,w-1):
    s1 = i*h
    s2 = ((w-i)//2)*h
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_3 = min(ans_3,s_max-s_min)

for i in range(1,w-1):
    s1 = i*h
    s2 = (w-i)*hm
    s3 = s-s1-s2
    s_max = max(s1,s2,s3)
    s_min = min(s1,s2,s3)
    ans_4 = min(ans_4,s_max-s_min)

print(min(ans_1,ans_2,ans_3,ans_4))

Submission status: Wrong Answer

Solution code:
def func(H,W):
    if H % 3 == 0 or W % 3 == 0:
        return 0
    area = H * W
    
    # よこ
    half_H = int(H/2)
    res = 10000000
    for x in range(1,W):
        S1 = H * x
        S2 = (W - x) * half_H
        S3 = area - S1 - S2

        tmpres = max(S1, S2, S3) - min(S1, S2, S3)
        if tmpres < res:
            res = tmpres
            
    # たて
    half_W = int(W/2)
    for y in range(1,H):
            S1 = W * y
            S2 = (H - y) * half_W
            S3 = area - S1 - S2
            
            tmpres = max(S1, S2, S3) - min(S1, S2, S3)
            if tmpres < res:
                res = tmpres
    return res

H, W = list(map(int, input().split()))
print(func(H,W))

Submission status: Wrong Answer

Solution code:
import sys

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

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

sht = min(h,w)
lg = max(h,w)
s = (h*w)//3

for i in range(1,lg+1):
    if i*sht > s:
        ans1 = i*sht
        break

if (h*w-ans1)%2==0:
    print(ans1-((h*w-ans1)//2))
    
    sys.exit()

else:
    lg_memo = lg - i
    sht = min(sht,lg_memo)
    lg = max(sht,lg_memo)
    s = (sht*lg)//2

    for i in range(1,lg+1):
        if i*sht >= s:
            ans2 = i*sht
            ans3 = h*w-ans1-ans2
            break

print(ans1-min(ans2,ans3))

Submission status: Wrong Answer

Solution code:
import sys

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

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

sht = min(h,w)
lg = max(h,w)
s = (h*w)//3

for i in range(1,lg+1):
    if i*sht > s:
        ans1 = i*sht
        break

if (h*w-ans1)%2==0:
    print(ans1-((h*w-ans1)//2))
    print(2222)
    sys.exit()

else:
    lg_memo = lg - i
    sht = min(sht,lg_memo)
    lg = max(sht,lg_memo)
    s = (sht*lg)//2

    for i in range(1,lg+1):
        if i*sht >= s:
            ans2 = i*sht
            ans3 = h*w-ans1-ans2
            break

print(ans1-min(ans2,ans3))

Submission status: Wrong Answer

Solution code:
def f(Hn, Wn):
    ret = float('inf')
    for Wi in range(1, Wn):
        h = Hn // 2
        w = Wn - Wi
        Sn = (Wi * Hn, w * h, w * (h + 1)) if Hn % 2 else (Wi * Hn, w * h)
        ret = min(ret, max(Sn) - min(Sn))
    return ret


def main():
    H, W = map(int, input().split())
    if not (H % 3 and W % 3):
        print(0)
        return
    ans = f(H, W)
    if not ans:
        print(0)
        return
    ans = min(ans, f(W, H))
    if not ans:
        print(0)
        return
    print(ans)


main()

Submission status: Wrong Answer

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

mind = float('inf')

for i in range(1,h):
  d = max(abs(w*i-(w//2)*(h-i)),(h-i)*bool(w%2))
  mind = min(mind,d)
  
for i in range(1,w):
  d = max(abs(h*i-(h//2)*(w-i)),(w-i)*bool(h%2))
  mind = min(mind,d)
  
mind = min(mind,min(h,w)*bool(max(h,w)%3)*bool((h*w)%3))

print(mind)

Submission status: Wrong Answer

Solution code:
def sp(n):
  a = n // 3
  b = (n - a) // 2
  c = n -a -b
  return a,b,c

def tatedake_diff(h, w):
  a,b,c = sp(h)
  s1 = (c-a) * w
  a,b,c = sp(w)
  s2 = (c-a) * h
  return min(s1, s2)

def tateyoko_diff(h, w):
  a,b,c = sp(h)
  s1 = (c * w) - (a + b) * (w // 2)
  a,b,c = sp(w)
  s2 = (c * h) - (a + b) * (h // 2)
  return min(s1, s2)

H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
  ans = 0
else:
  ans = min(tatedake_diff(H, W), tateyoko_diff(H, W))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
else:
    ans=float('inf')
    for i in range(1,h):
        c=max(i*(w//2),i*(w-w//2),(h-i)*w)-min(i*(w//2),i*(w-w//2),(h-i)*w)
        if ans>c:
            ans=c
    for i in range(1,w):
        c=max(i*(h//2),i*(h-h//2),(w-i)*h)-min(i*(h//2),i*(h-h//2),(w-i)*h)
        if ans>c:
            ans=c
    print(ans)

Submission status: Wrong Answer

Solution code:
# C - Chocolate Bar

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

if H%3 == 0 or W%3 == 0:
    print(0)
else:
    H, W = min(H,W), max(H,W) # 横長状態にしてまず縦方向に割る
    
    # 横3分の1に近い所で割る→残りをなるべく均等に割る
    piece1_W = round(W/3)
    piece1 = H * piece1_W
    if H%2 == 0 or (W - piece1_W)%2 == 0:
        piece3 = (H*W - piece1)//2
    elif H >= (W - piece1_W):
        piece3 = (W - piece1_W) * (H//2)
    else:
        piece3 = H * (W - piece1_W)//2
    piece2 = H*W - piece1 - piece3
    
    print(max(piece1, piece2, piece3) - min(piece1, piece2, piece3))

Submission status: Wrong Answer

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

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

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

    min_edge = min(h, w)
    max_edge = max(h, w)
    edge_min_piece_1 = max_edge // 3
    rest_edge_1 = max_edge - edge_min_piece_1
    edge_max_piece_1 = (min_edge + 1) // 2

    ans_1 = edge_max_piece_1 * rest_edge_1 - edge_min_piece_1 * min_edge

    edge_max_piece_2 = (max_edge + 2) // 3
    rest_edge_2 = max_edge - edge_max_piece_2
    edge_min_piece_2 = min_edge // 2

    ans_2 = edge_max_piece_2 * min_edge - edge_min_piece_2 * rest_edge_2
    print(min(ans_1, ans_2))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ans=10000000000000
for i in range(1,n):
    a=i*m
    nk=n-i
    if nk%2==0:
        b=m*(nk//2)
        ans=min(ans,abs(a-b))
    elif nk>1:
        b=m*((nk+1)//2)
        c=m*((nk-1)//2)
        ans=min(ans,max(a,b,c)-min(a,b,c))
    if m%2==0:
        b=nk*m//2
        ans=min(ans,abs(a-b))
    elif m>1:
        b=nk*(m+1)//2
        c=nk*(m-1)//2
        ans=min(ans,max(a,b,c)-min(a,b,c))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
  print(0)
else:
  ans=h*w
  v=w//2
  for i in range(1,h):
    a=i*w
    y=(h-i)//2
    z=h-i-y
    b=y*w
    c=z*w
    d=(h-i)*v
    e=(h-i)*(w-v)
    ans=min(ans,max(a,b,c)-min(a,b,c),max(a,d,e)-min(a,d,e))
  print(ans)

Submission status: Wrong Answer

Solution code:

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

ans = min(H, W)

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

for w in range(W // 3, W // 3 + 3):
    h = H // 2
    s1 = w*H
    s2 = (W-w)*h
    s3 = (W-w)*(H-h)
    ans = min(ans, max(s1,s2,s3) - min(s1,s2,s3))
    
print(ans)

Submission status: Wrong Answer

Solution code:


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

ans = min(H, W)


if H % 3 == 0 or W % 3 == 0:
    ans = 0
    
for ha in range(H//3, H//3+3):
    Sa = ha * W
    
    ## 横に分割
    hb = (H - ha)//2
    hc = H - ha - hb
    Sb = hb * W
    Sc = hc * W
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

for wa in range(W//3, W//3+3):
    Sa = H * wa
    
    ## 横に分割
    hb = H//2
    hc = H - hb
    Sb = hb * (W - wa)
    Sc = hc * (W - wb)
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Submission status: Wrong Answer

Solution code:


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

ans = min(H, W)

for ha in range(H//3, H//3+3):
    Sa = ha * W
    
    ## 横に分割
    hb = (H - ha)//2
    hc = H - ha - hb
    Sb = hb * W
    Sc = hc * W
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

for wa in range(W//3, W//3+3):
    Sa = H * wa
    
    ## 横に分割
    hb = H//2
    hc = H - hb
    Sb = hb * (W - wa)
    Sc = hc * (W - wb)
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Submission status: Wrong Answer

Solution code:

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

ans = float("inf")

for ha in range(H//3, H//3+3):
    Sa = ha * W
    
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

for wa in range(W//3, W//3+3):
    Sa = H * wa
    
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Submission status: Wrong Answer

Solution code:

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

ans = float("inf")

for ha in range(H//3, H//3+3):
    Sa = ha * W
    
    ## 横に分割
    hb = (H - ha)//2
    hc = H - ha - hb
    Sb = hb * W
    Sc = hc * W
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

for wa in range(W//3, W//3+3):
    Sa = H * wa
    
    ## 横に分割
    hb = H//2
    hc = H - hb
    Sb = hb * (W - wa)
    Sc = hc * (W - wb)
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Submission status: Wrong Answer

Solution code:

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

hal = [H//3, H//3+3]

ans = float("inf")

for ha in hal:
    Sa = ha * W
    
    ## 横に分割
    hb = (H - ha)//2
    hc = H - ha - hb
    Sb = hb * W
    Sc = hc * W
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

wal = [W//3, W//3+3]

for wa in wal:
    Sa = H * wa
    
    ## 横に分割
    hb = H//2
    hc = H - hb
    Sb = hb * (W - wa)
    Sc = hc * (W - wb)
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Submission status: Wrong Answer

Solution code:
H, W = tuple(map(int, input().split()))
print(H % 3 * (W // 2))

Submission status: Wrong Answer

Solution code:

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

hal = [H//3, H//3+1]

ans = float("inf")

for ha in hal:
    Sa = ha * W
    
    ## 横に分割
    hb = (H - ha)//2
    hc = H - ha - hb
    Sb = hb * W
    Sc = hc * W
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

wal = [W//3, W//3+1]

for wa in wal:
    Sa = H * wa
    
    ## 横に分割
    hb = H//2
    hc = H - hb
    Sb = hb * (W - wa)
    Sc = hc * (W - wb)
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Submission status: Wrong Answer

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

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

first_peace = [0, 0]
s_and_t_peace = [0, 0]
second_peace_area = 0
third_peace_area = 0

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

pat1 = h
  
first_peace[0] = int(round(w / 3))
first_peace[1] = h
s_and_t_peace = [w - first_peace[0], h]

if s_and_t_peace[0] % 2 == 0 or s_and_t_peace[1] % 2 == 0:
  second_peace_area = s_and_t_peace[0] * s_and_t_peace[1] / 2
  third_peace_area = s_and_t_peace[0] * s_and_t_peace[1] / 2
else:
  second_peace_area = (max(s_and_t_peace[0], s_and_t_peace[1]) + 1) * min(s_and_t_peace[0], s_and_t_peace[1]) / 2
  third_peace_area = (max(s_and_t_peace[0], s_and_t_peace[1]) - 1) * min(s_and_t_peace[0], s_and_t_peace[1]) / 2
first_peace_area = first_peace[0] * first_peace[1]

pat2 = int(max(abs(first_peace_area - second_peace_area), abs(first_peace_area - third_peace_area)))
print(min(pat1, pat2))

Submission status: Wrong Answer

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

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

first_peace = [0, 0]
s_and_t_peace = [0, 0]
second_peace_area = 0
third_peace_area = 0

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

first_peace[0] = int(round(w / 3))
first_peace[1] = h
s_and_t_peace = [w - first_peace[0], h]

if s_and_t_peace[0] % 2 == 0 or s_and_t_peace[1] % 2 == 0:
  second_peace_area = s_and_t_peace[0] * s_and_t_peace[1] / 2
  third_peace_area = s_and_t_peace[0] * s_and_t_peace[1] / 2
else:
  second_peace_area = (max(s_and_t_peace[0], s_and_t_peace[1]) + 1) * min(s_and_t_peace[0], s_and_t_peace[1]) / 2
  third_peace_area = (max(s_and_t_peace[0], s_and_t_peace[1]) - 1) * min(s_and_t_peace[0], s_and_t_peace[1]) / 2
first_peace_area = first_peace[0] * first_peace[1]

print(int(max(abs(first_peace_area - second_peace_area), abs(first_peace_area - third_peace_area))))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = float("Inf")
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
for i in range(1,h-1):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    ans = min(ans,abs(max(i*w,a,b)-min(i*w,a,b)))
for i in range(1,w-1):
    a = (w-i)*(h//2)
    b = (w-i)*-(-h//2)
    ans = min(ans,abs(max(i*h,a,b)-min(i*h,a,b)))
print(min(ans,h,w))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = float("Inf")
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
for i in range(1,h-1):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    ans = min(ans,max(i*w,a,b)-min(i*w,a,b))
for i in range(1,w-1):
    a = (w-i)*(h//2)
    b = (w-i)*-(-h//2)
    ans = min(ans,max(i*h,a,b)-min(i*h,a,b))
print(min(ans,h,w))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = float("Inf")
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
for i in range(1,h-1):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    ans = min(ans,max(i*w,a,b)-min(i*w,a,b))
for i in range(1,w-1):
    a = (w-i)*(h//2)
    b = (w-i)*-(h-(h//2))
    ans = min(ans,max(i*h,a,b)-min(i*h,a,b))
print(min(ans,h,w))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = float("Inf")
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
for i in range(1,h-1):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    ans = min(ans,max(i*w,a,b)-min(i*w,a,b))
for i in range(1,w-1):
    a = (w-i)*(h//2)
    b = (w-i)*(h-(h//2))
    ans = min(ans,max(i*h,a,b)-min(i*h,a,b))
print(min(ans,h,w))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = float("Inf")
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
for i in range(1,h-1):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    ans = min(ans,max(i*w,a,b)-min(i*w,a,b))
for i in range(1,w-1):
    a = (w-i)*(h//2)
    b = (w-i)*(h-(h//2))
    ans = min(ans,max(i*h,a,b)-min(i*h,a,b))
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:
    print(0)
    exit()
for i in range(1,h-1):
    a = (h-i)*(w//2)
    b = (h-i)*-(-w//2)
    c = ((h-i)//2) * w
    d = -(-(h-i)//2) * w
    ans = min(ans,max(i*w,a,b)-min(i*w,a,b))
    ans = min(ans,max(i*w,c,d)-min(i*w,c,d))
for i in range(1,w-1):
    a = (w-i)*(h//2)
    b = (w-i)*(h-(h//2))
    c = ((w-i)//2) * h
    d = -(-(w-i)//2) * h
    ans = min(ans,max(i*h,a,b)-min(i*h,a,b))
    ans = min(ans,max(i*h,c,d)-min(i*h,c,d))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = 10**9

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    for h in range(1,H):
        A = h * W
        B = (H - h) * -(-W // 2)
        C = (H - h) * (W // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))

    for w in range(1,W):
        A = w * H
        B = (W - w) * -(-H // 2)
        C = (W - w) * (H // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))
    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = 10**9

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    for h in range(1,H):
        A = h * W
        B = (H - h) * (W // 2 + (W % 2))
        C = (H - h) * (W // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))

    for w in range(1,W):
        A = w * H
        B = (W - w) * (H // 2 + (H % 2))
        C = (W - w) * (H // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))
    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = 10**9

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    for h in range(1,H):
        A = h * W
        B = (H - h) * (W // 2 + (W % 2))
        C = (H - h) * (W // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))
        
    H, W = W, H
    for w in range(1,W):
        A = w * H
        B = (W - w) * (H // 2 + (H % 2))
        C = (W - w) * (H // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))
    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = 10**9

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    for h in range(1,H):
        A = h * W
        B = (H - h) * (W // 2 + (W % 2))
        C = (H - h) * (W // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))
        
    H, W = W, H
    for h in range(1,H):
        A = h * W
        B = (H - h) * (W // 2 + (W % 2))
        C = (H - h) * (W // 2) 
        ans = min(ans, max(A,B,C)-min(A,B,C))
    print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
if h%3!=0 and w%3!=0:
    ansh=h*w
    for j in range(1,h):
        area1=w*j
        number=w//2
        area2=(h-j)*number
        area3=(h-j)*(w-number)
        check=max(abs(area1-area2),abs(area1-area3),abs(area2-area3))
        if check<ansh:
            ansh=check
        else:
            continue  
    answ=w*h
    for j in range(1,w):
        area1=h*j
        number=h//2
        area2=(w-j)*number
        area3=(w-j)*(h-number)
        check=max(abs(area1-area2),abs(area1-area3),abs(area2-area3))
        if check<answ:
            answ=check
        else:
             continue
    print(min(ansh,answ))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    h,w=map(int,input().split())
    if h%3==0 or w%3==0:
        print(0)
        return
    ans=INF
    # hをtrisect, wをbisect
    a=(h//3)*w
    b=(h-h//3)*(w//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    a=(h//3+1)*w
    b=(h-h//3-1)*(w//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    # hをbisect, wをtrisect
    a=(w//3)*h
    b=(w-w//3)*(h//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    a=(w//3+1)*h
    b=(w-w//3-1)*(h//2)
    c=h*w-a-b
    ans=min(ans,max(a,b,c)-min(a,b,c))
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
h,w= map(int,input().split())
ans = float("Inf")

if h % 3 == 0 or h % 3 == 0:
    print(0)
    exit()
for i in range(1,w):
    H1, H2= h // 2, -(-h // 2)
    check = max(h*i,H2*(w-i),H1*(w-i))
    check2 = min(h*i,H1*(w-i),H2*(w-i))
    ans = min(ans,abs(check-check2))
for i in range(1,h):
    w1,w2 = w // 2, -(-w // 2)
    check = max(w*i,w1*(h-i),w2*(h-i))
    check2 = min(w*i,w1*(h-i),w2*(h-i))
    ans = min(ans,abs(check-check2))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w= map(int,input().split())
ans = float("Inf")

if h % 3 == 0 or h % 3 == 0:
    print(0)
    exit()
for i in range(1,w+1):
    H1, H2= h // 2, -(-h // 2)
    check = max(h*i,H2*(w-i),H1*(w-i))
    check2 = min(h*i,H1*(w-i),H2*(w-i))
    ans = min(ans,abs(check-check2))
for i in range(1,h+1):
    w1,w2 = w // 2, -(-w // 2)
    check = max(w*i,w1*(h-i),w2*(h-i))
    check2 = min(w*i,w1*(h-i),w2*(h-i))
    ans = min(ans,abs(check-check2))
print(ans)

Submission status: Wrong Answer

Solution code:
h= sorted(map(int,input().split()))
ans = float("Inf")
H1,H2,W = h[0]//2,h[0]-h[0]//2,h[1]//2
check,check2 = -1,-1
if h[0] % 3 == 0 or h[1] % 3 == 0:
    print(0)
    exit()
for i in range(1,W+1):
    check = max(h[0]*i,H2*(h[1]-i),H1*(h[1]-i))
    check2 = min(h[0]*i,H1*(h[1]-i),H2*(h[1]-i))
    ans = min(ans,abs(check-check2))
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
ans = float("Inf")
H1,H2,W = h//2,h-h//2,w//2
check,check2 = -1,-1
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
if w == 2:
    for i in range(1,h//2+1):
        ans = min(ans,abs(w*i-(h-i)))
else:
    for i in range(1,W+1):
        check = max(h*i,H2*(w-i),H1*(w-i))
        check2 = min(h*i,H1*(w-i),H2*(w-i))
        ans = min(ans,abs(check-check2))
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
ans = float("Inf")
H1,H2,W = h//2,h-h//2,w//2
check,check2 = -1,-1
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
if w == 2:
    for i in range(1,h//2+1):
        ans = min(ans,abs(w*i-(h-i)))
else:
    for i in range(1,W+1):
        check = max(h*i,H2*(w-i))
        check2 = min(h*i,H1*(w-i))
        ans = min(ans,abs(check-check2))
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
if h == 2 or w == 2:
    h //3
H1,H2,W = h//2,h-h//2,w//2
check,check2 = -1,-1
ans = float("Inf")
for i in range(1,W+1):
    check = max(h*i,H2*(w-i))
    check2 = min(h*i,H1*(w-i))
    ans = min(ans,abs(check-check2))
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    h,w=map(int,input().split(' '))
    if h%3 == 0 or w%3 ==0:
        print(0)
        exit()
    ans = h*w
    for i in range(1,w):
        a=h*i
        b=(w-i)*(h//2)
        c=(w-i)*-(-h//2)
        if ans > max(a,b,c)-min(a,b,c):
            ans = max(a,b,c)-min(a,b,c)
    for j in range(1,h):
        a=w*j
        b=(h-j)*(w//2)
        c=(h-j)*-(-w//2)
        if ans > max(a,b,c)-min(a,b,c):
            ans = max(a,b,c)-min(a,b,c)
    print(ans)
if __name__ == '__main__':
    main()