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

ans = 10 ** 18

# 平行に三分割出来れば答えは0
Hsplit = W * ((H + 2) // 3 - H // 3)
Wsplit = H * ((W + 2) // 3 - W // 3)
ans = min(ans, Hsplit, Wsplit)

# 縦横に分割


def calc(X, Y):
    res = 10 ** 18

    Ymax = (Y + 1) // 2
    Ymin = Y // 2
    for i in range(1, Y):
        s1 = Ymax * i
        s2 = Ymin * i
        s3 = Y * (X - i)
        res = min(res, max(s1, s2, s3) - min(s1, s2, s3))
    return res


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

print(ans)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
if H%3==0 or W%3==0:
    print(0)
    exit()

ans = min(H,W)

for i in range(1,H-1):
    a = i*W
    m = W//2
    b = (H-i)*m
    c = (H-i)*(W-m)
    tmp = max(a,b,c) - min(a,b,c)
    ans = min(ans, tmp)

for i in range(1,W-1):
    a = i*H
    m = H//2
    b = (W-i)*m
    c = (W-i)*(H-m)
    tmp = max(a,b,c) - min(a,b,c)
    ans = min(ans, tmp)

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

def getmin(h,w):
    min_w = 10**10

    if h%2==0:
        h1=h//2
        h2=h//2
    else:
        h1=h//2
        h2=h1+1

    for i in range(1,w):
        area1=h*i
        area2=h1*(w-i)
        area3=h2*(w-i)

        lis=[area1,area2,area3]
        lis.sort()
        if lis[0] <= 0:
            continue

        dif = lis[2] - lis[0]
        if min_w > dif:
            min_w=dif

    return min_w

print(min(getmin(h,w),getmin(w,h)))

Submission status: Wrong Answer

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

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

def getmin(h,w):
    min_w = 10**10

    if h%2==0:
        h1=h//2
        h2=h//2
    else:
        h1=h//2
        h2=h1+1

    for i in range(1,w):
        area1=h*i
        area2=h1*(w-i)
        area3=h2*(w-i)

        lis=[area1,area2,area3]
        lis.sort()
        if lis[0] < 0:
            continue

        dif = lis[2] - lis[0]
        if min_w > dif:
            min_w=dif

    return min_w

print(min(getmin(h,w),getmin(w,h)))

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    if w < h:
        h, w = w, h
    if h == 1:
        return 1
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
        if (w - i) % 2 == 0:
            vals[1] = h * ((w - i) // 2)
            vals[2] = vals[1]
        else:
            vals[1] = h * ((w - i) // 2 + 1)
            vals[2] = h * ((w - i) // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    if w < h:
        h, w = w, h
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w // 2 + 1):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
        if (w - i) % 2 == 0:
            vals[1] = h * ((w - i) // 2)
            vals[2] = vals[1]
        else:
            vals[1] = h * ((w - i) // 2 + 1)
            vals[2] = h * ((w - i) // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    if w < h:
        h, w = w, h
    if h == 1:
        return 1
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w // 2 + 1):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
        if (w - i) % 2 == 0:
            vals[1] = h * ((w - i) // 2)
            vals[2] = vals[1]
        else:
            vals[1] = h * ((w - i) // 2 + 1)
            vals[2] = h * ((w - i) // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    if w < h:
        h, w = w, h
    if h == 1:
        return 1
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    if w < h:
        h, w = w, h
    if h == 1:
        return 1
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w // 2 + 1):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    if w < h:
        h, w = w, h
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w // 2 + 1):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

else:
    for m in range(2):
        H,W=W,H
        if H%2==0:
            A1=(H//2)* W
        else:
            A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0
        if m == 0:
            value=max(A1,A3)-min(A1,A3)
        else:
            value=min(max(A1,A3)-min(A1,A3),value)
        for i in range(W):
            A3+=H
            if H%2==0:
                A1-=H//2
            else:
                A1-=H//2 + 1
            A2-=H//2
            value=min(max(A1,A3)-min(A1,A3),value)



    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

else:
    for m in range(2):
        H,W=W,H
        if H%2==0:
            A1=(H//2)* W
        else:
            A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0
        if m == 0:
            value=max(A1,A2,A3)-min(A1,A2,A3)
        else:
            value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
        for i in range(W):
            A3+=H
            if H%2==0:
                A1-=H//2
            else:
                A1-=H//2 + 1
            A2-=H//2
            value=min(max(A1,A2,A3)-min(A1,A2,A3),value)



    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

else:

    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=W,H
    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0

    value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    for i in range(W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

else:

    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(1,W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=W,H
    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0

    value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    for i in range(1,W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

else:

    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(1,W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=H,W
    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0

    value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    for i in range(1,W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

else:

    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(1,W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=H,W
    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0

    A1=(H//2 +1)* W
    A2=(H//2) * W
    A3=0
    value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    for i in range(1,W):
        A3+=H
        if H%2==0:
            A1-=H//2
        else:
            A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

else:

    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(1,W):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=H,W
    if H%2==0:
        A1=(H//2)* W
        A2=(H//2)* W
        A3=0
    else:
        A1=(H//2 +1)* W
        A2=(H//2)* W
        A3=0

    A1=(H//2 +1)* W
    A2=(H//2) * W
    A3=0
    value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    for i in range(1,W):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

elif H%2==0 or W%2==0:
    if W%2==0:
        H,W=W,H
    A1=(H//2 )* W
    A3=0
    value=A1-A3
    for i in range(1,W):
        A3+=H
        A1-=H//2
        value=min(int(abs(A1-A3)),value)
    print(value)
else:
    A1=(H//2 +1)* W
    A2=(H//2)* W
    A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(1,W):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=H,W
    A1=(H//2 +1)* W
    A2=(H//2) * W
    A3=0
    value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    for i in range(1,W):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

elif H%2==0 or W%2==0:
    if W%2==0:
        H,W=W,H
    A1=(H//2 )* W
    A3=0
    value=A1-A3
    for i in range(1,W//2 +1):
        A3+=H
        A1-=H//2
        value=min(int(abs(A1-A3)),value)
    print(value)
else:
    A1=(H//2 +1)* W
    A2=(H//2)* W
    A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(1,W//2 +1):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=H,W
    A1=(H//2 +1)* W
    A2=(H//2) * W
    A3=0
    for i in range(1,W//2 +1):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)

elif H%2==0 or W%2==0:
    if W%2==0:
        H,W=W,H
    A1=(H//2 )* W
    A3=0
    value=A1-A3
    for i in range(1,W//2 +1):
        A3+=H
        A1-=H//2
        value=min(int(abs(A1-A3)),value)
    print(value)
else:
    A1=(H//2 +1)* W
    A2=(H//2)* W
    A3=0
    value=max(A1,A2,A3)-min(A1,A2,A3)
    for i in range(1,W//2 +1):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)

    H,W=H,W
    A1=(H//2 +1)* W
    A2=(H//2)
    A3=0
    for i in range(1,W//2 +1):
        A3+=H
        A1-=H//2 + 1
        A2-=H//2
        value=min(max(A1,A2,A3)-min(A1,A2,A3),value)
    print(value)

Submission status: Wrong Answer

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

if h*w%3 != 0:
    def div(x,y):
        res = float("inf")
        for i in range(1,x):
            r = x-i
            tmp = (i*y,r*(y//2),r*((y+1)//2))
            res = min(res,max(tmp)-min(tmp))
        return res
    ans = min(div(h,w),div(w,h))

print(ans)

Submission status: Wrong Answer

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

if h*w%3 != 0:
    def div(x,y):
        res = float("inf")
        s = x*y
        lst = [0]*x
        for i in range(1,x): lst[i] = lst[i-1] + y
        for i in range(1,x):
            r = s-lst[i]
            tmp = (r,r//2,r+1//2)
            res = min(res,max(tmp)-min(tmp))
        return res
    ans = min(div(h,w),div(w,h))

print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()

  if h%3==0 or w%3==0:
    return 0

  ans=inf
  for i in range(1,h):
    a=i*w
    b=(h-i)*w//2
    c=h*w-(a+b)
    ans=min(ans,max([a,b,c])-min([a,b,c]))

    a=i*w
    b=(h-i)//2*w
    c=h*w-(a+b)
    ans=min(ans,max([a,b,c])-min([a,b,c]))

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

    a=i*h
    b=(w-i)//2*h
    c=h*w-(a+b)
    ans=min(ans,max([a,b,c])-min([a,b,c]))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()

  if h%3==0 or w%3==0:
    return 0

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

    a=i*w
    b=(h-i)//2*w
    c=-(-(h-i)//2)*w
    ans=min(ans,max([a,b,c])-min([a,b,c]))

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

    a=i*h
    b=(w-i)//2*h
    c=-(-(w-i)//2)*h
    ans=min(ans,max([a,b,c])-min([a,b,c]))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
  print(0)
else:
  if H % 3 == 1:
    x = (H-1)//3
    S2 = (H-x)*((W+1)//2)
  else:
    x = (H+1)//3
    S2 = (H-x)*(W//2)
  S1 = x*W
  if W % 3 == 1:
    x = (W-1)//3
    S4 = (W-x)*((H+1)//2)
  else:
    x = (W+1)//3
    S4 = (W-x)*(H//2)
  S3 = x*H
  print(min(abs(S1-S2), abs(S3-S4)))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
  print(0)
else:
  if H % 3 == 1:
    x = (H-1)//3
  else:
    x = (H+1)//3
  S1 = x*W
  S2 = (H-x)*((W+1)//2)
  if W % 3 == 1:
    x = (W-1)//3
  else:
    x = (W+1)//3
  S3 = x*H
  S4 = (W-x)*((H+1)//2)
  print(min(abs(S1-S2), abs(S3-S4)))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
  print(0)
else:
  if H >= W:
    if H % 3 == 1:
      x = (H-1)//3
    else:
      x = (H+1)//3
    S1 = x*W
    S2 = (H-x)*(W//2)
    print(abs(S1-S2))
  else:
    if W % 3 == 1:
      x = (W-1)//3
    else:
      x = (W+1)//3
    S1 = x*H
    S2 = (W-x)*(H//2)
    print(abs(S1-S2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
res = float("inf")

if H * W % 3:
  for x in range(1, H):
    a = x * W
    b = (H - x) * (W // 2)
    c = (H - x) * W - b
    t = max(a, b, c) - min(a, b, c)
    res = min(res, t)
    #print(a,b,c)
  for x in range(1, W):
    a = x * H
    b = (W - x) * (H // 2)
    c = (W - x) * H - b
    t = max(a, b, c) - min(a, b, c)
    res = min(res, t)
    #print(a,b,c)
  for x in range(1, H):
    a = x * W
    b = W * (H - x) // 2
    c = (H - x) * W - b
    t = max(a, b, c) - min(a, b, c)
    res = min(res, t)
  for x in range(1, W):
    a = x * H
    b = H * (W - x) // 2
    c = (W - x) * H - b
    t = max(a, b, c) - min(a, b, c)
    res = min(res, t)
  print(res)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
res = float("inf")

if H * W % 3:
  for x in range(1, H):
    a = x * W
    b = (H - x) * (W // 2)
    c = (H - x) * W - b
    t = max(a, b, c) - min(a, b, c)
    res = min(res, t)
  for x in range(1, W):
    a = x * H
    b = (W - x) * (H // 2)
    c = (W - x) * H - b
    t = max(a, b, c) - min(a, b, c)
    res = min(res, t)
  print(res)
else:
  print(0)

Submission status: Wrong Answer

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


def cut(H, W):
    ans = 10**10
    for i in range(1, H):
        S1 = i*W

        h = H-i  # 残りの高さ
        if W % 2 == 0 or h % 2 == 0:
            S2 = S3 = h*W//2
        else:
            W, h = max(W, h), min(W, h)
            S2 = (W+1)//2*h
            S3 = (W-1)//2*h

        ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))
    return ans


print(min(cut(H, W), cut(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if not bool(H % 3 and W % 3):
    print(0)
    exit()
ans = float('inf')

n = H
m = H//2
l = H-m
for i in range(1, W):
    S1 = n*i
    S2 = m*(W-i)
    S3 = l*(W-i)
    ans = min(ans, max(S1, S2, S3)-min(S1, S2, S3))
n = W
m = W//2
l = W-m
for i in range(1, H):
    S1 = n*i
    S2 = m*(H-i)
    S3 = l*(H-i)
    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())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if h % 3 == 1:
        a = h//3
        b = a*2
        if w % 2 == 0:
            l1 = [(a+1)*w,b*w//2]
            l2 = [a*w,(b+1)*w//2]
            ans1 = min(max(l1)-min(l1),max(l2)-min(l2))
        else:
            c = w//2
            l1 = [(a+1)*w,b*(c+1),b*c]
            l2 = [a*w,(b+1)*(c+1),(b+1)*c]
            ans1 = min(max(l1)-min(l1),max(l2)-min(l2))
    else:
        a = h//3
        b = h//3*2
        c = w//2
        if w % 2 == 0:
            l1 = [(a+2)*w,b*c]
            l2 = [(a+1)*w,(b+1)*c]
            l3 = [a*w,(b+2)*c]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans1 = min(l4)
        else:
            #print(a,b,c)
            l1 = [(a+2)*w,b*c,b*(c+1)]
            l2 = [(a+1)*w,(b+1)*c,(b+1)*(c+1)]
            l3 = [a*w,(b+2)*c,(b+2)*(c+1)]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans1 = min(l4)
    a = h
    h = w
    w = a
    if h % 3 == 1:
        a = h//3
        b = a*2
        if w % 2 == 0:
            l1 = [(a+1)*w,b*w//2]
            l2 = [a*w,(b+1)*w//2]
            ans2 = min(max(l1)-min(l1),max(l2)-min(l2))
        else:
            c = w//2
            l1 = [(a+1)*w,b*(c+1),b*c]
            l2 = [a*w,(b+1)*(c+1),(b+1)*c]
            ans2 = min(max(l1)-min(l1),max(l2)-min(l2))
    else:
        a = h//3
        b = a*2
        c = w//2
        if w % 2 == 0:
            l1 = [(a+2)*w,b*c]
            l2 = [(a+1)*w,(b+1)*c]
            l3 = [a*w,(b+2)*c]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans2 = min(l4)
        else:
            #print(a,b,c)
            l1 = [(a+2)*w,b*c,b*(c+1)]
            l2 = [(a+1)*w,(b+1)*c,(b+1)*(c+1)]
            l3 = [a*w,(b+2)*c,(b+2)*(c+1)]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans2 = min(l4)
    print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if h % 3 == 1:
        a = h//3
        b = a*2
        if w % 2 == 0:
            l1 = [(a+1)*w,b*w//2]
            l2 = [a*w,(b+1)*w//2]
            ans1 = min(max(l1)-min(l1),max(l2)-min(l2))
        else:
            c = w//2
            l1 = [(a+1)*w,b*(c+1),b*c]
            l2 = [a*w,(b+1)*(c+1),(b+1)*c]
            ans1 = min(max(l1)-min(l1),max(l2)-min(l2))
    else:
        a = h//3
        b = h//3*2
        c = w//2
        if w % 2 == 0:
            l1 = [(a+2)*w,b*c]
            l2 = [(a+1)*w,(b+1)*c]
            l3 = [a*w,(b+2)*c]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans1 = min(l4)
        else:
            l1 = [(a+2)*w,b*c,b*(c+1)]
            l2 = [(a+1)*w,(b+1)*c,(b+1)*(c+1)]
            l3 = [a*w,(b+2)*c,(b+2)*(c+1)]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans1 = min(l4)
    h = a
    h = w
    w = a
    if h % 3 == 1:
        a = h//3
        b = a*2
        if w % 2 == 0:
            l1 = [(a+1)*w,b*w//2]
            l2 = [a*w,(b+1)*w//2]
            ans2 = min(max(l1)-min(l1),max(l2)-min(l2))
        else:
            c = w//2
            l1 = [(a+1)*w,b*(c+1),b*c]
            l2 = [a*w,(b+1)*(c+1),(b+1)*c]
            ans2 = min(max(l1)-min(l1),max(l2)-min(l2))
    else:
        a = h//3
        b = h//3*2
        c = w//2
        if w % 2 == 0:
            l1 = [(a+2)*w,b*c]
            l2 = [(a+1)*w,(b+1)*c]
            l3 = [a*w,(b+2)*c]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans2 = min(l4)
        else:
            l1 = [(a+2)*w,b*c,b*(c+1)]
            l2 = [(a+1)*w,(b+1)*c,(b+1)*(c+1)]
            l3 = [a*w,(b+2)*c,(b+2)*(c+1)]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            ans2 = min(l4)
    print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if w > h:
        a = w
        w = h
        h = a
    if h % 3 == 1:
        a = h//3
        b = a*2
        if w % 2 == 0:
            l1 = [(a+1)*w,b*w//2]
            l2 = [a*w,(b+1)*w//2]
            print(min(max(l1)-min(l1),max(l2)-min(l2)))
        else:
            c = w//2
            l1 = [(a+1)*w,b*(c+1),b*c]
            l2 = [a*w,(b+1)*(c+1),(b+1)*c]
            print(min(max(l1)-min(l1),max(l2)-min(l2)))
    else:
        a = h//3
        b = h//3*2
        c = w//2
        if w % 2 == 0:
            l1 = [(a+2)*w,b*c]
            l2 = [(a+1)*w,(b+1)*c]
            l3 = [a*w,(b+2)*c]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            print(min(l4))
        else:
            l1 = [(a+2)*w,b*c,b*(c+1)]
            l2 = [(a+1)*w,(b+1)*c,(b+1)*(c+1)]
            l3 = [a*w,(b+2)*c,(b+2)*(c+1)]
            l4 = [max(l1)-min(l1),max(l2)-min(l2),max(l3)-min(l3)]
            print(min(l4))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if w > h:
        a = w
        w = h
        h = a
    if h % 3 == 1:
        a = h//3
        b = a*2+1
        if w % 2 == 0:
            print(abs(a*w-b*w//2))
        else:
            print(max(a*w,b*(w//2+1))-b*(w//2))
    else:
        a = h//3+1
        b = h//3*2+1
        if w % 2 == 0:
            print(abs(a*w-b*w//2))
        else:
            print(max(a*w,b*(w//2+1))-b*(w//2))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calc(x, y):
  x1 = x // 2
  x2 = x - x1
  ans = (x % 3) * y
  if x == 2:
    ans = x * y
  for i in range(y):
    s1 = (i + 1) * x
    s2 = (y - i - 1) * x1
    s3 = (y - i - 1) * x2
    s = [s1, s2, s3]
    s.sort()
    cand = s[2] - s[0]
    if cand < ans:
      ans = cand
  return ans

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

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calc(x, y):
  x1 = x // 2
  x2 = x - x1
  ans = (x % 3) * y
  for i in range(y):
    s1 = (i + 1) * x
    s2 = (y - i - 1) * x1
    s3 = (y - i - 1) * x2
    s = [s1, s2, s3]
    s.sort()
    cand = s[2] - s[0]
    if cand < ans:
      ans = cand
  return ans

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

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calc(x, y):
  x1 = x // 2
  x2 = x - x1
  ans = x * y
  for i in range(y):
    s1 = (i + 1) * x
    s2 = (y - i - 1) * x1
    s3 = (y - i - 1) * x2
    s = [s1, s2, s3]
    s.sort()
    cand = s[2] - s[0]
    if cand < ans:
      ans = cand
  return ans

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

Submission status: Wrong Answer

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

h = H % 3
w = W % 3
print(h * w)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    # ||,|-,--,-|全部試す
    h, w = MI()
    a = []
    l1 = w // 3
    for w0 in [l1, l1 + 1]:
        p1 = w0 * h
        w12 = w - w0
        l2 = w12 // 3
        for w1 in [l2, l2 + 1]:
            p2 = w1 * h
            a.append(max(p1, p2, h * w - p1 - p2) - min(p1, p2, h * w - p1 - p2))
        u = h // 2
        for h1 in [u, u + 1]:
            p2 = w12 * h1
            a.append(max(p1, p2, h * w - p1 - p2) - min(p1, p2, h * w - p1 - p2))
    u1 = h // 3
    for h0 in [u1, u1 + 1]:
        p1 = h0 * w
        h12 = h - h0
        u2 = h12 // 2
        for h1 in [u2, u2 + 1]:
            p2 = h1 * w
            a.append(max(p1, p2, h * w - p1 - p2) - min(p1, p2, h * w - p1 - p2))
        l = w // 2
        for w1 in [l, l + 1]:
            p2 = h12 * w1
            a.append(max(p1, p2, h * w - p1 - p2) - min(p1, p2, h * w - p1 - p2))
    print(min(a))

main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    # ||,|-,--,-|全部試す
    h,w=MI()
    a=[]
    l1=int(w/3)
    for w0 in [l1,l1+1]:
        p1=w0*h
        w12=w-w0
        l2=int(w12/3)
        for w1 in [l2,l2+1]:
            p2=w1*h
            a.append(max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
        u=int(h/2)
        for h1 in [u,u+1]:
            p2=w12*h1
            a.append(max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    u1=int(h/3)
    for h0 in [u1,u1+1]:
        p1=h0*w
        h12=h-h0
        u2=int(h12/2)
        for h1 in [u2,u2+1]:
            p2=h1*w
            a.append(max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
        l=int(w/2)
        for w1 in [l,l+1]:
            p2=h12*w1
            a.append(max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    print(min(a))




main()

Submission status: Wrong Answer

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