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*W%3 == 0:
    print(0)

else:
    M = max(H, W)
    m = min(H, W)
    ans1 = m*(M//2)-(M-M//2)*(m//2)
    ans2 = (M//2)*m - (M-M//2)//2*m
    ans3 = (m//2)*M - (m-m//2)//2*M
    ans4 = M*(m//2)-(m-m//2)*(M//2)
    print(ans1, ans2, ans3, ans4)

Submission status: Wrong Answer

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

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

else:
    M = max(H, W)
    m = min(H, W)
    print(m*(M//2)-(M-M//2)*(m//2))

Submission status: Wrong Answer

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

if h%3==0:
    d=0
elif w%3==0:
    d=0
else:
    if h>=w:
        h,w=w,h
    d1=h
    d2min=h*w
    for ww in range(1,w):
        h1=h//2
        h2=h-h1
        d2=max(ww*h,(w-ww)*h2)-min(ww*h,(w-ww)*h1)
#        print(d2,max(ww*h,(w-ww)*h2),min(ww*h,(w-ww)*h1))
        d2min=min(d2,d2min)
    d=min(d1,d2min)
print(d)

Submission status: Wrong Answer

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

if (H%3==0) |(W%3==0):
    ans =0
    
else:
    # cand1
    a1 = H*(W//3)
    a2 = (H//2)*(W-W//3)
    a3 = (H-H//2)*(W-W//3)
    cand1 = max(a1,a2,a3)-min(a1,a2,a3)
    
    #cand2
    
    a1 = H*(W//3+1)
    a2 = (H//2)*(W-W//3-1)
    a3 = (H-H//2)*(W-W//3-1)
    cand2 = max(a1,a2,a3)-min(a1,a2,a3)
    
    #cand3
    
    dum1 = H
    dum2 = W
    H =dum2
    W =dum1
    
    a1 = H*(W//3)
    a2 = (H//2)*(W-W//3)
    a3 = (H-H//2)*(W-W//3)
    cand3 = max(a1,a2,a3)-min(a1,a2,a3)
    
    #cand4
    
    a1 = H*(W//3+1)
    a2 = (H//2)*(W-W//3-1)
    a3 = (H-H//2)*(W-W//3-1)
    cand4 = max(a1,a2,a3)-min(a1,a2,a3)
    
    ans = min(cand1,cand2,cand3,cand4)
    
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s_h_list = []
for i in range(1, h):
	sa = w * i
	sb = (w//2) * (h-i)
	sc = h*w - (sa + sb)
	s_list = [sa, sb, sc]
	s_h_list.append(max(s_list) - min(s_list))
s_h_list.sort()
s_w_list = []
for i in range(1, w):
	sa = h * i
	sb = (h//2) * (w-i)
	sc = h*w - (sa + sb)
	s_list = [sa, sb, sc]
	s_w_list.append(max(s_list) - min(s_list))
s_w_list.sort()
print(min([s_h_list[0], s_w_list[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 = 10000000000
for change in range(2): #heightとwidthを入れ替えてもう一回実行する
  #二分割
  for ty in range(1,h):
    ans = min(ans, max(ty*w, int(w/2)*(h-ty), (w-int(w/2))*(h-ty)) - min(ty*w, int(w/2)*(h-ty), (w-int(w/2))*(h-ty)))
  ans = min(ans, h*(max(int(w/3), w-int(w/3)*2) - min(int(w/3), w-int(w/3)*2)))
  temp = h
  h = w
  w = temp
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 = 1000000000
for change in range(2): #heightとwidthを入れ替えてもう一回実行する
  #二分割
  for ty in range(1,h):
    ans = min(ans, max(ty*w, int(w/2)*(h-ty), (w-int(w/2))*(h-ty)) - min(ty*w, int(w/2)*(h-ty), (w-int(w/2))*(h-ty)))
  ans = min(ans, h*(max(int(w/3), w-int(w/3)*2) - min(int(w/3), w-int(w/3)*2)))
  temp = h
  h = w
  w = temp
print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
#from scipy.misc import comb

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

if H%3 == 0:
    print(0)
elif W%3 == 0:
    print(0)
else:
    ans = H*W
    A = [0]*3
    for i in range(1,H):
        A[0] = i*W
        A[1] = (H-i)*(W//2)
        A[2] = (H-i)*(W-W//2)
        A.sort()
        ans = min(ans,A[2]-A[0])
    
    for j in range(1,W):
        A[0] = H*j
        A[1] = (H//2)*(W-j)
        A[2] = (H-H//2)*(W-j)
        A.sort()
        ans = min(ans,A[2]-A[0])

    ans = min(ans, H*(W//3), (H//3)*W)

    print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
#from scipy.misc import comb

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

if H%3 == 0:
    print(0)
elif W%3 == 0:
    print(0)
else:
    ans = H*W
    A = [0]*3
    for i in range(1,H):
        A[0] = i*W
        A[1] = (H-i)*(W//2)
        A[2] = (H-i)*(W-W//2)
        A.sort()
        ans = min(ans,A[2]-A[0])
    
    for j in range(1,W):
        A[0] = H*j
        A[1] = (H//2)*(W-j)
        A[2] = (H-H//2)*(W-j)
        A.sort()
        ans = min(ans,A[2]-A[0])

    print(ans)

Submission status: Wrong Answer

Solution code:
#coding utf-8
import math
h, w = map(int,input().split())
S = h * w
s = max(h,w)
t = min(h,w)

def find_difference(n):
    if t % 2 == 0:
        ans = abs(n*t-(s-n)*t//2)
    else:
        ans = max(n*t,(s-n)*(t//2+1)) - min(n*t,(s-n)*(t//2))
    #print('s=' + str(s), 't =' + str(t))
    #print('n=' + str(n))
    #print('hidari=' + str(n*t),'migiue=' + str((s-n)*(t//2+1)),'migishita=' + str((s-n)*(t//2)))
    return ans

c = math.ceil(s/3)
#print('c=' + str(c))
ans = min(find_difference(c),find_difference(c-1),find_difference(c+1))
#print(find_difference(c),find_difference(c-1))
ans = min(ans,t)
if h % 3 == 0 or w % 3 == 0:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
#coding utf-8
import math
h, w = map(int,input().split())
S = h * w
s = max(h,w)
t = min(h,w)

def find_difference(n):
    if t % 2 == 0:
        ans = abs(n*t-(s-n)*t//2)
    else:
        ans = max(n*t,(s-n)*(t//2+1)) - min(n*t,(s-n)*(t//2))
    #print('s=' + str(s), 't =' + str(t))
    #print('n=' + str(n))
    #print('hidari=' + str(n*t),'migiue=' + str((s-n)*(t//2+1)),'migishita=' + str((s-n)*(t//2)))
    return ans

c = math.ceil(s/3)
#print('c=' + str(c))
ans = min(find_difference(c),find_difference(c-1),find_difference(c+1))
#print(find_difference(c),find_difference(c-1))
if h % 3 == 0 or w % 3 == 0:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
#coding utf-8
import math
h, w = map(int,input().split())
S = h * w
s = max(h,w)
t = min(h,w)

def find_difference(n):
    if t % 2 == 0:
        ans = abs(n*t-(s-n)*t//2)
    else:
        ans = max(n*t,(s-n)*(t//2+1)) - min(n*t,(s-n)*(t//2))
    #print('s=' + str(s), 't =' + str(t))
    #print('n=' + str(n))
    #print('hidari=' + str(n*t),'migiue=' + str((s-n)*(t//2+1)),'migishita=' + str((s-n)*(t//2)))
    return ans

c = math.ceil(s/3)
#print('c=' + str(c))
ans = min(find_difference(c),find_difference(c-1))
#print(find_difference(c),find_difference(c-1))
if h % 3 == 0 or w % 3 == 0:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    ans = 10 ** 9
    for _ in range(2):
        for i in range(1, w):
            c1 = h * i
            c2 = (h // 2) * (w - i)
            c3 = h * w - c1 - c2
            maxc = max(c1, c2, c3)
            minc = min(c1, c2, c3)
            if maxc - minc < ans:
                ans = maxc - minc
        h, w = w, h
print(ans)

Submission status: Wrong Answer

Solution code:
import math
H,W=map(int,input().split())
d=[H,W],[W,H]
ans=10**15
for x,y in d:
    for i in range(1,x):
        A=(x-i)*y;B=i*y
        if(i%2==0 or y%2==0):
            ans=min(ans,max(A,B//2)-min(A,B//2))
        else:
            e=[y,i],[i,y]
            for c,d in e:
                C=math.floor(c/2)*d;
                ans=min(ans,max(A,C)-min(A,C))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
H,W=map(int,input().split())
d=[H,W],[W,H]
ans=10*15
for x,y in d:
    for i in range(1,x):
        A=(x-i)*y;B=i*y
        if(i%2==0 or y%2==0):
            ans=min(ans,max(A,B//2)-min(A,B//2))
        else:
            e=[y,i],[i,y]
            for c,d in e:
                C=math.ceil(c/2)*d;D=math.floor(c/2)*d
                ans=min(ans,max(A,C,D)-min(A,C,D))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
ans = min(h,w)
if w%3 == 1:
    a = w//3
else:
    a = w//3+1
c = abs(h*a-(h//2)*(w-a))
if h % 3== 1:
    b = h//3
else:
    b = h//3+1
d = abs(w*b-(w//2)*(h-b))
print(min(ans,c,d))

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    ans = float('inf')
    # 横と縦の切るところを真ん中に固定してそれぞれのパターンを線形検索
    center = W//2
    for i in range(1, H):
        # 縦を先に割る
        a1 = center * H
        a2 = (W-center) * i
        a3 = (W-center) * (H-i)

        # 横を先に割る
        b1 = i * W
        b2 = (H-i) * center
        b3 = (H-i) * (W-center)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
        ans = min(ans, max(b1, b2, b3) - min(b1, b2, b3))

    center = H//2
    for i in range(1, W):
        # 縦を先に割る
        a1 = i * H
        a2 = (W-i) * center
        a3 = (W-i) * (H-center)

        # 横を先に割る
        b1 = center * W
        b2 = (H-center) * i
        b3 = (H-center) * (W-i)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
        ans = min(ans, max(b1, b2, b3) - min(b1, b2, b3))
    print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    ans = float('inf')
    # 横と縦の切るところを真ん中に固定してそれぞれのパターンを線形検索
    for i in range(1, H):
        center = W//2
        # 縦を先に割る
        a1 = center * H
        a2 = (W-center) * i
        a3 = (W-center) * (H-i)

        # 横を先に割る
        b1 = i * W
        b2 = (H-i) * center
        b3 = (H-i) * (W-center)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
        ans = min(ans, max(b1, b2, b3) - min(b1, b2, b3))

    for i in range(1, W):
        center = H//2
        # 縦を先に割る
        a1 = center * H
        a2 = (W-i) * center
        a3 = (W-i) * (H-center)

        # 横を先に割る
        b1 = center * W
        b2 = (H-center) * i
        b3 = (H-center) * (W-i)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
        ans = min(ans, max(b1, b2, b3) - min(b1, b2, b3))
    print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    ans = float('inf')
    # 横と縦の切るところを真ん中に固定してそれぞれのパターンを線形検索
    for i in range(1, H):
        center = W//2
        # 縦を先に割る
        a1 = center * H
        a2 = (W-center) * i
        a3 = (W-center) * (H-i)

        # 横を先に割る
        b1 = i * W
        b2 = (H-i) * center
        b3 = (H-i) * (W-center)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
        ans = min(ans, max(b1, b2, b3) - min(b1, b2, b3))

    for i in range(1, H):
        center = H//2
        # 縦を先に割る
        a1 = center * H
        a2 = (W-i) * center
        a3 = (W-i) * (H-center)

        # 横を先に割る
        b1 = center * W
        b2 = (H-center) * i
        b3 = (H-center) * (W-i)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
        ans = min(ans, max(b1, b2, b3) - min(b1, b2, b3))
    print(ans)

Submission status: Wrong Answer

Solution code:
l=sorted(list(int(i) for i in input().split()),reverse=True)
if len([i for i in l if i % 3 == 0])>0:
  print(0)
else:
  h=l[0]
  w=l[1]
  if w%2==0:
    minw=int(w/2)
    minh=2*h//3
    maxw=w
    maxh=h-minh
    print(min(abs(maxw*maxh-minw*minh),abs((maxw*(maxh-1)-minw*(minh+1)))))
  else:
    maxh=h-2*h//3
    maxw=w
    minh=h-maxh
    minw=w-2*w//3
    min2h=h-maxh
    min2w=w-minw
    if minh*minw >= min2h*min2w:
      print(maxw*maxh-min2w*min2h)
    else:
      print(maxw*maxh-minw*minh)

Submission status: Wrong Answer

Solution code:
l=sorted(list(int(i) for i in input().split()),reverse=True)
if len([i for i in l if i % 3 == 0])>0:
  print(0)
else:
  h=l[0]
  w=l[1]
  if w%2==0:
    minw=int(w/2)
    minh=2*h//3
    maxw=w
    maxh=h-minh
    print(min(abs(maxw*maxh-minw*minh),abs(maxw*(maxh-1)-minw*(minh+1))))
  else:
    maxh=h-2*h//3
    maxw=w
    minh=h-maxh
    minw=w-2*w//3
    print(abs(maxw*maxh-minw*minh))

Submission status: Wrong Answer

Solution code:
l=sorted(list(int(i) for i in input().split()),reverse=True)
if len([i for i in l if i % 3 == 0])>0:
  print(0)
else:
  h=l[0]
  w=l[1]
  if w%2==0:
    minw=int(w/2)
    minh=2*h//3
    maxw=w
    maxh=h-minh
    print(min(abs(maxw*maxh-minw*minh),abs(maxw*(maxh-1)-minw*(minh+1))))
  else:
    maxh=h//3
    maxw=w
    minh=h-maxh
    minw=w//3
    print(min(abs(maxw*maxh-minw*minh),abs(maxw*(maxh-1)-minw*(minh+1))))

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    H,W=MI()
    if H%3==0 or W%3==0:
        print(0)
    else:
        a=(H//3)+(H%3-1)
        b=(W//3)+(W%3-1)
        aa=abs(1/3-a/H)
        bb=abs(1/3-b/W)
        if aa<=bb:
            s1=a*W
            H=H-a
        else:
            s1=b*H
            W=W-b
            
        c=(H//2)/H
        d=(W//2)/W
        cc=abs(1/2-c)
        dd=abs(1/2-d)
        
        if cc<=dd:
            c=H//2
            s2=c*W
            s3=(H-c)*W
        else:
            d=W//2
            s2=d*H
            s3=(W-d)*H
        print(max(s1,s2,s3)-min(s1,s2,s3))
    
            

main()

Submission status: Wrong Answer

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

if h>w:
    h,w = w,h

for i in range(1, h):
    a = i*w
    x = w//2
    res = min(res, max(abs(a-(h-i)*x), abs(a-(h-i)*(x+1)), (h-i)))
    y = (h-i)//2
    res = min(res, w * max(abs(i-y), abs(2*y-h+i), abs(h-i-i-y)))
print(res)

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
res = 10**10
if h%3==0  or w%3==0:
    print(0)
else:
    if h>w:
        h,w = w,h
    x = h//2
    if h%2==0:
        for i in range(1, w):
            res = min(res, abs(i*x-(w-i)*h))
    else:
        for i in range(1, w):
            res = min(res, max(abs(i*x-(w-i)*h), abs(i*(x+1)-(w-i)*h), i))
    print(res)

Submission status: Wrong Answer

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

for i in range(math.floor(h / 3), math.ceil(h / 3) + 1):
    r1 = w * i
    for i1 in range(math.floor((h-i) / 2), math.ceil((h-i) / 2) + 1):
        r2 = i1 * w
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1,r2,r3) - min(r1,r2,r3)) > r else (max(r1,r2,r3) - min(r1,r2,r3))
    for i2 in range(math.floor(w / 2), math.ceil(w / 2) + 1):
        r2 = i2 * h
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1, r2, r3) - min(r1, r2, r3)) > r else (max(r1, r2, r3) - min(r1, r2, r3))

for j in range(math.floor(w / 3), math.ceil(w / 3) + 1):
    r1 = h * j
    for j1 in range(math.floor((w-j) / 2), math.ceil((w-j) / 2) + 1):
        r2 = j1 * h
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1,r2,r3) - min(r1,r2,r3)) > r else (max(r1,r2,r3) - min(r1,r2,r3))
    for j2 in range(math.floor(h / 2), math.ceil(h / 2) + 1):
        r2 = j2 * w
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1, r2, r3) - min(r1, r2, r3)) > r else (max(r1, r2, r3) - min(r1, r2, r3))

print(r)

Submission status: Wrong Answer

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

for i in range(math.floor(h / 3), math.ceil(h / 3) + 1):
    r1 = w * i
    for i1 in range(math.floor((h-i) / 2), math.ceil((h-i) / 2) + 1):
        r2 = i1 * w
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1,r2,r3) - min(r1,r2,r3)) > r else (max(r1,r2,r3) - min(r1,r2,r3))
    for i2 in range(math.floor(w / 2), math.ceil(w / 2) + 1):
        r2 = i2 * h
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1, r2, r3) - min(r1, r2, r3)) > r else (max(r1, r2, r3) - min(r1, r2, r3))

for j in range(math.floor(w / 3), math.ceil(w / 3) + 1):
    r1 = h * j
    for j1 in range(math.floor((w-i) / 2), math.ceil((w-i) / 2) + 1):
        r2 = j1 * h
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1,r2,r3) - min(r1,r2,r3)) > r else (max(r1,r2,r3) - min(r1,r2,r3))
    for j2 in range(math.floor(h / 2), math.ceil(h / 2) + 1):
        r2 = j2 * w
        r3 = hw - r1 - r2
        if r3 == 0:
            pass
        r = r if (max(r1, r2, r3) - min(r1, r2, r3)) > r else (max(r1, r2, r3) - min(r1, r2, r3))

print(r)

Submission status: Wrong Answer

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

for i in range(math.floor(h / 3), math.ceil(h / 3) + 1):
    r1 = w * i
    for i1 in range(math.floor((h-i) / 2), math.ceil((h-i) / 2) + 1):
        r2 = i1 * w
        r3 = hw - r1 - r2
        r = r if (max(r1,r2,r3) - min(r1,r2,r3)) > r else (max(r1,r2,r3) - min(r1,r2,r3))
    for i2 in range(math.floor(w / 2), math.ceil(w / 2) + 1):
        r2 = i2 * h
        r3 = hw - r1 - r2
        r = r if (max(r1, r2, r3) - min(r1, r2, r3)) > r else (max(r1, r2, r3) - min(r1, r2, r3))
        
for j in range(math.floor(w / 3), math.ceil(w / 3) + 1):
    r1 = h * j
    for j1 in range(math.floor((w-i) / 2), math.ceil((w-i) / 2) + 1):
        r2 = j1 * h
        r3 = hw - r1 - r2
        r = r if (max(r1,r2,r3) - min(r1,r2,r3)) > r else (max(r1,r2,r3) - min(r1,r2,r3))
    for j2 in range(math.floor(h / 2), math.ceil(h / 2) + 1):
        r2 = j2 * w
        r3 = hw - r1 - r2
        r = r if (max(r1, r2, r3) - min(r1, r2, r3)) > r else (max(r1, r2, r3) - min(r1, r2, r3))

print(r)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
areas = []
h1 = round(H/3)
w1 = round(W/3)
if abs(h1*W - H*W/3) < abs(H*w1 - H*W/3):
    areas.append(h1*W)
    H -= h1
else:
    areas.append(H*w1)
    W -= w1

h2 = round(H/2)
w2 = round(W/2)
if abs(h2*W - H*W/2) < abs(H*w2 - H*W/2):
    areas.append(h2*W)
    areas.append((H-h2)*W)
else:
    areas.append(H*w2)
    areas.append(H*(W-w2))

areas.sort()
print(areas[2] - areas[0])

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
    ans = 0
    
else:
    ans = float('inf')
    for x, y in [[h, w], [w, h]]:
        mid = x//2
        r = x%2
        for i in range(1, y):
            a, b, c = x*i, (mid+r)*(y-i), (mid)*(y-i)
            ans = min(abs(max(a, b, c) - min(a, b, c)), ans)
    
print(ans)

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
    ans = 0
    
else:
    ans = float('inf')
    for x, y in [[h, w], [w, h]]:
        mid = x//2
        r = x%2
        temp = float('inf')
        for i in range(1, y):
            a, b, c = x*i, (mid+r)*(y-i), (mid)*(y-i)
            area = abs(max(a, b, c) - min(a, b, c))
            if area < temp:
                temp = area
            else:
                break
        ans = min(ans, temp)
    
print(ans)

Submission status: Wrong Answer

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

h1,h2 = h//3, h//3+1
w1,w2= min(w//2, w//2+1), max(w//2, w//2+1)
h3,h4 = min(h//2, h//2+1), max(h//2, h//2+1)
w3,w4 = w//3, w//3+1

ans = []

a,b,c = h1*w, (h-h1)*w1, (h-h1)*w2
ans.append(max([a,b,c])-min([a,b,c]))
a,b,c = h2*w, (h-h2)*w1, (h-h2)*w2
ans.append(max([a,b,c])-min([a,b,c]))
a,b,c = w3*h, (w-w3)*h3, (w-w3)*h4
ans.append(max([a,b,c])-min([a,b,c]))
a,b,c = w4*h, (w-w4)*h3, (w-w4)*h4
ans.append(max([a,b,c])-min([a,b,c]))

ans.append(h)
ans.append(w)

Submission status: Wrong Answer

Solution code:
import math

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

li = [0] * 3
ans = 10**9

for h in range(1, H):
  li[0] = h * W
  li[1] = (H-h) * math.ceil(W / 2)
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

  li[0] = h * W
  li[1] = math.ceil((H-h) / 2) * W
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

for w in range(1, W):
  li[0] = h * W
  li[1] = (W-w) * math.ceil(H / 2)
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

  li[0] = h * W
  li[1] = math.ceil((W-w) / 2) * H
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

print(math.floor(ans))

Submission status: Wrong Answer

Solution code:
import math

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

li = [0] * 3
ans = 10**9

for h in range(1, H):
  li[0] = h * W
  li[1] = (H-h) * (W // 2)
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

  li[0] = h * W
  li[1] = ((H-h) // 2) * W
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

for w in range(1, W):
  li[0] = h * W
  li[1] = (W-w) * (H // 2)
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

  li[0] = h * W
  li[1] = ((W-w) // 2) * H
  li[2] = H * W - (li[0] + li[1])
  li.sort()
  ans = min(ans, li[2] - li[0])

print(math.floor(ans))

Submission status: Wrong Answer

Solution code:
def calc(a, b, c):
    return max(a, b, c) - min(a, b, c)


h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    ans1 = h * w

    for i in range(1, h+1):
        s1 = i * w
        if w % 2 == 0:
            x = w // 2
            s2 = (h-i) * x
            s3 = s2
        else:
            x = w // 2 + 1
            s2 = (h-i) * x
            s3 = (h-i) * (w-x)
        ans1 = min(ans1, calc(s1, s2, s3))


    ans2 = h * w
    for i in range(1, w+1):
        s1 = i * h
        if h % 2 == 0:
            x = h // 2
            s2 = (w - i) * x
            s3 = s2
        else:
            x = h // 2 + 1
            s2 = (w - i) * x
            s3 = (w - i) * (h - x)
        ans2 = min(ans2, calc(s1, s2, s3))

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

Submission status: Wrong Answer

Solution code:
def calc(a, b, c):
    return max(a, b, c) - min(a, b, c)


h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    ans1 = h * w

    for i in range(1, h//3 + 3):
        s1 = i * w
        if w % 2 == 0:
            x = w // 2
            s2 = (h-i) * x
            s3 = s2
        else:
            x = w // 2 + 1
            s2 = (h-i) * x
            s3 = (h-i) * (w-x)
        ans1 = min(ans1, calc(s1, s2, s3))


    ans2 = h * w
    for i in range(1, w // 3 + 3):
        s1 = i * h
        if h % 2 == 0:
            x = h // 2
            s2 = (w - i) * x
            s3 = s2
        else:
            x = h // 2 + 1
            s2 = (w - i) * x
            s3 = (w - i) * (h - x)
        ans2 = min(ans2, calc(s1, s2, s3))

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

Submission status: Wrong Answer

Solution code:
def calc(A, B):
  if A % 3 == 1:
    AA = A // 3
  if A % 3 == 2:
    AA = A // 3 + 1
  BB = B // 2
  h_list = [AA * B, (A - AA) * BB, (A - AA) * (B - BB)]
  return max(h_list) - min(h_list)
 
H, W = map(int, input().split(' '))
L = list()
if H % 3 == 0 or W % 3 == 0:
  L = [0]
else:
  L = [int(calc(W, H)), int(calc(H, W))]
    
print(min(L))

Submission status: Wrong Answer

Solution code:
def calc(A, B):
  if A % 3 == 1:
    AA = A // 3
  if A % 3 == 2:
    AA = A // 3 + 1
  BB = B // 2
  h_list = [AA * B, (A - AA) * BB, (A - AA) * (B - BB)]
  return max(h_list) - min(h_list)

H, W = map(int, input().split(' '))
ans = int()
L = list()
div1, div2, div3 = 0, 0, 0
if H % 3 == 0 or W % 3 == 0:
  L = [0]
else:
  L = [calc(W, H), calc(H, W)]
    
print(min(L))

Submission status: Wrong Answer

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

for i in range(1,h):
    s1 = i*w

    if (h-i)%2 != 0:
        s2 = (h-1)//2*w
        s3 = ((h-1)//2+1)*w
    else:
        s2 = (h-1)//2*w
        s3 = (h-1)//2*w
    ans = min(ans,max(abs(s1-s2),abs(s1-s3),abs(s2-s3)))

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

h,w = w,h

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

h,w = w,h

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

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

print(ans)

Submission status: Wrong Answer

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

if h%3==0 or w%3==0:
    print("0")
    exit()
#print(h*w)
ans=10**100**2
    #hが大きい方とみなす
if h<w:
    comp=h
    h=w
    w=comp
ans=min(ans,(h//3+1)*w-h//3*w)

print(h*w)
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green#三等分に分ける
    #print(green,red,blue)
    ma=max(green,red,blue)#一番大きい長方形
    mi=min(green,red,blue)#一番小さい長方形
    #print(green,red,blue,ma-mi)
   
    ans=min(ans,ma-mi)
comp=w
w=h
h=comp
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green#三等分に分ける
    ma=max(green,red,blue)#一番大きい長方形
    mi=min(green,red,blue)#一番小さい長方形
    #print(green,red,blue,ma-mi)
   
    ans=min(ans,ma-mi)

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()
#print(h*w)
ans=10**100**2
    
#print(h*w)
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green#三等分に分ける
    #print(green,red,blue)
    ma=max(green,red,blue)#一番大きい長方形
    mi=min(green,red,blue)#一番小さい長方形
    ans=min(ans,ma-mi)
comp=w
w=h
h=comp
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green#三等分に分ける
    #print(green,red,blue)
    ma=max(green,red,blue)#一番大きい長方形
    mi=min(green,red,blue)#一番小さい長方形
    ans=min(ans,ma-mi)
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()
#print(h*w)
ans=10**100**2
if h>w:
    comp=w
    w=h
    h=comp
#print(h*w)
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green#三等分に分ける
    #print(green,red,blue)
    ma=max(green,red,blue)#一番大きい長方形
    mi=min(green,red,blue)#一番小さい長方形
    ans=min(ans,ma-mi)
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()
#print(h*w)
ans=10**100**2
if h>w:
    h=w
    w=h
#print(h*w)
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green#三等分に分ける
    #print(green,red,blue)
    ma=max(green,red,blue)#一番大きい長方形
    mi=min(green,red,blue)#一番小さい長方形
    ans=min(ans,ma-mi)
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()
#print(h*w)
ans=10**100**2
if h<w:
    h=w
    w=h
#print(h*w)
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green#三等分に分ける
    #print(green,red,blue)
    ma=max(green,red,blue)#一番大きい長方形
    mi=min(green,red,blue)#一番小さい長方形
    ans=min(ans,ma-mi)
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()

green=0
red=0

ans=10**100**2
h=min(h,w)
w=max(h,w)
for i in range(1,w):
    green = h*i 
    red   = (h//2) * (w-i)
    blue  = h*w-red-green
    #print(green,red,blue)
    ma=max(green,red,blue)
    mi=min(green,red,blue)
    ans=min(ans,ma-mi)
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()

green=0
red=0

ans=10**100**2
h=min(h,w)
w=max(h,w)
for i in range(1,w+1):
    green = h*i
    red   = (h//2) * (w-i)
    if green>red:
        ans=min(ans,abs(green-red))
print(ans)

Submission status: Wrong Answer

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

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

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

h,w = w,h


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

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

print(ans)

Submission status: Wrong Answer

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

ans=float('inf')
for h in range(1,H+1):
  divs=[h*W,(H-h)*(W//2),abs(H*W-h*W-(H-h)*(W//2))]
  ans=min(ans,max(divs)-min(divs))
  divs=[h*W,((H-h)//2)*W,abs(H*W-h*W-((H-h)//2)*W)]
  ans=min(ans,max(divs)-min(divs))
H,W=W,H
for h in range(1,H+1):
  divs=[h*W,(H-h)*(W//2),abs(H*W-h*W-(H-h)*(W//2))]
  ans=min(ans,max(divs)-min(divs))
  divs=[h*W,((H-h)//2)*W,abs(H*W-h*W-((H-h)//2)*W)]  
print(ans)

Submission status: Wrong Answer

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

ans=float('inf')
if H%3==0 or W%3==0:
  ans=0

for h in range(1,H+1):
  divs=[h*W,(H-h)*(W//2),abs(H*W-h*W-(H-h)*(W//2))]
  ans=min(ans,max(divs)-min(divs))
  divs=[h*W,((H-h)//2)*W,abs(H*W-h*W-((H-h)//2)*W)]
  ans=min(ans,max(divs)-min(divs))
H,W=W,H
for h in range(1,H+1):
  divs=[h*W,(H-h)*(W//2),abs(H*W-h*W-(H-h)*(W//2))]
  ans=min(ans,max(divs)-min(divs))
  divs=[h*W,((H-h)//2)*W,abs(H*W-h*W-((H-h)//2)*W)]  
print(ans)