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:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



h, w = LI()
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ret = INF
    for y in range(1, h):
        ret = min(ret, max(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)) - min(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)))
    h, w = w, h
    for y in range(1, h):
        ret = min(ret, max(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)) - min(y * w, w // 2 * (h - y), (w - w // 2) * (h - y)))
    print(ret)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h*w%3==0:
  print(0)
else:
  a=sorted([(h//3)*w,(h-h//3)*(w//2),(h-h//3)*(w//2+w%2)])
  c=sorted([(w//3)*h,(w-w//3)*(h//2),(w-w//3)*(h//2+h%2)])
  print(min(h,w,abs(a[2]-a[0]),abs(c[2]-c[0])))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a=h*w
if h==1 or w==1:
  print(1 if h*w%3!=0 else 0)
  exit()
for i in range(1,-(-h//3)+1):
  A=sorted([i*w,(h-i)//2*w,((h-i)//2+(h-1)%2)*w])
  B=sorted([i*w,(h-i)*(w//2),(h-i)*(w//2+w%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
for i in range(1,-(-w//3)+1):
  A=sorted([i*h,(w-i)//2*h,((w-i)//2+(w-1)%2)*h])
  B=sorted([i*h,(w-i)*(h//2),(w-i)*(h//2+h%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
print(a)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a=h*w
if h==1 or w==1:
  print(1 if h*w%3!=0 else 0)
  exit()
for i in range(-(-h//3)+1):
  A=sorted([i*w,(h-i)//2*w,((h-i)//2+(h-1)%2)*w])
  B=sorted([i*w,(h-i)*(w//2),(h-i)*(w//2+w%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
for i in range(-(-w//3)+1):
  A=sorted([i*h,(w-i)//2*h,((w-i)//2+(w-1)%2)*h])
  B=sorted([i*h,(w-i)*(h//2),(w-i)*(h//2+h%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
print(a)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a=h*w
if h==1 or w==1:
  print(1)
  exit()
for i in range(-(-h//3)+1):
  A=sorted([i*w,(h-i)//2*w,((h-i)//2+(h-1)%2)*w])
  B=sorted([i*w,(h-i)*(w//2),(h-i)*(w//2+w%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
for i in range(-(-w//3)+1):
  A=sorted([i*h,(w-i)//2*h,((w-i)//2+(w-1)%2)*h])
  B=sorted([i*h,(w-i)*(h//2),(w-i)*(h//2+h%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
print(a)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a=h*w
if h==1 or w==1:
  print(h*w%3)
  exit()
for i in range(-(-h//3)+1):
  A=sorted([i*w,(h-i)//2*w,((h-i)//2+(h-1)%2)*w])
  B=sorted([i*w,(h-i)*(w//2),(h-i)*(w//2+w%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
for i in range(-(-w//3)+1):
  A=sorted([i*h,(w-i)//2*h,((w-i)//2+(w-1)%2)*h])
  B=sorted([i*h,(w-i)*(h//2),(w-i)*(h//2+h%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
print(a)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a=h*w
for i in range(-(-h//3)+1):
  A=sorted([i*w,(h-i)//2*w,((h-i)//2+(h-1)%2)*w])
  B=sorted([i*w,(h-i)*(w//2),(h-i)*(w//2+w%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
for i in range(-(-w//3)+1):
  A=sorted([i*h,(w-i)//2*h,((w-i)//2+(w-1)%2)*h])
  B=sorted([i*h,(w-i)*(h//2),(w-i)*(h//2+h%2)])
  a=min(a,abs(A[2]-A[0]),abs(B[2]-B[0]))
print(a)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
h,w=H//2,W//3
s=sorted([w*H,(W-w)*(H-h),(W-w)*h])
a=s[2]-s[0]
s=sorted([(w+1)*H,(W-w-1)*(H-h),(W-w-1)*h])
a=min(s[2]-s[0],a)
s=sorted([(w+2)*H,(W-w-2)*(H-h),(W-w-2)*h])
a=min(s[2]-s[0],a)
a=min(H*(W-3*w),a)
h,w=H//3,W//2
s=sorted([W*h,(W-w)*(H-h),w*(H-h)])
a=min(s[2]-s[0],a)
s=sorted([W*(h+1),(W-w)*(H-h-1),w*(H-h-1)])
a=min(s[2]-s[0],a)
s=sorted([W*(h+2),(W-w)*(H-h-2),w*(H-h-2)])
a=min(s[2]-s[0],a)
a=min((H-3*h)*W,a)
print(a)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
h,w=H//2,W//3
s=sorted([w*H,(W-w)*(H-h),(W-w)*h])
a=s[2]-s[0]
s=sorted([(w+1)*H,(W-w-1)*(H-h),(W-w-1)*h])
a=min(s[2]-s[0],a)
a=min(H*(W-3*w),a)
h,w=H//3,W//2
s=sorted([W*h,(W-w)*(H-h),w*(H-h)])
a=min(s[2]-s[0],a)
s=sorted([W*(h+1),(W-w)*(H-h-1),w*(H-h-1)])
a=min(s[2]-s[0],a)
a=min((H-3*h)*W,a)
print(a)

Submission status: Wrong Answer

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

if H % 3 == 0:
    ans = 0
else:
    ans = min(ans, W)

if W % 3 == 0:
    ans = 0
else:
    ans = min(ans, H)

for i in range(1, H):
    if W % 2 == 0:
        dif = abs(i*W - (H-i)*(W//2))
    else:
        dif = max(i*W, (H-i)*(W//2)+1) - min(i*W, (H-i)*(W//2))
    ans = min(ans, dif)

for i in range(1, W):
    if H % 2 == 0:
        dif = abs(i*H - (W-i)*(H//2))
    else:
        dif = max(i*H, (W-i)*(H//2)+1) - min(i*H, (W-i)*(H//2))
    ans = min(ans, dif)

print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0:
    ans = 0
else:
    ans = min(ans, W)

if W % 3 == 0:
    ans = 0
else:
    ans = min(ans, H)

tmp = H // 3
for i in range(tmp, tmp+2):
    if W % 2 == 0:
        dif = abs(i*W - (H-i)*(W//2))
    else:
        dif = max(i*W, (H-i)*(W//2)+1) - min(i*W, (H-i)*(W//2))
    ans = min(ans, dif)

tmp = W // 3
for i in range(tmp, tmp+2):
    if H % 2 == 0:
        dif = abs(i*H - (W-i)*(H//2))
    else:
        dif = max(i*H, (W-i)*(H//2)+1) - min(i*H, (W-i)*(H//2))
    ans = min(ans, dif)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
S, L = sorted(list(map(int, input().split())))

if S % 3 == 0 or L % 3 == 0:
	print(0)
	sys.exit()

if L % 3 == 1:
	a_1 = math.floor(L/3)
else:
	a_1 = math.ceil(L/3)

a = a_1*S


LL = L - a_1

sec_S,sec_L = sorted([S,LL])

if sec_S % 2 == 0:
	b = sec_L * (sec_S//2)
else:
	b = sec_S * (sec_L//2)

S_min, S_mid, S_max = sorted([a, b, S*L-(a+b)])
print(S_max-S_min)

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline

def main():
    H, W = map(int, input().split())

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

    ans = int(1e10)
    for h in range(1, H):
        s1 = h * W
        tmp = (H - h) // 2
        s2 = tmp * W
        s3 = (H - h - tmp) * W
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
        tmp = W // 2
        s2 = tmp * (H - h)
        s3 = (W - tmp) * (H - h)
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
    for w in range(1, W):
        s1 = w * H
        tmp = (W - w) // 2
        s2 = tmp * H
        s3 = (W - w - tmp) * H
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
        tmp = H // 2
        s2 = (W - w) * tmp
        s3 = (W - w) * (H - tmp)
        ans = min(ans, max((s1, s2, s3)) - min((s1, s2, s3)))
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]
ret = 10 ** 9 + 7

def f(a, b, c):
  s = [a[0] * a[1], b[0] * b[1], c[0] * c[1]]
  return max(s) - min(s)

a, b, c = [H // 3] * 3
if H % 3 > 0:
  a += 1
if H % 3 == 2:
  b += 1

ret = min(ret, f([a, W],[b, W],[c, W]))

a, b, c = [W // 3] * 3
if W % 3 > 0:
  a += 1
if W % 3 == 2:
  b += 1

a, b = [W // 2] * 2
if W % 2 == 1:
  a += 1
for i in range(1, H):
  t = f([a, H - i],[b, H - i],[i, W])
  ret = min(ret, t)
  
a, b = [H // 2] * 2
if H % 2 == 1:
  a += 1
for i in range(1, W):
  t = f([a, W - i],[b, W - i],[i, H])
  ret = min(ret, t)
  

ret = min(ret, f([a, H],[b, H],[c, H]))

print(ret)

Submission status: Wrong Answer

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

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

ans = H * W * 2

# 最初に縦に割る場合
for i in range(1, W):
    h2 = H // 2
    # 右側を半分にする場合
    s1 = i * H
    s2 = (W - i) * h2
    s3 = (W - i) * (H - h2)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)
    # 左側を半分にする場合
    s1 = (W - i) * H
    s2 = i * h2
    s3 = i * (H - h2)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)

# 最初に横に割る場合
for i in range(1, H):
    w2 = W // 2
    # 下側を半分にする場合
    s1 = W * i
    s2 = w2 * (H - i)
    s3 = (W - w2) * (H - i)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)
    # 上側を半分にする場合
    s1 = W * (H - i)
    s2 = w2 * i
    s3 = (W - w2) * i
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)

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 = H * W

# 最初に縦に割る場合
for i in range(1, W):
    h2 = H // 2
    # 右側を半分にする場合
    s1 = i * H
    s2 = (W - i) * h2
    s3 = (W - i) * (H - h2)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)
    # 左側を半分にする場合
    s1 = (W - i) * H
    s2 = i * h2
    s3 = i * (H - h2)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)

# 最初に横に割る場合
for i in range(1, H):
    w2 = W // 2
    # 下側を半分にする場合
    s1 = W * i
    s2 = w2 * (H - i)
    s3 = (W - w2) * (H - i)
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)
    # 上側を半分にする場合
    s1 = W * (H - i)
    s2 = w2 * i
    s3 = (W - w2) * i
    diff = max(s1, s2, s3) - min(s1, s2, s3)
    ans = min(ans, diff)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    L = [[H,W], [W,H]]
    ans = 1e10
    for H,W in L: 
        Hp = [H, math.ceil(H/2), math.floor(H/2)]
        for x in range(1, W):
            S = [Hp[0]*x, Hp[1]*(W-x), Hp[2]*(W-x)]
            temp = max(S) - min(S)
            if ans > temp:  ans = temp
            else:   break
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    if W < H:   H,W = W,H
    ans = 1e10
    Hp = [H, math.ceil(H/2), math.floor(H/2)]
    for x in range(1, W):
        S = [Hp[0]*x, Hp[1]*(W-x), Hp[2]*(W-x)]
        temp = max(S) - min(S)
        if ans > temp:  ans = temp
    print(ans)

Submission status: Wrong Answer

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

ds = a * b

s0 = (a//3) * b
s1 = (a - (a//3)) * (b//2)
s2 = (a - (a//3)) * (b - (b//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))

s0 = (a//3 + 1) * b
s1 = (a - (a//3) - 1) * (b//2)
s2 = (a - (a//3) - 1) * (b - (b//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))

s0 = (b//3) * a
s1 = (b - (b//3)) * (a//2)
s2 = (b - (b//3)) * (a - (a//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))

s0 = (b//3 + 1) * a
s1 = (b - (b//3) - 1) * (a//2)
s2 = (b - (b//3) - 1) * (a - (a//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))

print(ds)

Submission status: Wrong Answer

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

ds = a * b

s0 = (a//3) * b
s1 = (a - (a//3)) * (b//2)
s2 = (a - (a//3)) * (b - (b//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))
  print(s0, s1, s2)

s0 = (a//3 + 1) * b
s1 = (a - (a//3) - 1) * (b//2)
s2 = (a - (a//3) - 1) * (b - (b//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))
  print(s0, s1, s2)

s0 = (b//3) * a
s1 = (b - (b//3)) * (a//2)
s2 = (b - (b//3)) * (a - (a//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))
  print(s0, s1, s2)

s0 = (b//3 + 1) * a
s1 = (b - (b//3) - 1) * (a//2)
s2 = (b - (b//3) - 1) * (a - (a//2))
if 0 not in (s0, s1, s2):
  ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))
  print(s0, s1, s2)

print(ds)

Submission status: Wrong Answer

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

s0 = (a//3) * b
s1 = (a - (a//3)) * (b//2)
s2 = (a - (a//3)) * (b - (b//2))
ds = max(s0, s1, s2) - min(s0, s1, s2)

s0 = (a//3 + 1) * b
s1 = (a - (a//3) - 1) * (b//2)
s2 = (a - (a//3) - 1) * (b - (b//2))
ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))

s0 = (b//3) * a
s1 = (b - (b//3)) * (a//2)
s2 = (b - (b//3)) * (a - (a//2))
ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))

s0 = (b//3 + 1) * a
s1 = (b - (b//3) - 1) * (a//2)
s2 = (b - (b//3) - 1) * (a - (a//2))
ds = min(ds, max(s0, s1, s2) - min(s0, s1, s2))

print(ds)

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    ans = 1e20
    for i in range(max(W//3-1, 1), min(W//3+2, W)):
      a = i
      b = W - a
      for i in range(max(H//2-1, 1), min(H//2+2, H)):
        c = i
        d = H - c
        S = [a*H, b*c, b*d]
        ans = min(ans, max(S)-min(S))
      for i in range(max(b//2-1, 1), min(b//2+2, b)):
        c = i
        d = b - c
        S = [a*H, b*H, b*H]
        ans = min(ans, max(S)-min(S))
  return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    ans = 1e20
    for i in range(max(W//3-1, 1), min(W//3+2, W)):
      a = i
      b = W - a
      for i in range(max(H//2-1, 1), min(H//2+2, H)):
        c = i
        d = H - c
        S = [a*H, b*c, b*d]
        ans = min(ans, max(S)-min(S))
  return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    ans = 1e20
    for i in range(max(W//3-1, 1), min(W//3+2, W+1)):
      a = i
      b = W - a
      for i in range(max(H//2-1, 1), min(H//2+2, H+1)):
        c = i
        d = H - c
        S = [a*H, b*c, b*d]
        ans = min(ans, max(S)-min(S))
  return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    ans = 1e20
    for i in range(W//3-1, W//2+2):
      a = i
      b = W - a
      for i in range(H//2-1, H//2+2):
        c = i
        d = H - c
        S = [a*H, b*c, b*d]
        ans = min(ans, max(S)-min(S))
  return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    min_ = 1e10
    a = 0
    ans = 1e20
    for i in range(1, W+1):
      a = i
      b = W - a
      target = H/2
      for i in range(min(int(target)-1, 1), min(int(target)+2, H+1)):
        if abs(i-target) < min_:
          c = i
          min_ = abs(i-target)
      d = H - c
      S = [a*H, b*c, b*d]
    ans = min(ans, max(S)-min(S))
  return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    min_ = 1e10
    a = 0
    ans = 1e20
    for i in range(W+1):
      a = i
      b = W - a
      target = H/2
      for i in range(min(int(target)-1, 1), min(int(target)+2, H+1)):
        if abs(i-target) < min_:
          c = i
          min_ = abs(i-target)
      d = H - c
      S = [a*H, b*c, b*d]
    ans = min(ans, max(S)-min(S))
  return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    min_ = 1e10
    a = 0
    ans = 1e20
    for i in range(W+1):
      a = i
      b = W - a
      target = H/2
      for i in range(min(int(target)-1, 2), min(int(target)+2, H+1)):
        if abs(i-target) < min_:
          c = i
          min_ = abs(i-target)
      d = H - c
      S = [a*H, b*c, b*d]
    ans = min(ans, max(S)-min(S))
  return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    target = W/3
    min_ = 1e10
    a = 0
    for i in range(min(int(target)-1, 2), min(int(target)+2, W+1)):
      if abs(i-target) < min_:
        a = i
        min_ = abs(i-target)
    b = W - a
    target = H/2
    min_ = 1e20
    for i in range(min(int(target)-1, 2), min(int(target)+2, H+1)):
      if abs(i-target) < min_:
        c = i
        min_ = abs(i-target)
    d = H - c
    S = [a*H, b*c, b*d]
    return max(S)-min(S)

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
H, W = sorted([int(x) for x in input().split()])

if H % 3 == 0 or W % 3 == 0:
  print(0)  
else:
  target = W/3
  min_ = 1e10
  a = 0
  for i in range(min(int(target)-1, 2), min(int(target)+2, W+1)):
    if abs(i-target) < min_:
      a = i
      min_ = abs(i-target)
  b = W - a
  target = H/2
  min_ = 1e20
  for i in range(min(int(target)-1, 2), min(int(target)+2, H+1)):
    if abs(i-target) < min_:
      c = i
      min_ = abs(i-target)
  d = H - c
  S = [a*H, b*c, b*d]
  print(max(S)-min(S))

Submission status: Wrong Answer

Solution code:
H, W = sorted([int(x) for x in input().split()])

if H % 3 == 0 or W % 3 == 0:
  print(0)  
else:
  target = W/3
  min_ = 1e10
  a = 0
  for i in range(int(target)-1, int(target)+2):
    if abs(i-target) < min_:
      a = i
      min_ = abs(i-target)
  b = W - a
  target = H/2
  min_ = 1e20
  for i in range(int(target)-1, int(target)+2):
    if abs(i-target) < min_:
      c = i
      min_ = abs(i-target)
  d = H - c
  S = [a*H, b*c, b*d]
  print(max(S)-min(S))

Submission status: Wrong Answer

Solution code:
H, W = sorted([int(x) for x in input().split()])

if H % 3 == 0 or W % 3 == 0:
  print(0)  
else:
  target = W/3
  min_ = 1e10
  a = 0
  for i in range(int(target)-1, int(target)+1):
    if abs(i-target) < min_:
      a = i
      min_ = abs(i-target)
  b = W - a
  target = H/2
  min_ = 1e20
  for i in range(int(target)-1, int(target)+1):
    if abs(i-target) < min_:
      c = i
      min_ = abs(i-target)
  d = H - c
  S = [a*H, b*c, b*d]
  print(max(S)-min(S))

Submission status: Wrong Answer

Solution code:
# 2019/09/03

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

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


def solve(s):
    global h,w
    
    cnt=float('inf')
    if s==h:
        H,W=w,h
    else:
        H,W=h,w
    for i in range(1,s+1):
        a=i*H
        rest=H*(w-i)
        if rest%2==0:
            b=c=rest//2
        else:
            ww=w-i
            hh=H
            bb=max(ww,hh)//2
            cc=bb+1
            b=min(ww,hh)*bb
            c=min(ww,hh)*cc
        cnt=min((max(a,b,c)-min(a,b,c)),cnt)

    return cnt


ans=min(solve(h),solve(w))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int, input().split())
a=[]
b=[]
c=[]
d=[]

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

a.append(h//3*w)
a.append(w//2*(h-h//3))
a.append((w-w//2)*(h-h//3))
ans1=max(a)-min(a)

b.append((h//3+1)*w)
b.append(w//2*(h-(h//3+1)))
b.append((w-w//2)*(h-(h//3+1)))
ans2=max(b)-min(b)

c.append(w//3*h)
c.append(h//2*(w-h//3))
c.append((h-w//2)*(w-h//3))
ans3=max(c)-min(c)

d.append((w//3+1)*h)
d.append(h//2*(w-(h//3+1)))
d.append((h-w//2)*(w-(h//3+1)))
ans4=max(d)-min(d)

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

Submission status: Wrong Answer

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

ans_list = list()
# S_list = list()
# h1
for h1 in {h // 3 - 1, h // 3, h // 3 + 1}:
    h_rest = h - h1
    if h1 <= 0 or h_rest <= 0:
        continue
    # h2
    h2 = h_rest >> 1
    if h2 != 0:
        S = [h1 * w, h2 * w, (h_rest - h2) * w]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)

    # w2
    w2 = w >> 1
    if w2 != 0:
        S = [h1 * w, h_rest * w2, h_rest * (w - w2)]
        # S_list.append(S)
        ans_list.append(max(S) - min(S))


for w1 in {w // 3 - 1, w // 3, w // 3 + 1}:
    w_rest = w - w1
    if w1 <= 0 or w_rest <= 0:
        continue
    # h2
    h2 = h >> 1
    if h2 != 0:
        S = [h * w1, h2 * w_rest, (h - h2) * w_rest]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)
    # w2
    w2 = w >> 1
    if w2 != 0:
        S = [h * w1, h * w2, h * (w_rest - w2)]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)

# print(ans_list)
# print(S_list)
print(min(ans_list))

Submission status: Wrong Answer

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

ans_list = list()
# S_list = list()
# h1
for h1 in {h // 3, h // 3 + 1}:
    h_rest = h - h1
    if h1 == 0 or h_rest == 0:
        continue
    # h2
    h2 = h_rest >> 1
    if h2 != 0:
        S = [h1 * w, h2 * w, (h_rest - h2) * w]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)

    # w2
    w2 = w >> 1
    if w2 != 0:
        S = [h1 * w, h_rest * w2, h_rest * (w - w2)]
        # S_list.append(S)
        ans_list.append(max(S) - min(S))


for w1 in {w // 3, w // 3 + 1}:
    w_rest = w - w1
    if w1 == 0 or w_rest == 0:
        continue
    # h2
    h2 = h >> 1
    if h2 != 0:
        S = [h * w1, h2 * w_rest, (h - h2) * w_rest]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)
    # w2
    w2 = w >> 1
    if w2 != 0:
        S = [h * w1, h * w2, h * (w_rest - w2)]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)

# print(ans_list)
# print(S_list)
print(min(ans_list))

Submission status: Wrong Answer

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

ans_list = list()
# S_list = list()
# h1
for h1 in {h // 3, h // 3 + 1}:
    if h1 == 0:
        continue
    h_rest = h - h1
    # h2
    h2 = h_rest >> 1
    if h2 != 0:
        S = [h1 * w, h2 * w, (h_rest - h2) * w]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)

    # w2
    w2 = w >> 1
    if w2 != 0:
        S = [h1 * w, h_rest * w2, h_rest * (w - w2)]
        # S_list.append(S)
        ans_list.append(max(S) - min(S))


for w1 in {w // 3, w // 3 + 1}:
    S = list()
    w_rest = w - w1
    # h2
    h2 = h >> 1
    if h2 != 0:
        S = [h * w1, h2 * w_rest, (h - h2) * w_rest]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)
    # w2
    w2 = w >> 1
    if w2 != 0:
        S = [h * w1, h * w2, h * (w_rest - w2)]
        ans_list.append(max(S) - min(S))
        # S_list.append(S)

# print(ans_list)
# print(S_list)
print(min(ans_list))

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
ans = []

#目,円
for i in range(1, H):
    area1 = W*i
    area2 = (H-i)//2*W
    area3 = (H-(H-i)//2)*W
    area4 = (H-i)*(W//2)
    area5 = (H-i)*(W-(W//2))
    if area1 != 0 or area2 != 0 or area3 !=0:
        ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    if area1 != 0 or area4 != 0 or area5 !=0:
        ans.append(max(area1, area4, area5)-min(area1, area4, area5))

#皿、ト
for i in range(1, W):
    area6 = H*i
    area7 = (W-i)//2*H
    area8 = (W-(W-i)//2)*H
    area9 = (W-i)*(H//2)
    area10 = (W-i)*(H-(H//2))
    if area6 != 0 or area7 != 0 or area8 !=0:
        ans.append(max(area6, area7, area8)-min(area6, area7, area8))
    if area6 != 0 or area9 != 0 or area10 !=0:
        ans.append(max(area6, area9, area10)-min(area6, area9, area10))

print(min(ans))

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
ans = []

#目,円
for i in range(1, H):
    area1 = W*i
    area2 = (H-i)//2*W
    area3 = (H-(H-i)//2)*W
    area4 = (H-i)*(W//2)
    area5 = (H-i)*(W-(W//2))
    if area1 != 0 or area2 != 0 or area3 !=0:
        ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    if area1 != 0 or area4 != 0 or area5 !=0:
        ans.append(max(area1, area4, area5)-min(area1, area4, area5))

#皿、ト
for i in range(1, W):
    area1 = H*i
    area2 = (W-i)//2*H
    area3 = (W-(W-i)//2)*H
    area4 = (W-i)*(H//2)
    area5 = (W-i)*(H-(H//2))
    if area1 != 0 or area2 != 0 or area3 !=0:
        ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    if area1 != 0 or area4 != 0 or area5 !=0:
        ans.append(max(area1, area4, area5)-min(area1, area4, area5))

print(min(ans))

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
ans = []

#目,円
for i in range(1, H):
    area1 = W*i
    area2 = (H-i)//2*W
    area3 = (H-(H-i)//2)*W
    area4 = (H-i)*(W//2)
    area5 = (H-i)*(W-(W//2))
    if area1 != 0 and area2 != 0 and area3 !=0:
        ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    if area1 != 0 and area4 != 0 and area5 !=0:
        ans.append(max(area1, area4, area5)-min(area1, area4, area5))

#皿、ト
for i in range(1, W):
    area1 = H*i
    area2 = (W-i)//2*H
    area3 = (W-(W-i)//2)*H
    area4 = (W-i)*(H//2)
    area5 = (W-i)*(H-(H//2))
    if area1 != 0 and area2 != 0 and area3 !=0:
        ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    if area1 != 0 and area4 != 0 and area5 !=0:    
        ans.append(max(area1, area4, area5)-min(area1, area4, area5))

print(min(ans))

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
ans = []

#目,円
for i in range(1, H+1):
    area1 = W*i
    area2 = (H-i)//2*W
    area3 = (H-(H-i)//2)*W
    area4 = (H-i)*(W//2)
    area5 = (H-i)*(W-(W//2))
    if area1 != 0 and area2 != 0 and area3 !=0:
        ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    if area1 != 0 and area4 != 0 and area5 !=0:
        ans.append(max(area1, area4, area5)-min(area1, area4, area5))

#皿、ト
for i in range(1, W+1):
    area1 = H*i
    area2 = (W-i)//2*H
    area3 = (W-(W-i)//2)*H
    area4 = (W-i)*(H//2)
    area5 = (W-i)*(H-(H//2))
    if area1 != 0 and area2 != 0 and area3 !=0:
        ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    if area1 != 0 and area4 != 0 and area5 !=0:    
        ans.append(max(area1, area4, area5)-min(area1, area4, area5))

print(min(ans))

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
ans = []

#目,円
for i in range(1, H+1):
    area1 = W*i
    area2 = (H-i)//2*W
    area3 = (H-(H-i)//2)*W
    area4 = (H-i)*(W//2)
    area5 = (H-i)*(W-(W//2))
    ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    ans.append(max(area1, area4, area5)-min(area1, area4, area5))

#皿、ト
for i in range(1, W+1):
    area1 = H*i
    area2 = (W-i)//2*H
    area3 = (W-(W-i)//2)*H
    area4 = (W-i)*(H//2)
    area5 = (W-i)*(H-(H//2))
    ans.append(max(area1, area2, area3)-min(area1, area2, area3))
    ans.append(max(area1, area4, area5)-min(area1, area4, area5))

print(min(ans))

Submission status: Wrong Answer

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

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

sa = 10**100

for i in range(2):
    s1 = w * (yoko + i)
    if w % 2 == 0:
        s2 = (h-yoko-i) * (w//2)
        sa = min(sa,abs(s1-s2))
    else:
        s2 = (h-yoko-i) * (w//2)
        s3 = (h-yoko-i) * ((w//2)+1)
        sa = min(sa,max(abs(s1-s2),abs(s1-s3),abs(s3-s2)))

tmp = h
h = w
w = tmp

yoko = h // 3

for i in range(2):
    s1 = w * (yoko + i)
    if w % 2 == 0:
        s2 = (h-yoko-i) * (w//2)
        sa = min(sa,abs(s1-s2))
    else:
        s2 = (h-yoko-i) * (w//2)
        s3 = (h-yoko-i) * ((w//2)+1)
        sa = min(sa,max(abs(s1-s2),abs(s1-s3),abs(s3-s2)))

        
print(sa)

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline

h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    c_1 = ((w // 3 + 1) * h) - ((w - 1 - w // 3) * (h // 2))
    c_2 = ((w - w // 3) * (h - h // 2)) - (w // 3 * h)
    c_3 = h
    c_4 = ((h // 3 + 1) * w) - ((h - 1 - h // 3) * (w // 2))
    c_5 = ((h - h // 3) * (w - w // 2)) - (h // 3 * w)
    ans = min(c_1, c_2, c_3, c_4, c_5)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline

h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    c_1 = (w // 3 + 1) * h - ((w - 1 - w // 3) * (h // 2))
    c_2 = ((w - w // 3) * (h - h // 2)) - (w // 3 * h)
    c_3 = h
    c_4 = (h // 3 + 1) * w - ((h - 1 - h // 3) * (w // 2))
    c_5 = ((h - h // 3) * (w - w // 2)) - (h // 3 * w)
    ans = min(c_1, c_2, c_3, c_4, c_5)
print(ans)

Submission status: Wrong Answer

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

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

ans = float('inf')
for _ in range(2):
  for h in range(1,H//2+1):
    a = W*h
    b = (H-h)*(W//2)
    c = (H-h)*(W-W//2)
    ans = min(max(a,b,c)-min(a,b,c), ans)
  H,W = W,H
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

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

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

bx = min(H, W) // 2
cx = min(H, W) - bx
ans = 10 ** 18
for i in range(1, max(H, W)):
    a = min(H, W) * i

    b = (max(H, W) - i) * bx
    c = (max(H, W) - i) * cx

    ans = min(ans, max(a, b, c) - min(a, b, c))

bx = max(H, W) // 2
cx = max(H, W) - bx
for i in range(1, min(H, W)):
    a = max(H, W) * i

    b = (min(H, W) - i) * bx
    c = (min(H, W) - i) * cx

    ans = min(ans, max(a, b, c) - min(a, b, c))

aa = H // 3
bb = aa
cc = H - aa - bb
ans = min(ans, (max(aa, bb, cc) - min(aa, bb, cc)) * W)

aa = W // 3
bb = aa
cc = W - aa - bb
ans = min(ans, (max(aa, bb, cc) - min(aa, bb, cc)) * H)


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

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

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

bx = min(H, W) // 2
cx = min(H, W) - bx
ans = 10 ** 18
for i in range(1, max(H, W)):
    a = min(H, W) * i

    b = (max(H, W) - i) * bx
    c = (max(H, W) - i) * cx

    ans = min(ans, max(a, b, c) - min(a, b, c))

bx = max(H, W) // 2
cx = max(H, W) - bx
for i in range(1, min(H, W)):
    a = max(H, W) * i

    b = (min(H, W) - i) * bx
    c = (min(H, W) - i) * cx

    ans = min(ans, max(a, b, c) - min(a, b, c))

print(ans)