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:
def solve():
    ans = 1000000
    for i in range(2):
        if H[i] % 3 == 0:
            return 0
        H1 = H[i] // 3
        S_max = (H1 + 1) * H[1 - i]
        S_min = (H[i] - H1 - 1) * (H[1 - i] // 2)
        ans = min(ans, S_max - S_min)
        S_min = H1 * H[1 - i]
        S_max = (H[i] - H1) * ((H[1 - i] + 1) // 2)
        ans = min(ans, S_max - S_min)
    return ans

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

Submission status: Wrong Answer

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

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

x = w//4
Sb =max(x*h,h//2*(w-x),(h-h//2)*(w-x))
Ss =min(x*h,h//2*(w-x),(h-h//2)*(w-x))
ans1 =Sb-Ss
x += 1
Sb =max(x*h,h//2*(w-x),(h-h//2)*(w-x))
Ss =min(x*h,h//2*(w-x),(h-h//2)*(w-x))
ans2 =Sb-Ss
y = h//4
Sb =max(y*w,w//2*(h-y),(w-w//2)*(h-y))
Ss =min(y*w,w//2*(h-y),(w-w//2)*(h-y))
ans3 =Sb-Ss
y += 1
Sb =max(y*w,w//2*(h-y),(w-w//2)*(h-y))
Ss =min(y*w,w//2*(h-y),(w-w//2)*(h-y))
ans4 =Sb-Ss

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

Submission status: Wrong Answer

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

diff = float("inf")

# divide H
if H >= 3:
    q, r = divmod(H, 3)
    if r == 1 or r == 2:
        diff = min(diff, abs(W * q - W * (q + 1)))
    else:
        diff = min(diff, abs(W * q - W * q))

# divide W
if W >= 3:
    q, r = divmod(W, 3)
    if r == 1 or r == 2:
        diff = min(diff, abs(H * q - H * (q + 1)))
    else:
        diff = min(diff, abs(H * q - H * q))

for w in range(W // 2 + 1, max(0, W // 2 - 1000), -1):
    for h in range(H // 2 + 1, max(0, W // 2 - 1000), -1):
        a = H * w
        b = (W - w) * h
        c = (W - w) * (H - h)
        diff = min(diff, abs(max(a, b, c) - min(a, b, c)))
        a = W * h
        b = (H - h) * w
        c = (H - h) * (W - w)
        diff = min(diff, abs(max(a, b, c) - min(a, b, c)))

print(diff)

Submission status: Wrong Answer

Solution code:
H, W = (int(i) for i in input().split())
ans = float("inf")
for i in range(1,H):
  a, b = (i, W)
  S = W*(H-i)
  if a%2==0 or b%2==0:
    ans = min(ans, abs(S-a*b//2))
  else:
    if a == 1 and b ==1:
      continue
    else:
      Sbig = (max(a, b)+1)*min(a, b)//2
      Ssma = (max(a, b)-1)*min(a, b)//2
      Sdiff = max(abs(S-Sbig), abs(S-Ssma), abs(Sbig-Ssma))
      ans = min(ans, Sdiff)
      
H, W = (W, H)
for i in range(1,H):
  a, b = (i, W)
  S = W*(H-i)
  if a%2==0 or b%2==0:
    ans = min(ans, abs(S-a*b//2))
  else:
    Sbig = (a*b - min(a, b))//2
    Ssma = (a*b + min(a, b))//2
    temp = max(abs(S-Sbig), abs(S-Ssma), Sbig-Ssma)
    if temp:
      ans = min(ans, abs(S-temp))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = (int(i) for i in input().split())
ans = float("inf")
for i in range(1,H):
  a, b = (i, W)
  S = W*(H-i)
  if a%2==0 or b%2==0:
    ans = min(ans, abs(S-a*b//2))
  else:
    Sbig = (a*b - min(a, b))//2
    Ssma = (a*b + min(a, b))//2
    temp = max(abs(S-Sbig), abs(S-Ssma), Sbig-Ssma)
    if temp:
      ans = min(ans, abs(S-temp))
H, W = (W, H)
for i in range(1,H):
  a, b = (i, W)
  S = W*(H-i)
  if a%2==0 or b%2==0:
    ans = min(ans, abs(S-a*b//2))
  else:
    Sbig = (a*b - min(a, b))//2
    Ssma = (a*b + min(a, b))//2
    temp = max(abs(S-Sbig), abs(S-Ssma), Sbig-Ssma)
    if temp:
      ans = min(ans, abs(S-temp))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = (int(i) for i in input().split())
ans = float("inf")
for i in range(1,H):
  a, b = (i, W)
  S = W*(H-i)
  if a%2==0 or b%2==0:
    ans = min(ans, abs(S-a*b//2))
  else:
    temp = (a*b - min(a, b))//2
    if temp:
      ans = min(ans, abs(S-temp))
H, W = (W, H)
for i in range(1,H):
  a, b = (i, W)
  S = W*(H-i)
  if a%2==0 or b%2==0:
    ans = min(ans, abs(S-a*b//2))
  else:
    temp = (a*b - min(a, b))//2
    if temp:
      ans = min(ans, abs(S-temp))
print(ans)

Submission status: Wrong Answer

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

ans = float("inf")

for w in range(1,W-1) :
    #print("yay")
    s1 = H*w
    
    w2 = (W-w)//2
    s2 = H*w2
    s3 = H*(W-(w+w2))
    calc = max([s1,s2,s3]) - min([s1,s2,s3])
    ans = min(ans,calc)
    #print(s1,s2,s3)
    h2 = H//2
    s2 = h2*(W-w)
    s3 = (H-h2)*(W-w)
    calc = max([s1,s2,s3]) - min([s1,s2,s3])
    ans = min(ans,calc)
    #print(s1,s2,s3)

H,W = W,H    

for w in range(1,W-1) :
    #print("wow")
    s1 = H*w
    
    w2 = (W-w)//2
    s2 = H*w2
    s3 = H*(W-(w+w2))
    calc = max([s1,s2,s3]) - min([s1,s2,s3])
    ans = min(ans,calc)
    #print(s1,s2,s3)
    h2 = H//2
    s2 = h2*(W-w)
    s3 = (H-h2)*(W-w)
    calc = max([s1,s2,s3]) - min([s1,s2,s3])
    ans = min(ans,calc)
    #print(s1,s2,s3)
    

print(ans)

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split(" "))
ar = []
if a % 3 == 0 or b % 3 == 0:
    print(0)
else:
    ma = max(a,b)
    mi = min(a,b)
    ar.append((ma // 2) * mi)
    if mi % 2 == 0:
        ar.append((mi // 2) * (ma - (ma // 2)))
    else:
        ar.append((mi // 2) * (ma - (ma // 2)))
        ar.append((mi - (mi // 2)) * (ma - (ma // 2)))
    print(max(ar)-min(ar))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if (H*W)%3==0:
    print(0)
else:
    s=10**10
    t=10**10
    for i in range(H):
        b=max(abs(W*i-(H-i)*(W//2)),abs(W*i-(H-i)*(W//2+W%2)),(H-i)*(W%2))
        if b<=s:
            s=b
    for j in range(W):
        c=max(abs(H*j-(W-j)*(H//2)),abs(H*j-(W-j)*(H//2+H%2)),(W-j)*(H%2))
        if c<=t:
            t=c
    print(min(s,t))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if (H*W)%3==0:
    print(0)
else:
    s=10**10
    t=10**10
    for i in range(H):
        b=max(abs(W*i-(H-i)*(W//2)),abs(W*i-(H-i)*(W//2+W%2)),(H-i)*(W%2))
        if b<=s:
            s=b
        else:
            break
    for j in range(W):
        c=max(abs(H*j-(W-j)*(H//2)),abs(H*j-(W-j)*(H//2+H%2)),(W-j)*(H%2))
        if c<=t:
            t=c
        else:
            break
    print(min(s,t))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
if a>b:
	f=a
	a=b
	b=f
c=int((b+1)/3)
b-=c
d=int(a/2)
e=a-d
print(max(c*a,max(b*d,b*e))-min(c*a,min(b*d,b*e)))

Submission status: Wrong Answer

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


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

def calc(h, w):
    bars = []
    first_cut = w//3 + w%3 - 1
    bars.append(first_cut*h)
    bars.append((h//2)*(w-first_cut))
    bars.append(h*w-sum(bars))
    return max(bars) - min(bars)

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

Submission status: Wrong Answer

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

min_score = 10**27

for i in range(1,H):
    if W%2==0:
        S1 = i*W/2
        S2 = (H-i)*W
        min_score = min(abs(S1-S2),min_score)
    else:
        S1 = i*(W-1)/2
        S2 = i*(W+1)/2
        S3 = (H-i)*W
        S4 = max(S1,S2,S3)
        S5 = min(S1,S2,S3)
        min_score = min(S4-S5,min_score)

for i in range(1,W):
    if H%2==0:
        S1 = i*H/2
        S2 = (W-i)*H
        min_score = min(abs(S1-S2),min_score)
    else:
        S1 = i*(H-1)/2
        S2 = i*(H+1)/2
        S3 = (W-i)*H
        S4 = max(S1,S2,S3)
        S5 = min(S1,S2,S3)
        min_score = min(S4-S5,min_score)

s1=int(H/3)
s2=int((H-s1)/2)
s3=H-s1-s2
S1 = max(s1*W,s2*W,s3*W)
S2 = min(s1*W,s2*W,s3*W)
min_score = min(S1-S2,min_score)

s1=int(W/3)
s2=int((W-s1)/2)
s3=H-s1-s2
S1 = max(s1*H,s2*H,s3*H)
S2 = min(s1*H,s2*H,s3*H)
min_score = min(S1-S2,min_score)

print(int(min_score))

Submission status: Wrong Answer

Solution code:
import sys
def inpl(): return [int(i) for i in input().split()]

H, W = inpl()
if H%3 == 0 or W%3 == 0:
    print(0)
    sys.exit()
if H == 2 or W == 2:
    print(1)
    sys.exit()

ans = float('inf') 

for i in range(1,W):
    ans = min(ans,max(H*i,(H//2+H%2)*(W-i),(H//2)*(W-i))-\
              min(H*i,(H//2+H%2)*(W-i),(H//2)*(W-i)))
for i in range(1,H):
    ans = min(ans,max(W*i,(W//2+W%2)*(H-i),(W//2)*(H-i))-\
              min(W*i,(W//2+W%2)*(H-i),(W//2)*(H-i))) 
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def inpl(): return [int(i) for i in input().split()]

H, W = inpl()
if H%3 == 0 or W%3 == 0:
    print(0)
    sys.exit()
if H == 2 or W == 2:
    print(1)
    sys.exit()

a = W//3
b = W%3
c = H//3
d = H%3

ans = float('inf') 

for i in range(b+2):
    ans = min(ans,max(H*(a+i),(H//2+H%2)*(W-a-i),(H//2)*(W-a-i))-\
              min(H*(a+i),(H//2+H%2)*(W-a-i),(H//2)*(W-a-i)))
for i in range(d+2):
    ans = min(ans,max(W*(c+i),(W//2+W%2)*(H-c-i),(W//2)*(H-c-i))-\
              min(W*(c+i),(W//2+W%2)*(H-c-i),(W//2)*(H-c-i)))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def inpl(): return [int(i) for i in input().split()]

H, W = inpl()
if H%3 == 0 or W%3 == 0:
    print(0)
    sys.exit()
if H == 2 or W == 2:
    print(1)
    sys.exit()

a = W//3
b = W%3
c = H//3
d = H%3

ans = float('inf') 

for i in range(b+1):
    ans = min(ans,max(H*(a+i),(H//2+H%2)*(W-a-i),(H//2)*(W-a-i))-\
              min(H*(a+i),(H//2+H%2)*(W-a-i),(H//2)*(W-a-i)))
for i in range(d+1):
    ans = min(ans,max(W*(c+i),(W//2+W%2)*(H-c-i),(W//2)*(H-c-i))-\
              min(W*(c+i),(W//2+W%2)*(H-c-i),(W//2)*(H-c-i)))
print(ans)

Submission status: Wrong Answer

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

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

hh = h//3+(1 if h%3==2 else 0)
ww = w//2
mmax = max(hh*w, (h-hh) * (ww+(1 if w%2==1 else 0)))
mmin = min(hh*w, (h-hh) * ww)
ret1 = mmax-mmin

ww = w//3+(1 if w%3==2 else 0)
hh = h//2
mmax = max(ww*h, (w-ww) * (hh+(1 if h%2==1 else 0)))
mmin = min(ww*h, (w-ww) * hh)
ret2 = mmax-mmin

print(min(ret1, ret2))

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
ans_min = 10 ** 10
if h % 3 == 0 or w % 3 == 0:
	ans_min = 0
	
else:
	for a in range(w+1):
		sa = h * a
		if h % 2 == 0:
			sb = (w-a) * (h // 2)
			sc = (w-a) * (h // 2)
		else:
			sb = (w-a) * (h // 2 + 1)
			sc = (w-a) * (h // 2)
		ans = max(sa,sb,sc) - min(sa,sb,sc)
		if ans_min > ans:
			ans_min = ans

	for a in range(h+1):
		sa = w * a
		if w % 2 == 0:
			sb = (h-a) * (w // 2)
			sc = (h-a) * (w // 2)
		else:
			sb = (h-a) * (w // 2 + 1)
			sc = (h-a) * (w // 2)
		ans = max(sa, sb, sc) - min(sa, sb, sc)
		
		if ans_min > ans:
			ans_min = ans
			
print(ans_min)

Submission status: Wrong Answer

Solution code:

h,w = map(int, input().split())
ans_min = 10 ** 10
if h % 3 == 0 or w % 3 == 0:
	ans_min = 0
else:
	for a in range(w+1):
		sa = h * a
		if h % 2 == 0:
			sb = (w-a) * (h // 2)
			sc = (w-a) * (h // 2)
		else:
			sb = (w-a) * (h // 2 + 1)
			sc = (w-a) * (h // 2)
		ans = max(sa,sb,sc) - min(sa,sb,sc)
		if ans_min > ans:
			ans_min = ans

	for a in range(h+1):
		sa = w * a
		if w % 2 == 0:
			sb = (h-a) * (w // 2)
			sc = (h-a) * (w // 2)
		else:
			sb = (h-a) * (w // 2 + 1)
			sc = (h-a) * (w // 2)
		ans = max(sa, sb, sc) - min(sa, sb, sc)
		if ans_min > ans:
			ans_min = ans
print(ans_min)

Submission status: Wrong Answer

Solution code:
def maxdif(a,b):
    if a % 3 == 0:
        return 0
    sabuns = []
    if a%2 == 0:
        s = a//2
        t = a//2
    else:
        s = a//2
        t = s + 1
    for i in range(b+1):
        sabun = max(abs(s*i-t*i),abs(s*i-(s+t)*(b-i)),abs(t*i-(s+t)*(b-i)))
        sabuns.append(sabun)

    return min(sabuns)


H ,W = [int(i) for i in input().split()]


print(min(maxdif(H,W),maxdif(W,H)))

Submission status: Wrong Answer

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *
from heapq import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

H, W = reads()

def solve(h, w):
  ans = 0 if h % 3 == 0 else w
  for i in range(1, h-1):
    l = [w*(h-i), w//2*i, (w+1)//2*i]
    ans = min(ans, max(l) - min(l))
  return ans

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

Submission status: Wrong Answer

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

def solve(H, W):
    if H % 3 == 0:
        return 0
    result = H * W
    H1 = H // 2
    H2 = H - H1
    for x in range(W + 1):
        a = H * x
        b = (W - x) * H1
        c = (W - x) * H2
        r = max(a, c) - min(a, b)
        result = min(result, r)
    return result

# print(H, W)

result = min(solve(H, W), solve(W, H))

print(result)

Submission status: Wrong Answer

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

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

def chk(h,w):
  a = m
  for i in range(1, h):
    l = i * w
    o = (h - i) // 2 * w
    g = m - l - o
    a = min(a, max(abs(l - o), abs(g - o), abs(g - l)))

  for i in range(1, h):
    l = i * w
    o = (h - i) * (w // 2)
    g = m - i - o
    a = min(a, max(abs(l - o), abs(g - o), abs(g - l)))

  return a

print(min(chk(h, w), chk(w, h)))

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    kouho.append(min(h,w)*1)
    w_small = w//2
    w_large = w-w_small
    border1 = w_large*h//(w+w_large) #ギリギリ上の四角が小さい
    h_small = h//2
    h_large = h-h_small
    border2 = h_large*w//(h+h_large)
    A1 = w*border1
    B1 = w_small*(h-border1)
    C1 = w_large*(h-border1)

    if A1 > B1:
        kouho.append(C1-B1)
    else:
        kouho.append(C1-A1)
    border1+=1
    A1 = w*border1
    B1 = w_small*(h-border1)
    C1 = w_large*(h-border1)

    kouho.append(A1-B1)

    A2 = h*border2
    B2 = h_small*(w-border2)
    C2 = h_large*(w-border2)

    if A2>B2:
        kouho.append(C2-B2)
    else:
        kouho.append(C2-A1)

    border2+=1

    A2 = h * border2
    B2 = h_small * (w - border2)
    C2 = h_large * (w - border2)

    kouho.append(A2-B2)

    #print(border1,border2)
    #print(kouho)
    print(min(kouho))

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    kouho.append(min(h,w)*1)
    w_small = w//2
    w_large = w-w_small
    border1 = w_large*h//(w+w_large) #ギリギリ上の四角が小さい
    h_small = h//2
    h_large = h-h_small
    border2 = h_large*w//(h+h_large)
    kouho.append(abs(w*(border1+1)-w_small*(h-(border1+1))))
    kouho.append(abs(h*(border2+1)-h_small*(w-(border2+1))))
    kouho.append(abs(w*border1-w_large*(h-border1)))
    kouho.append(abs(w*border2-h_large*(w-border2)))
    #print(border1,border2)
    #print(kouho)
    print(min(kouho))

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    kouho.append(min(h,w)*1)
    w_small = w//2
    w_large = w-w_small
    border1 = w_large*h//(w+w_large) #ギリギリ上の四角が小さい
    h_small = h//2
    h_large = h-h_small
    border2 = h_large*w//(h+h_large)
    kouho.append(abs(w*(border1+1)-w_small*(h-(border1+1))))
    kouho.append(abs(h*(border2+1)-h_small*(w-(border2+1))))
    if w%2 == 1:
        kouho.append(abs(h-border1))
    if h%2 == 1:
        kouho.append(abs(w-border2))
    kouho.append(abs(w*border1-w_large*(h-border1)))
    kouho.append(abs(w*border2-h_large*(w-border2)))
    #print(border1,border2)
    #print(kouho)
    print(min(kouho))

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    kouho.append(min(h,w)*1)
    w_small = w//2
    w_large = w-w_small
    border1 = w_large*h//(w+w_large) #ギリギリ上の四角が小さい
    h_small = h//2
    h_large = h-h_small
    border2 = h_large*w//(h+h_large)
    kouho.append(abs(w*(border1+1)-w_small*(h-(border1+1))))
    kouho.append(abs(h*(border2+1)-h_small*(w-(border2+1))))
    kouho.append(abs(h-border1))
    kouho.append(abs(w-border2))
    kouho.append(abs(w*border1-w_large*(h-border1)))
    kouho.append(abs(w*border2-h_large*(w-border2)))
    #print(border1,border2)
    #print(kouho)
    print(min(kouho))

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    kouho.append(min(h,w)*1)
    w_small = w//2
    w_large = w-w_small
    border1 = w_large*h//(w+w_large) #ギリギリ上の四角が小さい
    h_small = h//2
    h_large = h-h_small
    border2 = h_large*w//(h+h_large)
    kouho.append(abs(w*(border1+1)-w_small*(h-(border1+1))))
    kouho.append(abs(h*(border2+1)-h_small*(w-(border2+1))))
    kouho.append(abs(h-border1))
    kouho.append(abs(w-border2))
    kouho.append(abs(w*border1-w_small*(h-border1)))
    kouho.append(abs(w*border2-h_small*(w-border2)))
    #print(border1,border2)
    #print(kouho)
    print(min(kouho))

Submission status: Wrong Answer

Solution code:
import math
H, W = map(int, input().split())
L = max(H, W)
S = min(H, W)

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    sq1 = (L//3+1) * S
    sq2 = (L-L//3-1)*(S//2)
    S1 = abs(sq1-sq2)
    sq3 = (L//3) * S
    sq4 = (L-L//3) * math.ceil(S/2)
    S2 = abs(sq4-sq3)
    print(min(S, S1, S2))

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
l = list(sorted([h,w]))
hh = l[0]
ww = l[1]
kouho_1 = 9999999999999999999999999999
kouho_2 = 9999999999999999999999999999
kouho_3 = 9999999999999999999999999999
kouho_4 = 9999999999999999999999999999

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    
    if ma_1 - mi_1 <   kouho_1 :
        kouho_1 =ma_1 - mi_1
    
    bb = ((ww)) * ((hh - i) // 2)
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 < kouho_2:
        kouho_2 = ma_2 - mi_2
            
p = hh
q = ww

hh = q
ww = p

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    
    if ma_1 - mi_1 <   kouho_3 :
        kouho_3 =ma_1 - mi_1
    
    bb = ((ww)) * ((hh - i) // 2)
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 < kouho_4:
        kouho_4 = ma_2 - mi_2
print(min(kouho_1,kouho_2,kouho_3,kouho_4))

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
l = list(sorted([h,w]))
hh = l[0]
ww = l[1]
kouho_1 = 9999999999999999999999999999
kouho_2 = 9999999999999999999999999999
kouho_3 = 9999999999999999999999999999
kouho_4 = 9999999999999999999999999999

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    
    if ma_1 - mi_1 <   kouho_1 :
        kouho_1 =ma_1 - mi_1
    
    bb = ((ww)) * (hh - i) // 2
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 < kouho_2:
        kouho_2 = ma_2 - mi_2
            
p = hh
q = ww

hh = q
ww = p

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    
    if ma_1 - mi_1 <   kouho_3 :
        kouho_3 =ma_1 - mi_1
    
    bb = ((ww)) * (hh - i) // 2
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 < kouho_4:
        kouho_4 = ma_2 - mi_2
print(min(kouho_1,kouho_2,kouho_3,kouho_4))

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
l = list(sorted([h,w]))
hh = l[0]
ww = l[1]
kouho_1 = 9999999999999999999999999999
kouho_2 = 9999999999999999999999999999

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    
    if ma_1 - mi_1 <= kouho_1 :
        kouho_1 =ma_1 - mi_1
    
    bb = ((ww)) * (hh - i) // 2
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 <=kouho_2:
        kouho_2 = ma_2 - mi_2
            
print(min(kouho_1,kouho_2))

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
l = list(sorted([h,w]))
hh = l[0]
ww = l[1]
kouho_1 = 1000000000000
kouho_2 = 1000000000000

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    if ma_1 - mi_1 <= kouho_1:
        kouho_1 =ma_1 - mi_1
    
    bb = ((ww)) * (hh - i) // 2
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 <=kouho_2:
        kouho_2 = ma_2 - mi_2
            
print(min(kouho_1,kouho_2))

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
l = list(sorted([h,w]))
hh = l[0]
ww = l[1]
kouho_1 = 1000000000000
kouho_2 = 1000000000000

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    if ma_1 - mi_1 <= kouho_1:
        kouho_1 =ma_1 - mi_1
    
    bb = ((ww) // 2 ) * hh
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 <=kouho_2:
        kouho_2 = ma_2 - mi_2
            
print(min(kouho_1,kouho_2))

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
l = list(sorted([h,w]))
hh = l[0]
ww = l[1]
kouho_1 = 1000000000000
kouho_2 = 1000000000000

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    if ma_1 - mi_1 <= kouho_1:
        kouho_1 =ma_1 - mi_1
    
    bb = (ww) // 2 * ( hh -i)
    cc = hh * ww - a- bb
    mi_2 = min(a,bb,cc)
    ma_2 = max(a,bb,cc)
        
    if ma_2 - mi_2 <=kouho_2:
        kouho_2 = ma_2 - mi_2
            
print(min(kouho_1,kouho_2))

Submission status: Wrong Answer

Solution code:
h, w = map(int,input().split())
l = list(sorted([h,w]))
hh = l[0]
ww = l[1]
kouho_1 = 1000000000000
kouho_2 = 1000000000000

for i in range(1,hh):
    a = i * ww
    b = ((hh - i) // 2) * ww
    c = hh * ww - (a + b) 
    
    mi_1 = min(a,b,c)
    ma_1 = max(a,b,c)
    if ma_1 - mi_1 <= kouho_1:
        kouho_1 =ma_1 - mi_1
    
    for j in range(1,ww):
        bb = (hh-i) * j
        cc = hh * ww - a- bb
        mi_2 = min(a,bb,cc)
        ma_2 = max(a,bb,cc)
        
        if ma_2 - mi_2 <=kouho_2:
            kouho_2 = ma_2 - mi_2
            
print(min(kouho_1,kouho_2))

Submission status: Wrong Answer

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

def check(h, w):
	if h % 3 == 0 or w % 3 == 0:
		return 0

	else:
		block1 = w * (h // 3)
		block2 = (h - (h // 3)) * (w // 2)
		block3 = h * w - block1 - block2
		gap1 = max(block1, block2, block3) - min(block1, block2, block3)

		block4 = w * (h // 3 + 1)
		block5 = (h - 1 - (h // 3)) * (w // 2)
		block6 = h * w - block4 - block5
		gap2 = max(block4, block5, block6) - min(block4, block5, block6)

		return(min(gap1, gap2))

print(min(check(H, W), check(W, H)))

Submission status: Wrong Answer

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

def check(h, w):
	l = max(h, w)
	s = min(h, w)
	if h % 3 == 0 or w % 3 == 0:
		return 0

	else:
		if abs((l // 3) - l / 3) <= abs((l // 3 + 1) - l / 3):
			block1 = s * (l // 3)
			if (l - (l // 3)) % 2 == 0:
				return abs(block1 - (l - (l // 3)) / 2)
			else:
				block3 = (l - (l // 3)) * (s // 2)
				block2 = h * w - block1 - block3
				return min(max(block1, block2, block3) - min(block1, block2, block3), s)

		elif abs((l // 3) - l / 3) > abs((l // 3 + 1) - l / 3):
			block1 = s * (l // 3 + 1)
			if (l - (l // 3 + 1)) % 2 == 0:
				return abs(block1 - (l - (l // 3 + 1)) / 2)
			else:
				block3 = (l - 1 - (l // 3)) * (s // 2)
				block2 = h * w - block1 - block3
				return min(max(block1, block2, block3) - min(block1, block2, block3), s)

print(min(check(H, W), check(W, H)))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
l = max(h, w)
s = min(h, w)

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

else:
	if abs((l // 3) - l / 3) <= abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3)
		if (l - (l // 3)) % 2 == 0:
			print(abs(block1 - (l - (l // 3)) / 2))
		else:
			block3 = (l - (l // 3)) * (s // 2)
			block2 = h * w - block1 - block3
			print(min(max(block1, block2, block3) - min(block1, block2, block3), s))

	elif abs((l // 3) - l / 3) > abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3 + 1)
		if (l - (l // 3 + 1)) % 2 == 0:
			print(abs(block1 - (l - (l // 3 + 1)) / 2))
		else:
			block3 = (l - 1 - (l // 3)) * (s // 2)
			block2 = h * w - block1 - block3
			print(min(max(block1, block2, block3) - min(block1, block2, block3), s))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
l = max(h, w)
s = min(h, w)
if h % 3 == 0 or w % 3 == 0:
	print(0)
else:
	if abs((l // 3) - l / 3) < abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * (s // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))
	elif abs((l // 3) - l / 3) > abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3 + 1)
		block3 = (l - 1 - (l // 3)) * (s // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))
	else:
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * (s // 2)
		block2 = h * w - block1 - block3
		block4 = s * (l // 3 + 1)
		block6 = (l - 1 - (l // 3)) * (s // 2)
		block5 = h * w - block4 - block6
		print(min([max(block1, block2, block3) - min(block1, block2, block3), max(block4, block5, block6) - min(block4, block5, block6), s]))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
l = max(h, w)
s = min(h, w)
if h % 3 == 0 or w % 3 == 0:
	print(0)
else:
	if abs((l // 3) - l / 3) < abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * ((s + 1) // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))
	elif abs((l // 3) - l / 3) > abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3 + 1)
		block3 = (l - 1 - (l // 3)) * (s // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))
	else:
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * ((s + 1) // 2)
		block2 = h * w - block1 - block3
		block4 = s * (l // 3 + 1)
		block6 = (l - 1 - (l // 3)) * (s // 2)
		block5 = h * w - block4 - block6
		print(min([max(block1, block2, block3) - min(block1, block2, block3), max(block4, block5, block6) - min(block4, block5, block6), s]))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
l = max(h, w)
s = min(h, w)
if h % 3 == 0 or w % 3 == 0:
	print(0)
else:
	if abs((l // 3) - l / 3) < abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * ((s + 1) // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))
	elif abs((l // 3) - l / 3) > abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3 + 1)
		block3 = (l - 1 - (l // 3)) * (s // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))
	else:
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * ((s + 1) // 2)
		block2 = h * w - block1 - block3
		block4 = s * (l // 3 + 1)
		block6 = (l - 1 - (l // 3)) * (s // 2)
		block5 = h * w - block1 - block3
		print(min([max(block1, block2, block3) - min(block1, block2, block3), max(block4, block5, block6) - min(block4, block5, block6), s]))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
l = max(h, w)
s = min(h, w)
if h % 3 == 0 or w % 3 == 0:
	print(0)
else:
	if abs((l // 3) - l / 3) <= abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * ((s + 1) // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))
	else:
		block1 = s * (l // 3 + 1)
		block3 = (l - 1 - (l // 3)) * (s // 2)
		block2 = h * w - block1 - block3
		print(min(max(block1, block2, block3) - min(block1, block2, block3), s))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
l = max(h, w)
s = min(h, w)
if h % 3 == 0 or w % 3 == 0:
	print(0)
else:
	if abs((l // 3) - l / 3) <= abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * ((s + 1) // 2)
		block2 = h * w - block1 - block3
		print(max(block1, block2, block3) - min(block1, block2, block3))
	else:
		block1 = s * (l // 3 + 1)
		block3 = (l - 1 - (l // 3)) * (s // 2)
		block2 = h * w - block1 - block3
		print(max(block1, block2, block3) - min(block1, block2, block3))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
l = max(h, w)
s = min(h, w)
if h % 3 == 0 or w % 3 == 0:
	print(0)
else:
	if abs((l // 3) - l / 3) <= abs((l // 3 + 1) - l / 3):
		block1 = s * (l // 3)
		block3 = (l - (l // 3)) * ((s + 1) // 2)
		print(block3 - block1)
	else:
		block1 = s * (l // 3 + 1)
		block3 = (l - 1 - (l // 3)) * (s // 2)
		print(block1 - block3)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
tmp=h*w
tmp_2=h*w
tmp_3=h*w
tmp_4=h*w
for i in range(1,h):
    half_h=h-i
    area_1=i*w
    area_2=half_h//2*w
    area_3=(half_h-(half_h//2))*w
    ans=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans<tmp:
        tmp=ans
for j in range(1,w):
    half_w=w-j
    area_1=h*j
    area_2=(half_w//2)*h
    area_3=half_w-half_w//2*h
    ans_2=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_2<tmp_2:
        tmp_2=ans_2
for i in range(1,h):
    half_w=w//2
    area_1=i*half_w
    area_2=i*(w-half_w)
    area_3=(h-i)*w
    ans_3=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_3<tmp_3:
        tmp_3=ans_3
for j in range(1,w):
    half_h=h//2
    area_1=h*j
    area_2=(w-j)*half_h
    area_3=(w-j)*(h-half_h)
    ans_4=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_4<tmp_4:
        tmp_4=ans_4
print(min(tmp,tmp_2,tmp_3,tmp_4))