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())
if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()
ans = 1000000000000000
for i in range(2):
    for h in range(1, H + 1):
        a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
        S_max, S_min = max(max(a, b), c), min(min(a, b), c)

        d, e, f = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
        S_max2, S_min2 = max(max(d, e), f), min(min(d, e), f)

        ans = min(min(ans, abs(S_max - S_min)), abs(S_max2 - S_min2))
    H, W = 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()
S = H * W
ans = S
for i in range(2):
    for h in range(1, H + 1):
        a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
        S_max, S_min = max(max(a, b), c), min(min(a, b), c)

        d, e, f = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
        S_max2, S_min2 = max(max(d, e), f), min(min(d, e), f)

        ans = min(min(ans, abs(S_max - S_min)), abs(S_max2 - S_min2))
    H, W = 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()
S = H * W
ans = S
for i in range(2):
    for h in range(1, H):
        a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
        S_max, S_min = max(max(a, b), c), min(min(a, b), c)

        d, e, f = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
        S_max2, S_min2 = max(max(d, e), f), min(min(d, e), f)

        ans = min(min(ans, abs(S_max - S_min)), abs(S_max2 - S_min2))
    H, W = W, H
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H < W: H, W = W, H
if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()
S = H * W
ans = S
for h in range(1, H):
    a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max, S_min = max(max(a, b), c), min(min(a, b), c)

    d, e, f = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max2, S_min2 = max(max(d, e), f), min(min(d, e), f)

    ans = min(min(ans, abs(S_max - S_min)), abs(S_max2 - S_min2))
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()
S = H * W
ans = S
for h in range(1, H):
    a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max, S_min = max(max(a, b), c), min(min(a, b), c)

    d, e, f = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max2, S_min2 = max(max(d, e), f), min(min(d, e), f)
    
    ans = min(min(ans, abs(S_max - S_min)), abs(S_max2 - S_min2))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H < W: H, W = W, H
S = H * W
ans = S
for h in range(H):
    a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max, S_min = max(max(a, b), c), min(min(a, b), c)

    d, e, f = int(h * W), int((H - h) * W - int(W / 2)), int((H - h) * int(W / 2))
    S_max2, S_min2 = max(max(d, e), f), min(min(d, e), f)
    ans = min(min(ans, abs(S_max - S_min)), abs(S_max2 - S_min2))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()
if H < W: H, W = W, H
S = H * W
ans = S
for h in range(H):
    a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max, S_min = max(max(a, b), c), min(min(a, b), c)

    d, e, f = int(h * W), int((H - h) * W - int(W / 2)), int((H - h) * int(W / 2))
    S_max2, S_min2 = max(max(d, e), f), min(min(d, e), f)
    ans = min(min(ans, abs(S_max - S_min)), abs(S_max2 - S_min2))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
H, W = map(int, input().split())
if H < W: H, W = W, H
if H % 3 == 0 or W % 3 == 0:
    print(0)
    sys.exit()
S = H * W
ans = S
for h in range(H):
    a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max, S_min = max(max(a, b), c), min(min(a, b), c)
    ans = min(ans, abs(S_max - S_min))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
    sys.exit()
S = H * W
ans = S
for h in range(H):
    a, b, c = int(h * W), int((H - h) * (W - int(W / 2))), int((H - h) * int(W / 2))
    S_max, S_min = max(max(a, b), c), min(min(a, b), c)
    ans = min(ans, abs(S_max - S_min))
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(0)
L=max(H,W)
S=min(H,W)
choco=[0]*3

def cutdiv(l,s,div):
  risou=l*s/div
  amari=l*s%div
  cut=risou//s
  if abs((cut+1)*s-risou)<abs(cut*s-risou):
    cut=cut+1
  return cut
  
cutpoint=cutdiv(L,S,3)
choco[0]=cutpoint*S

L-=cutpoint
L2=max(L,S)
S2=min(L,S)
if L2%2==0 or S2%2==0:
  choco[1],choco[2]=L2*S2//2,L2*S2//2
else:
  cutpoint2=cutdiv(L2,S2,2)

  choco[1]=(L2-cutpoint2)*S2
  choco[2]=(cutpoint2)*S2

print(int(max(choco)-min(choco)))

Submission status: Wrong Answer

Solution code:
import sys

if sys.platform in ['ios','win32','darwin']:
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

H, W = MAP()

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

if H == 1 or W == 1:
	print(1)
	sys.exit()

tmp = []
ans = []
for i in range(-10, 11):
	for j in range(-10, 11):
		if i%2:
			H, W = W, H
			a, b, c, d = W//3, W-(W//3), H//2, H-(H//2)
		else:
			a, b, c, d = H//3, H-(H//3), W//2, W-(W//2)
		a += i
		b -= i
		c += j
		d -= j

		A = a*(c+d)
		B = b*c
		C = b*d

		tmp.append([H,W,A,B,C,A+B+C])
		ans.append(max([A,B,C])-min([A,B,C]))

# print(tmp)
# print(ans)
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys

if sys.platform in ['ios','win32','darwin']:
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

H, W = MAP()

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

if H == 1 or W == 1:
	print(1)
	sys.exit()

tmp = []
ans = []
for i in range(4):
	if i%2:
		H, W = W, H
		a, b, c, d = W//3, W-(W//3), H//2, H-(H//2)
	else:
		a, b, c, d = H//3, H-(H//3), W//2, W-(W//2)
	if i==3:
		a += 1
		b -= 1

	A = a*(c+d)
	B = b*c
	C = b*d

	tmp.append([H,W,A,B,C,A+B+C])
	ans.append(max([A,B,C])-min([A,B,C]))

# print(tmp)
# print(ans)
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys

if sys.platform in ['ios','win32','darwin']:
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

H, W = MAP()

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

if H == 1 or W == 1:
	print(1)
	sys.exit()

if H%2 == 0 and W%2 != 0:
	H, W = W, H
	if W%3 == 1:
		a, b, c, d = W//3, W-(W//3), H//2, H-(H//2)
	else:
		a, b, c, d = W//3+1, W-(W//3+1), H//2, H-(H//2)
else:
	if H%3 == 1:
		a, b, c, d = H//3, H-(H//3), W//2, W-(W//2)
	else:
		a, b, c, d = H//3+1, H-(H//3+1), W//2, W-(W//2)
	
# print(a,b,c,d)

A = a*(c+d)
B = b*c
C = b*d

# print(A, B, C)
print(max([A,B,C])-min([A,B,C]))

Submission status: Wrong Answer

Solution code:
import sys

if sys.platform in ['ios','win32','darwin']:
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

H, W = MAP()

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

if H%2 == 0 and W%2 != 0:
	H, W = W, H
	if W%3 == 1:
		a, b, c, d = W//3, W-(W//3), H//2, H-(H//2)
	else:
		a, b, c, d = W//3+1, W-(W//3+1), H//2, H-(H//2)
else:
	if H%3 == 1:
		a, b, c, d = H//3, H-(H//3), W//2, W-(W//2)
	else:
		a, b, c, d = H//3+1, H-(H//3+1), W//2, W-(W//2)
	
# print(a,b,c,d)

A = a*(c+d)
B = b*c
C = b*d

# print(A, B, C)
print(max([A,B,C])-min([A,B,C]))

Submission status: Wrong Answer

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

def func(main,sub):
    al=main*sub
    if main%3==0 or sub%3==0:
        return 0
    a=-(-main//3)#行数
    c=(main-a)*(sub//2)
    b=al-a*sub-c
    aa=(main//3)
    cc=(main-aa)*(sub//2)
    bb=al-aa*sub-cc
    
    if aa*sub==0 or cc==0:
        return (a*sub-c)
    else:
        return min(max(abs(a*sub-c),abs(a*sub-b)),max(abs(aa*sub-cc),abs(aa*sub-bb)))
    

print(min(func(H,W),func(W,H)))

Submission status: Wrong Answer

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

def func(main,sub):
    al=main*sub
    if main%3==0 or sub%3==0:
        return 0
    a=-(-main//3)#行数
    c=(main-a)*(sub//2)
    b=al-a*sub-c
    aa=(main//3)
    cc=(main-aa)*(sub//2)
    bb=al-aa*sub-cc
    
    if aa*sub==0 or cc==0:
        return (a*sub-c)
    else:
        return min(abs(a*sub-c),max(abs(aa*sub-cc),abs(aa*sub-bb)))
    

print(min(func(H,W),func(W,H)))

Submission status: Wrong Answer

Solution code:
import sys
import math
h,w=map(int,input().split())
x=max(h,w)
y=min(h,w)

ans=x*y

for i in range(1,x):
    x1=i
    
    x2=(x-x1)//2
    x3=x-x1-x2
    s1=x1*y
    s2=x2*y
    s3=x3*y
    a_yoko=max(s1,s2,s3)-min(s1,s2,s3)
    
    y2=y//2
    y3=y-y2
    x2=x-x1
    x3=x2
    y1=y
    
    s1=x1*y1
    s2=x2*y2
    s3=x3*y3
    a_tate=max(s1,s2,s3)-min(s1,s2,s3)
    
    ans=min(a_yoko,a_tate,ans)
print(ans)

Submission status: Wrong Answer

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

if h % 3 == 0 or w % 3 ==0:
    print(0)
    sys.exit()
else:
    x=max(h,w)
    y=min(h,w)
    
    x1=math.ceil(x/3)
    x2=x-x1
    x3=x2
    y1=y
    y2=y//2
    y3=y1-y2
    s1=x1*y1
    s2=x2*y2
    s3=x3*y3
    a1=max(s1,s2,s3)-min(s1,s2,s3)
    
    x1=x//3
    x2=x-x1
    x3=x2
    y1=y
    y2=y//2
    y3=y1-y2
    s1=x1*y1
    s2=x2*y2
    s3=x3*y3
    a2=max(s1,s2,s3)-min(s1,s2,s3)
print(min(a1,a2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w=[int(i) for i in input().split()]
ans=10000
for i in range(1,w):
    Smax=i*h
    Smin=(w-i)*(h//2)
    if(Smax-Smin<ans and Smax-Smin>=0):
        ans=Smax-Smin
for i in range(1,h):
    Smax=i*w
    Smin=(h-i)*(w//2)
    if(Smax-Smin<ans and Smax-Smin>=0):
        ans=Smax-Smin

for i in range(1,h):
    Smax=i*w
    Smin=w*((h-i)//2)
    if(Smax-Smin<ans and Smax-Smin>=0):
        ans=Smax-Smin
for i in range(1,w):
    Smax=i*h
    Smin=h*((w-i)//2)
    if(Smax-Smin<ans and Smax-Smin>=0):
        ans=Smax-Smin
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()
elif h==2 and w==2:
  print("1")
  exit()
elif h%2==0 or w%2==0:
  print("2")
  exit()
x = (h*w)//3

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
if h%3==0 or w%3==0:
  print("0")
  exit()
x = (h*w)//3

Submission status: Wrong Answer

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

def yokowari(h, w):
  min_dfr = 10**10
  for h_wareme in range(1, h-1):
    rest_h = h - h_wareme
    rest_w = w
    if rest_h % 2 == 0 or rest_w % 2 == 0:
      rest_s1 = rest_h * rest_w // 2
      rest_s2 = rest_h * rest_w // 2
    elif rest_w >= rest_h:
      rest_s1 = (rest_w // 2) * rest_h
      rest_s2 = (rest_w // 2 + 1) * rest_h
    else:
      rest_s1 = (rest_h // 2) * rest_w
      rest_s2 = (rest_h // 2 + 1) * rest_w
    s = h_wareme * w
    if s == 0 or rest_s1 == 0 or rest_s2 == 0:
      continue
    dfr = max(s, rest_s1, rest_s2) - min(s, rest_s1, rest_s2)
    min_dfr = min(min_dfr, dfr)
#    print("h", h, "w", w, "hwa", h_wareme, "S", h_wareme*w, "s", rest_s1, rest_s2)
  return min_dfr

"""
def yokowari(h, w):
  o_th = h * w / 3
  print(o_th, h, o_th / h)
  h_wareme = round(o_th / h)
  rest_h = h - h_wareme
  rest_w = w
  print("", rest_h, rest_w)
  if rest_h % 2 == 0 or rest_w % 2 == 0:
    rest_s = rest_h * rest_w // 2
  elif rest_w >= rest_h:
    rest_s = (resr_w // 2) * rest_h
  else:
    rest_s = (resr_h // 2) * rest_w
  dfr = h_wareme * w - rest_s
  print(h_wareme * w, rest_s ,dfr)
  print()
  return abs(dfr)
"""
if h % 3 == 0 or w % 3 == 0:
  print(0)
  sys.exit()
else:
  print(min(yokowari(h, w), yokowari(w, h)))

Submission status: Wrong Answer

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

def yokowari(h, w):
  min_dfr = 10**10
  for h_wareme in range(1, h-1):
    rest_h = h - h_wareme
    rest_w = w
    if rest_h % 2 == 0 or rest_w % 2 == 0:
      rest_s1 = rest_h * rest_w // 2
      rest_s2 = rest_h * rest_w // 2
    elif rest_w >= rest_h:
      rest_s1 = (rest_w // 2) * rest_h
      rest_s2 = (rest_w // 2 + 1) * rest_h
    else:
      rest_s1 = (rest_h // 2) * rest_w
      rest_s2 = (rest_h // 2 + 1) * rest_w
    s = h_wareme * w
    dfr = max(s, rest_s1, rest_s2) - min(s, rest_s1, rest_s2)
    min_dfr = min(min_dfr, dfr)
#    print("h", h, "w", w, "hwa", h_wareme, "S", h_wareme*w, "s", rest_s1, rest_s2)
  return min_dfr

"""
def yokowari(h, w):
  o_th = h * w / 3
  print(o_th, h, o_th / h)
  h_wareme = round(o_th / h)
  rest_h = h - h_wareme
  rest_w = w
  print("", rest_h, rest_w)
  if rest_h % 2 == 0 or rest_w % 2 == 0:
    rest_s = rest_h * rest_w // 2
  elif rest_w >= rest_h:
    rest_s = (resr_w // 2) * rest_h
  else:
    rest_s = (resr_h // 2) * rest_w
  dfr = h_wareme * w - rest_s
  print(h_wareme * w, rest_s ,dfr)
  print()
  return abs(dfr)
"""
if h % 3 == 0 or w % 3 == 0:
  print(0)
  sys.exit()
else:
  print(min(yokowari(h, w), yokowari(w, h)))

Submission status: Wrong Answer

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

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

ans=min(H,W)

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

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

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

s_org = h * w

answer = h * w
for i in range(2):
  if i == 1:
    h, w = w, h
  for height in range(1, h+1):
    s1 = height * w
    remain_height = h - height
    s2 = remain_height * int(w / 2)
    s3 = s_org - (s1 + s2)
    s_max = max([s1, s2, s3])
    s_min = min([s1, s2, s3])
    answer = min([answer, (s_max - s_min)])

    s1 = height * w
    s2 = (h - height) * w
    s3 = s_org - (s1 + s2)
    s_max = max([s1, s2, s3])
    s_min = min([s1, s2, s3])
    answer = min([answer, (s_max - s_min)])
  
print(answer)

Submission status: Wrong Answer

Solution code:
from math import floor, ceil

H,W = map(int,input().split())
ans = float('inf')

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

for x in range(1,H):
  S = [W*x, (H-x)*floor(W/2), (H-x)*ceil(W/2)]
  ans = min(ans, abs(max(S)-min(S)))

for x in range(1,W):
  S = [H*x, (W-x)*floor(H/2), (W-x)*ceil(H/2)]
  ans = min(ans, abs(max(S)-min(S)))

print(ans)

Submission status: Wrong Answer

Solution code:
from math import floor, ceil

H,W = map(int,input().split())
ans = float('inf')

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

for x in range(1,H+1):
  S = [W*x, (H-x)*floor(W/2), (H-x)*ceil(W/2)]
  ans = min(ans, abs(max(S)-min(S)))

for x in range(1,W+1):
  S = [H*x, (W-x)*floor(H/2), (W-x)*ceil(H/2)]
  ans = min(ans, abs(max(S)-min(S)))

print(ans)

Submission status: Wrong Answer

Solution code:

def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return

    if H % 2 == 0 or W % 2 == 0:
        if H % 2 == 0:
            tmp = H
            H = W
            W = tmp
        w = W // 2
        a = 1
        while 2*w*H - 3*w*a > 0:
            a += 1
        print(min(2*w*H-3*w*(a-1), 3*w*a-2*w*H))
        return

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return

    if H % 2 == 0 or W % 2 == 0:
        if H % 2 == 0:
            tmp = H
            H = W
            W = H
        w = W // 2
        a = 1
        while 2*w*H - 3*w*a > 0:
            a += 1
        print(min(2*w*H-3*w*(a-1), 3*w*a-2*w*H))
        return

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return

    if H % 2 == 0 or W % 2 == 0:
        if H % 2 == 0:
            tmp = H
            H = W
            W = H
        w = W // 2
        a = 1
        while 2*w*H - 3*w*a > 0:
            a += 1
        print(min(2*w*H-3*w*(a-1), 3*w*a-2*w*H))
        return

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return

    if H % 2 == 0 or W % 2 == 0:
        if H % 2 == 0:
            tmp = H
            H = W
            W = H
        w = W // 2
        a = 1
        while 2*w*H - 3*w*a > 0:
            a += 1
        print(min(2*w*H-3*w*(a-1), 3*w*a-2*w*H))
        return

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    h, w = list(map(int, readline().split()))
    if h % 3 == 0 or w % 3 == 0:
        print(0)
    else:
        mt = 10 ** 10
        for i in range(1, h - 1):
            y = i * w
            x = (h * w - i * w) // w * (w // 2)
            z = (h * w - i * w) // w * ((w + 2 - 1) // 2)
            mt = min(mt, abs(max(x, y, z) - min(x, y, z)))
        for i in range(1, w - 1):
            y = i * h
            x = (h * w - i * h) // h * (h // 2)
            z = (h * w - i * h) // h * ((h + 2 - 1) // 2)
            mt = min(mt, abs(max(x, y, z) - min(x, y, z)))
        print(mt)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
num = list(map(int, input().split(' ')))
tate, yoko = num[0], num[1]
#tate = max(num)
#yoko = min(num)

ans = [0, 0]
chocolate = [0, 0]
if tate % 3 == 0 or yoko % 3 == 0:
    ans = [0, 0]
    
else:
    for i in range(len(num)):
        first_tate = (tate + 1) // 3
        first = first_tate * yoko
        second_yoko = (yoko + 1) // 2
        second = (tate - first_tate) * second_yoko
        third = tate * yoko - first - second
        chocolate[i] = [first, second, third]
        ans[i] = max(chocolate[i]) - min(chocolate[i])
        tate = num[1 - i]
        yoko = num[i]

print(min(ans))

Submission status: Wrong Answer

Solution code:
num = list(map(int, input().split(' ')))
tate = max(num)
yoko = min(num)

ans = 0
if tate % 3 == 0 or yoko % 3 == 0:
    ans == 0
else:
    first_tate = (tate + 1) // 3
    first = first_tate * yoko
    second_yoko = (yoko + 1) // 2
    second = (tate - first_tate) * second_yoko
    third = tate * yoko - first - second
    chocolate = [first, second, third]
    ans = max(chocolate) - min(chocolate)

print(ans)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
w,h=max(x,y),min(x,y)
if h*w%3==0:
  print(0)
  exit()
a,b,c=0,0,0
ans=h
for i in [0,1]:
  a=h*(w//3+i)
  b=(h//2)*(w-w//3-i)
  c=h*w-a-b
  ans=min(max(a,b,c)-min(a,b,c),ans)
print(ans)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
w,h=max(x,y),min(x,y)
if h*w%3==0:
  print(0)
  exit()
a,b,c=0,0,0
ans=min(h,w)
for i in [0,1]:
  a=h*(w//3+i)
  b=(h//2)*(w-w//3-i)
  c=h*w-a-b
  ans=max(a,b,c)-min(a,b,c)
print(ans)

Submission status: Wrong Answer

Solution code:
x,y=map(int,input().split())
w,h=max(x,y),min(x,y)
if h*w%3==0:
  print(0)
  exit()
a,b,c=0,0,0
ans=min(h,w)
for i in [0,1]:
  a=h*(w//3+i)
  b=(h//2)*(w-w//3-i)
  c=h*w-a-b
  print(a,b,c)
  ans=max(a,b,c)-min(a,b,c)
print(ans)

Submission status: Wrong Answer

Solution code:
def pat1(H,W):
    if H % 3 == 0:
        return 0
    else:
        return W

def pat2(H,W):
    if W % 3 == 0:
        return 0
    else:
        return H

def pat3(H,W):
    if W == 2:
        return -1

    h1 = H // 2
    h2 = H - h1
    ans = 10**15 
    for w in range(1,W//2+1):
        s1 = h1 * (W-w)
        s2 = h2 * (W-w)
        s3 = H*w
        min1 = min(s1,s2,s3)
        max1 = max(s1,s2,s3)
        ans = min(ans,max1-min1)
    return ans

def pat4(H,W):
    if H == 2:
        return -1
    w1 = W // 2
    w2 = W - w1
    ans = 10**15
    for h in range(1,H//2+1):
        s1 = w1*(H-h)
        s2 = w2*(H-h)
        s3 = W*h
        min1 = min(s1,s2,s3)
        max1 = max(s1,s2,s3)
        ans = min(ans,max1-min1)
    return ans

H,W = map(int,input().split())
ans = 10**15
ans = min(ans,pat1(H,W))
ans = min(ans,pat2(H,W))
if W > 2:
    ans = min(ans,pat3(H,W))
if H > 2:
    ans = min(ans,pat4(H,W))

print(ans)

Submission status: Wrong Answer

Solution code:
H, W = list(map(int,input().split()))
ans = H*W
h = H
w = W
for i in range(h//3,h//2+1):
    p1 = i*w
    for j in range((h-i)//2-1,(h-i)//2+1):
        p2 = (h-i-j)*W
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    for j in range(w//2-1,w//2+1):
        p2 = (h-i)*(w-j)
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
h = W
w = H
for i in range(h//3,h//2+1):
    p1 = i*w
    for j in range((h-i)//2-1,(h-i)//2+1):
        p2 = (h-i-j)*W
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    for j in range(w//2-1,w//2+1):
        p2 = (h-i)*(w-j)
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = list(map(int,input().split()))
ans = H*W
h = H
w = W
for i in range(h//3,h//2+1):
    p1 = i*w
    for j in range((h-i)//2,(h-i)//2+1):
        p2 = (h-i-j)*W
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    for j in range(w//2,w//2+1):
        p2 = (h-i)*(w-j)
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
h = W
w = H
for i in range(h//3,h//2+1):
    p1 = i*w
    for j in range((h-i)//2,(h-i)//2+1):
        p2 = (h-i-j)*W
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
    for j in range(w//2,w//2+1):
        p2 = (h-i)*(w-j)
        ans = min(ans, max(p1,p2,h*w-p1-p2)-min(p1,p2,h*w-p1-p2))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if h % 3 == 1:
    d = h // 3 
else:
    d = h // 3 + 1
block = [w * d, (h-d) * (w // 2), (h-d) * (w - w // 2)]
res1 = max(block) - min(block)

if w % 3 == 1:
    d = w // 3 
else:
    d = w // 3 + 1
block = [h * d, (w-d) * (h // 2), (w-d) * (h - h // 2)]
res2 = max(block) - min(block)

print(min(res1, res2))

Submission status: Wrong Answer

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

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

            yoko_third = (h//2)*remain_w
            yoko_second = h*remain_w-yoko_third

            answer = min(answer,max(abs(yoko_third-yoko_second),abs(first-yoko_third)),max(abs(tate_third-tate_second),abs(first-tate_third)))

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

            yoko_third = (remain_h//2)*w
            yoko_second = remain_h*w-yoko_third

            answer = min(answer,max(abs(yoko_third-yoko_second),abs(first-yoko_third)),max(abs(tate_third-tate_second),abs(first-tate_third)))
    print(answer)

    return

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

            yoko_third = (h//2)*remain_w
            yoko_second = h*remain_w-yoko_third

            answer = min(answer,max(abs(yoko_third-yoko_second),abs(first-yoko_third)),max(abs(tate_third-tate_second),abs(first-tate_third)))

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

            yoko_third = (remain_h//2)*w
            yoko_second = remain_h*w-yoko_third

            answer = min(answer,max(abs(yoko_third-yoko_second),abs(first-yoko_third)),max(abs(tate_third-tate_second),abs(first-tate_third)))
    print(answer)

    return

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

            yoko_third = (h//2)*remain_w
            yoko_second = h*remain_w-yoko_third

            answer = min(answer,max(abs(yoko_third-yoko_second),abs(first-yoko_third)),max(abs(tate_third-tate_second),abs(first-tate_third)))

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

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

    return

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

if __name__ == '__main__':
    main()