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: Accepted

Solution code:
h, w = map(int, input().split())
div1 = 0
div2 = 0
if h%3==0 or w%3==0:
	print("0")
else:
	div1 = min(h, w)
	def div2(h, w):
		m = h*w
		for l in range(1, w):
			s = [l*h, (w-l)*(h-h//2), (w-l)*(h//2)]
			m = min(m, max(s)-min(s))
		return m
	div2 = min(div2(h, w), div2(w, h))
	print(min(div1, div2))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
div1 = 0
div2 = 0
if h%3==0 or w%3==0:
	print("0")
else:
	div1 = min(h, w)

	def div2(h, w):
		m = h*w
		for l in range(1, w):
			s = [l*h, (w-l)*(h-h//2), (w-l)*(h//2)]
			m = min(m, max(s)-min(s))
		return m

	div2 = min(div2(h, w), div2(w, h))
	print(min(div1, div2))

Submission status: Accepted

Solution code:
x, y = map(int, input().split())

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

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

ans1 = h

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

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

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

ans2 = h

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

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

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
height, width = map(int, input().strip().split())


def get_min(H, W):
    min_whole = 100000000000
    for h in range(1, H):
        S_a = h * W
        w = W // 2
        S_b = (H - h) * w
        S_c = (H - h) * (W - w)
        pat1 = max(S_a, S_b, S_c) - min(S_a, S_b, S_c)
        pat2 = 1000000000

        h_b = (H - h) // 2
        S_b = h_b * W
        S_c = (H - h - h_b) * W
        tmp = max(S_a, S_b, S_c) - min(S_a, S_b, S_c)
        if pat2 > tmp:
            pat2 = tmp
        min_loop = min(pat1, pat2)
        if min_whole > min_loop:
            min_whole = min_loop
    return min_whole


port = get_min(height, width)
land = get_min(width, height)
print(min(port, land))

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

    h_res = H - h

    B1 = W*(h_res//2)
    C1 = W*(h_res - h_res//2)

    diff1 = max(abs(A - B1), abs(A - C1), abs(B1 - C1))

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

    ans = min(ans, diff1, diff2)

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

    w_res = W - w

    B1 = w_res*(H//2)
    C1 = w_res*(H - H//2)

    diff1 = max(abs(A - B1), abs(A - C1), abs(B1 - C1))

    B2 = H* (w_res//2)
    C2 = H * (w_res - w_res//2)
    diff2 = max(abs(A - B2), abs(A - C2), abs(B2 - C2))

    ans = min(ans, diff1, diff2)


print(ans)

Submission status: Accepted

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

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

else:
    result = []
    for i in range(1, H):
        s1 = i * W
        if (H - i) % 2 == 0 or W % 2 == 0:
            s2 = (H - i) * W // 2
            result.append(max(s1, s2) - min(s1, s2))
        else:
            s2 = (H - i) // 2 * W
            s3 = (H - i) * W - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))
            s2 = (H - i) * (W // 2)
            s3 = (H - i) * W - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))

    for i in range(1, W):
        s1 = i * H
        if (W - i) % 2 == 0 or H % 2 == 0:
            s2 = (W - i) * H // 2
            result.append(max(s1, s2) - min(s1, s2))
        else:
            s2 = (W - i) // 2 * H
            s3 = (W - i) * H - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))
            s2 = (W - i) * (H // 2)
            s3 = (W - i) * H - s2
            result.append(max(s1, s2, s3) - min(s1, s2, s3))

    print(min(result))

Submission status: Accepted

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

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

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

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

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

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

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

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

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

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

    ans = min(anss)

print(ans)

Submission status: Accepted

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

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

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

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

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

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

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

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

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

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

        ans = min(anss)

    print(ans)
    
main()

Submission status: Accepted

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

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

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

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

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

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

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

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

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

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

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

        ans = min(anss)

    print(ans)
main()

Submission status: Accepted

Solution code:
import math
h, w = map(int, input().split())
if (h * w) % 3 == 0:
    print(0)
    exit()
if h % 3 == 1:
    k = h + 2
if h % 3 == 2:
    k = h + 1
if w % 3 == 1:
    v = w + 2
if w % 3 == 2:
    v = w + 1

if h % 2 == 0:
    m = h
if h % 2 == 1:
    m = h + 1
if w % 2 == 0:
    u = w
if w % 2 == 1:
    u = w + 1

s11 = k // 3 * w
s12 = (h - k // 3) * (math.floor(w / 2))
s13 = (h - k // 3) * (math.ceil(w/ 2))
s1 = max(s11, s12, s13) - min(s11, s12, s13)

t11 = (k // 3 - 1) * w
t12 = (h - k // 3 + 1) * (math.floor(w / 2))
t13 = (h - k // 3 + 1) * (math.ceil(w/ 2))
t1 = max(t11, t12, t13) - min(t11, t12, t13)

s21 = (k // 3) * w
s22 = (k // 3 - 1) * w
if h % 3 == 1:
    s23 = (k // 3 - 1) * w
else:
    s23 = (k // 3) * w
s2 = max(s21, s22, s23) - min(s21, s22, s23)

s31 = v // 3 * h
s32 = (w - v // 3) * (math.floor(h / 2))
s33 = (w - v // 3) * (math.ceil(h / 2))
s3 = max(s31, s32, s33) - min(s31, s32, s33)

t31 = (v // 3 - 1) * h
t32 = (w - v // 3 + 1) * (math.floor(h / 2))
t33 = (w - v // 3 + 1) * (math.ceil(h / 2))
t3 = max(t31, t32, t33) - min(t31, t32, t33)

s41 = (v // 3) * h
s42 = (v // 3 - 1) * h
if h % 3 == 1:
    s43 = (v // 3 - 1) * h
else:
    s43 = (v // 3) * h
s4 = max(s41, s42, s43) - min(s41, s42, s43)

print(min(s1, s2, s3, s4, t1, t3))

Submission status: Accepted

Solution code:
# メインアイデア
# T分割と||分割で差が小さい方を探す
# 90度回転させた場合も考える
# 総合的に小さい方を採用する
# 図的な解説は別のPDFをアップロードしてある
H, W = list(map(int, input().split()))

# 3の倍数の場合は即座に0とわかる
if (H * W) % 3 == 0:
    print(0)
    exit()


def try_divi(W, H):
    # パターン1
    err = W
    # パターン2
    w2, w3 = W // 2, W // 2
    if W % 2:
        w2 += 1
    for h1 in range(1, H // 2 + 1):
        h2 = H-h1
        S1, S2, S3 = h1*W, w2*h2, w3*h2
        new_err = max(S1, S2, S3) - min(S1, S2, S3)
        if new_err < err:
            err = new_err
    return err


print(min(try_divi(W, H), try_divi(H, W)))

Submission status: Accepted

Solution code:
# メインアイデア
# T分割と||分割で差が小さい方を探す
# 90度回転させた場合も考える
# 総合的に小さい方を採用する
# 図的な解説は別のPDFをアップロードしてある
import sys
H, W = list(map(int, input().split()))

# 3の倍数の場合は即座に0とわかる
if (H * W) % 3 == 0:
    print(0)
    exit()


def try_divi(W, H):
    # パターン1
    err = W
    # パターン2
    w2, w3 = W // 2, W // 2
    if W % 2:
        w2 += 1
    for h1 in range(1, H // 2 + 1):
        h2 = H-h1
        S1, S2, S3 = h1*W, w2*h2, w3*h2
        new_err = max(S1, S2, S3) - min(S1, S2, S3)
        if new_err < err:
            err = new_err
    return err


print(min(try_divi(W, H), try_divi(H, W)))

Submission status: Accepted

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

def divide(h, w):
    ans = h * w
    for hi in range(1, h):
        #saを決める
        sa = hi * w

        #w分割
        wm = w // 2
        sb = (h - hi) * wm
        sc = (h - hi) * (w - wm)
        ans1 = max(sa, sb, sc) - min(sa, sb, sc)
        #(h-hi)分割
        hm = (h - hi) // 2
        sb = hm * w
        sc = (h - hi - hm) * w
        ans2 = max(sa, sb, sc) - min(sa, sb, sc)

        ans = min(ans, ans1, ans2)
    return ans

print(min(divide(h, w), divide(w, h)))

Submission status: Accepted

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

def divide(h, w):
  ans = h*w
  for w1 in range(1, w):
    s1 = h*w1

    w2 = (w-w1)//2
    s21 = h*w2
    s31 = h*(w-w1-w2)
    ans1 = max(s1, s21, s31) - min(s1, s21, s31)

    h2 = h//2
    s22 = (w-w1)*h2
    s32 = (w-w1)*(h-h2)
    ans2 = max(s1, s22, s32) - min(s1, s22, s32)
    ans = min(ans, ans1, ans2)
  return ans

print(min(divide(h, w), divide(w, h)))

Submission status: Accepted

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

def divide(h, w):
  ans = h*w
  for w1 in range(1, w):
    s1 = h*w1
    # Pattern1 : ||
    w2 = (w-w1)//2
    s21 = h*w2
    s31 = h*(w-w1-w2)
    ans1 = max(s1, s21, s31) - min(s1, s21, s31)
    # Pattern2 : |-
    h2 = h//2
    s22 = (w-w1)*h2
    s32 = (w-w1)*(h-h2)
    ans2 = max(s1, s22, s32) - min(s1, s22, s32)
    ans = min(ans, ans1, ans2)
  return ans
    
print(min(divide(h, w), divide(w, h)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=float('inf')
for i in range(1,h):
    a1=min(i*w,w*((h-i)//2))
    b1=max(i*w,w*(h-i-(h-i)//2))
    d1=b1-a1
    a2=min(i*w,(h-i)*(w//2))
    b2=max(i*w,(h-i)*(w-w//2))
    d2=b2-a1
    s=min(s,d1,d2)
for j in range(1,w):
    a1=min(j*h,h*((w-j)//2))
    b1=max(j*h,h*(w-j-(w-j)//2))
    d1=b1-a1
    a2=min(j*h,(w-j)*(h//2))
    b2=max(j*h,(w-j)*(h-h//2))
    d2=b2-a2
    s=min(s,d1,d2)
print(s)

Submission status: Accepted

Solution code:
def separate(a,b):
    r=[]
    tmp=[a//3*b,(a-a//3)*(b//2),(a-a//3)*(b//2+b%2)]
    r.append(max(tmp)-min(tmp))
    tmp=[(a//3+1)*b,(a-a//3-1)*(b//2),(a-a//3-1)*(b//2+b%2)]
    r.append(max(tmp)-min(tmp))
    return min(r)

H,W=list(map(int,input().split()))
ans=0

if H%3==0 or W%3==0:
    ans=0
else:
    S=[H,W]
    S.append(separate(H,W))
    S.append(separate(W,H))
    ans=min(S)
print(ans)

Submission status: Accepted

Solution code:
import math

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

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

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

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

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

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


print(min_num)

Submission status: Accepted

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

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

calc_ans(H,W)

Submission status: Accepted

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

S_list = []
for h in range(1, H):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
H, W = W, H
for h in range(1, H):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
print(min(S_list))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
ans = 1e10
for w in range(1, W):
    rec1 = [H * w]
    rec1.append((W-w) * (H//2))
    rec1.append((W-w) * (H - H//2))
    dif = max(rec1) - min(rec1)
    ans = min(ans, dif)
    
    rec2 = [H*w]
    rec2.append((W-w)//2 * H)
    rec2.append( (W-w-(W-w)//2) * H )
    dif2 = max(rec2) - min(rec2)
    ans = min(ans, dif2)
    
for h in range(1, H):
    rec1 = [h*W]
    rec1.append((H-h) * (W//2))
    rec1.append((H-h) * (W - W//2))
    dif = max(rec1) - min(rec1)
    ans = min(ans, dif)
    
    rec2 = [h*W]
    rec2.append((H-h)//2 * W)
    rec2.append( (H-h-(H-h)//2) * W )
    dif2 = max(rec2) - min(rec2)
    ans = min(ans, dif2)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
ans=h*w
for x,y in ((h,w),(w,h)):
    x1=x//3
    x2=(x+2)//3
    y2=(y+1)//2
    ans=min(ans,abs(y*x2-y*x1))
    t=(y*x1,y2*(x-x1),(y-y2)*(x-x1))
    ans=min(ans,max(t)-min(t))
    t=(y*x2,y2*(x-x2),(y-y2)*(x-x2))
    ans=min(ans,max(t)-min(t))
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

if H%3==0 or W%3==0:
    print(0)
else:
    ans5=W
    ans6=H
    ans1=max(int(H/3)*W,(H-int(H/3))*int(W/2),H*W-int(H/3)*W-(H-int(H/3))*int(W/2))-min(int(H/3)*W,(H-int(H/3))*int(W/2),H*W-int(H/3)*W-(H-int(H/3))*int(W/2))
    ans2=max((int(H/3)+1)*W,(H-int(H/3)-1)*int(W/2),H*W-(int(H/3)+1)*W-(H-int(H/3)-1)*int(W/2))-min((int(H/3)+1)*W,(H-int(H/3)-1)*int(W/2),H*W-(int(H/3)+1)*W-(H-int(H/3)-1)*int(W/2))
    ans3=max(int(W/3)*H,(W-int(W/3))*int(H/2),W*H-int(W/3)*H-(W-int(W/3))*int(H/2))-min(int(W/3)*H,(W-int(W/3))*int(H/2),W*H-int(W/3)*H-(W-int(W/3))*int(H/2))
    ans4=max((int(W/3)+1)*H,(W-int(W/3)-1)*int(H/2),H*W-(int(W/3)+1)*H-(W-int(W/3)-1)*int(H/2))-min((int(W/3)+1)*H,(W-int(W/3)-1)*int(H/2),H*W-(int(W/3)+1)*H-(W-int(W/3)-1)*int(H/2))
    print(min(ans1,ans2,ans3,ans4,ans5,ans6))

Submission status: Accepted

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

ah = round(h / 3)
aw = w // 2
bh = h // 2
bw = round(w / 3)

ans1_area_1 = ah * w
ans1_area_2 = (h - ah) // 2 * w
ans1_area_3 = (h - (h - ah) // 2 - ah) * w
ans1 = max(ans1_area_1, ans1_area_2, ans1_area_3) - min(ans1_area_1, ans1_area_2, ans1_area_3)


ans2_area_1 = ah * w
ans2_area_2 = (h - ah) * aw
ans2_area_3 = (h - ah) * (w - aw)
ans2 = max(ans2_area_1, ans2_area_2, ans2_area_3) - min(ans2_area_1, ans2_area_2, ans2_area_3)

ans3_area_1 = bw * h
ans3_area_2 = (w - bw) // 2 * h
ans3_area_3 = (w - (w - bw) // 2 - bw) * h
ans3 = max(ans3_area_1, ans3_area_2, ans3_area_3) - min(ans3_area_1, ans3_area_2, ans3_area_3)

ans4_area_1 = bw * h
ans4_area_2 = (w - bw) * bh
ans4_area_3 = (w - bw) * (h - bh)
ans4 = max(ans4_area_1, ans4_area_2, ans4_area_3) - min(ans4_area_1, ans4_area_2, ans4_area_3)

print(min(ans1, ans2, ans3, ans4))

Submission status: Accepted

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

ans = H * W
for i in range(H):
	l_1 = [(i+1) * W, (H - i - 1) * (W//2), (H - i - 1) * (W-(W//2))]
	l_2 = [(i+1) * W, ((H - i - 1)//2) * W, (H-i-1 -((H - i - 1)//2)) * W]
	ans = min(ans, max(l_1) - min(l_1), max(l_2) - min(l_2))


H, W = W, H
for i in range(H):
	l_1 = [(i+1) * W, (H - i - 1) * (W//2), (H - i - 1) * (W-(W//2))]
	l_2 = [(i+1) * W, ((H - i - 1)//2) * W, (H-i-1 -((H - i - 1)//2)) * W]
	ans = min(ans, max(l_1) - min(l_1), max(l_2) - min(l_2))



print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
H, W = map(int, input().split())

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

diff_list = [W,H]

#縦→横
split1 = [W//3, W//3 + 1]
split2 = [H//2, H//2 +1]

for w in split1:
    for h in split2:
        temp = [H*w, h*(W-w), (H-h)*(W-w)]
        diff_list.append(abs(max(temp)-min(temp)))

#横→縦
if H != W:
    split1 = [H//3, H//3 + 1]
    split2 = [W//2, W//2 + 1]

    for h in split1:
        for w in split2:
            temp = [h*W, (H-h)*w, (H-h)*(W-w)]
            diff_list.append(abs(max(temp)-min(temp)))

# print(diff_list)
print(min(diff_list))

Submission status: Accepted

Solution code:
def 解():
    iH,iW = [int(_) for _ in input().split()]

    if iH % 3 == 0 or iW % 3 == 0:
        print(0)
    else:
        w3 = iW // 3
        w2 = iW // 2
        h3 = iH // 3
        h2 = iH // 2
        aY1 = [iH*w3,(iH-h2)*(iW-w3),h2*(iW-w3)]
        aY2 = [iH*(w3+1),(iH-h2)*(iW-w3-1),h2*(iW-w3-1)]
        aT1 = [iW*h3,(iW-w2)*(iH-h3),w2*(iH-h3)]
        aT2 = [iW*(h3+1),(iW-w2)*(iH-h3-1),w2*(iH-h3-1)]
        aH31 = [iW*h3,iW*(iH-2*h3)]
        aH32 = [iW*(h3+1),iW*(iH-2*(h3+1))]
        aW31 = [iH*w3,iH*(iW-2*w3)]
        aW32 = [iH*(w3+1),iH*(iW-2*(w3+1))]

        iMin = iH*iW
        for ea in [aY1,aY2,aT1,aT2,aH31,aH32,aW31,aW32]:
            iMin = min(iMin, max(ea) - min(ea))
        print(iMin)
解()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
def Scal(H, W):
    for i in range(1, (H//2)+1):
        S1 = W * i
        S2 = W * ((H-i)//2)
        S3 = H * W - S1 - S2
        s1 = [S1, S2, S3]
        S.append(s1)
        S2 = W//2 * (H-i)
        S3 = H * W - S1 - S2
        s2 = [S1, S2, S3]
        S.append(s2)
    
Scal(H, W)
Scal(W, H)
Sdif = []
for i in range(len(S)):
    Sdif.append(max(S[i])-min(S[i]))
print(min(Sdif))

Submission status: Accepted

Solution code:
from math import floor,ceil
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,W):
    M = max(i*H,floor(H/2)*(W-i),ceil(H/2)*(W-i))
    m = min(i*H,floor(H/2)*(W-i),ceil(H/2)*(W-i))
    if M-m<ans:
        ans = M-m
for i in range(1,H):
    M = max(i*W,floor(W/2)*(H-i),ceil(W/2)*(H-i))
    m = min(i*W,floor(W/2)*(H-i),ceil(W/2)*(H-i))
    if M-m<ans:
        ans = M-m
print(ans)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = [h//2+w//3+1, h//3+w//2+1, h, w]
if h%3 == 0 or w%3 == 0:
  s += [0]
if h%2 == 0:
  s += [h//2]
if w%2 == 0:
  s += [w//2]
print(min(s))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
if H%3==0 or W%3==0:
    print(0)
else:
    minH = float("inf")
    for i in range(1, H):
        A = W * i
        BH = W * ((H-i)//2)
        CH = H*W - A - BH
        BW = W//2 * (H-i)
        CW = H*W - A - BW
        Hdiff = max(A, BH, CH) - min(A, BH, CH)
        Wdiff = max(A, BW, CW) - min(A, BW, CW)
        minH = min(minH, min(Hdiff, Wdiff))

    minW = float("inf")
    for i in range(1, W):
        A = H * i
        BH = H * ((W-i)//2)
        CH = H*W - A - BH
        BW = H//2 * (W-i)
        CW = H*W - A - BW
        Hdiff = max(A, BH, CH) - min(A, BH, CH)
        Wdiff = max(A, BW, CW) - min(A, BW, CW)
        minW = min(minW, min(Hdiff, Wdiff))

    print(min(minH, minW))

Submission status: Accepted

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

min_p = 10 ** 6
for i in range(1, W):
    a = H * i
    b = H * ((W - i) // 2)
    c = H * W - a - b
    if max(a, b, c) - min(a, b, c) < min_p:
        min_p = max(a, b, c) - min(a, b, c)
    a = H * i
    b = (H // 2) * (W - i)
    c = H * W - a - b
    if max(a, b, c) - min(a, b, c) < min_p:
        min_p = max(a, b, c) - min(a, b, c)

for i in range(1, H):
    a = W * i
    b = W * ((H - i) // 2)
    c = W * H - a - b
    if max(a, b, c) - min(a, b, c) < min_p:
        min_p = max(a, b, c) - min(a, b, c)
    a = W * i
    b = (W // 2) * (H - i)
    c = W * H - a - b
    if max(a, b, c) - min(a, b, c) < min_p:
        min_p = max(a, b, c) - min(a, b, c)

print(min_p)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
def keisan1(H, W):
    kotae = 10**9
    for h in range(1, H):
        SA = h*W
            
        if W % 2 == 0:
            SB = (H-h) * (W//2)
            SC = (H-h) * (W//2)
        else:
            SB = (H-h) * (W//2)
            SC = (H-h) * ((W//2)+1)
        kotae = min(kotae, abs(max(SA, SB, SC)-min(SA, SB, SC)))
    
    return kotae
        
def keisan2(H, W):
    kotae=10**9
    if H >= 3:
        for h in range(1, H):
            SA = h*W
            
            if (H-h) % 2 == 0:
                SB = W*((H-h)//2)
                SC = W*((H-h)//2)
            else:
                SB = W*((H-h)//2)
                SC = W*(((H-h)//2)+1)
            kotae = min(kotae, abs(max(SA, SB, SC)-min(SA, SB, SC)))
    else:
        kotae = 10**9
    
    return kotae
        
print(min(keisan1(H, W), keisan1(W, H), keisan2(H, W), keisan2(W, H)))

Submission status: Accepted

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

ans = 10**10

if w >= 3:
    if w % 3 == 0:
        ans = 0
    else:
        ans = min(ans, h)

if h >= 3:
    if h % 3 == 0:
        ans = 0
    else:
        ans = min(ans, w)

for i in range(1, h):
    s1 = i*w
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w - w//2)
    smax = max(max(s1, s2), s3)
    smin = min(min(s1, s2), s3)

    ans = min(ans, smax - smin)

for i in range(1, w):
    s1 = i*h
    s2 = (w-i)*(h//2)
    s3 = (w-i)*(h - h//2)
    smax = max(max(s1, s2), s3)
    smin = min(min(s1, s2), s3)

    ans = min(ans, smax - smin)

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

h,w = li()

# タテに3つ
if w%3 == 0:
    pat1 = 0
else:
    pat1 = h

# ヨコに3つ
if h%3 == 0:
    pat2 = 0
else:    
    pat2 = w

# T形に分ける
h1to = h//3
h2to = h-h1to

wto = w//2

choco1 = h1to * w
choco2 = h2to*wto
choco3 = h2to*(w-wto)

pat3 = max(choco1,choco2,choco3) - min(choco1,choco2,choco3)

h1to = h//3+1
h2to = h-h1to

wto = w//2

choco1 = h1to * w
choco2 = h2to*wto
choco3 = h2to*(w-wto)

pat4 = max(choco1,choco2,choco3) - min(choco1,choco2,choco3)


# ト型に分ける
w1t = w//3
w2t = w - w1t

ht = h//2

choco1 = w1t * h
choco2 = w2t * ht
choco3 = w2t * (h-ht)

pat5 = max(choco1,choco2,choco3) - min(choco1,choco2,choco3)


w1t = w//3+1
w2t = w - w1t

ht = h//2

choco1 = w1t * h
choco2 = w2t * ht
choco3 = w2t * (h-ht)

pat6 = max(choco1,choco2,choco3) - min(choco1,choco2,choco3)

print(min(pat1,pat2,pat3,pat4,pat5,pat6))