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

Solution (Python):
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))
	sb = ((h-i)//2) * w
	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))
	sb = h * ((w-i)//2)
	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]]))

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

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

for a, b in ((H, W), (W, H)):
    tmp = (a // 3 * b, (a - a // 3) * (b // 2), (a - a // 3) * (b - b // 2))
    ans = min(ans, max(tmp) - min(tmp))
    tmp = ((a // 3 + 1) * b, (a - a // 3 - 1) * (b // 2), (a - a // 3 - 1) * (b - b // 2))
    ans = min(ans, max(tmp) - min(tmp))

print(ans)

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

candicate=[]
for y in range(1, H):
    
    candicate.append(max(y*W, W * (((H-y)+1)//2)) - min(y*W, W* ((H-y)//2)))
    candicate.append(max(y*W, (H-y)*((W+1)//2)) - min(y*W, (H-y)* (W//2)))

for x in range(1, W):
    candicate.append(max(x*H, H * (((W-x)+1)//2)) - min(x*H, H* ((W-x)//2)))
    candicate.append(max(x*H, (W-x) * ((H+1)//2)) - min(x*H, (W-x)* (H//2)))

print(min(candicate))

Solution (Python):
h,w = map(int,input().split())
if h % 3 == 0 or w % 3 == 0:
  print(0)
  exit()
ans = 10000000000
for change in range(2): 
  
  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)))
  a = round(w/3 + 0.1)
  ans = min(ans, h*(max(a, w-a*2) - min(a, w-a*2)))
  temp = h
  h = w
  w = temp
print(ans)

Solution (Python):
h,w=map(int,input().split())
def solve(h,w):
    ans=h*w
    for i in range(1,h):
        
        a=(h-i)*(w//2)
        b=(h-i)*w-a
        ans=min(ans,max(i*w,a,b)-min(i*w,a,b))
        a=((h-i)//2)*(w)
        b=(h-i)*w-a
        ans=min(ans,max(i*w,a,b)-min(i*w,a,b))
    return ans
print(min(solve(h,w),solve(w,h)))

Solution (Python):
h,w = map(int,input().split())
S = h*w

ans = h*w

for a,b in [(h,S//(3*h)),(h,S//(3*h)+1),(S//(3*w),w),(S//(3*w)+1,w)]:
    
    s_list = [a*b]
    hr = h - a*(a<h)
    wr = w - b*(b<w)
    
    if (hr*wr)%2 == 0:
        s_list += [hr*wr//2]
    else:
        s_list += [min(hr,wr)*(max(hr,wr)//2), min(hr,wr)*(max(hr,wr)//2+1)]
    ans = min(ans, max(s_list)-min(s_list))
print(ans)

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque


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)

    print(ans)

Solution (Python):
h,w=map(int,input().split())
m=s=h*w
def f(x,y):
  global m
  g=lambda:min(m,max(p,q,r)-min(p,q,r))
  for i in (x//3,x//3+1):
    p=i*y
    q=(x-i)//2*y; r=s-p-q; m=g()
    q=y//2*(x-i); r=s-p-q; m=g()
  return m
f(h,w);f(w,h)
print(m)

Solution (Python):
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
    if ans > min(h, w):
        ans = min(h, w)
print(ans)

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

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

ans = min(h, w)

for i in range(w//3, w//3 + 2):
    s = [h * i]
    s.append((w - i) * (h // 2))
    s.append((w - i) * (h - h // 2))
    ans = min(ans, max(s) - min(s))

h, w = w, h

for i in range(w//3, w//3 + 2):
    s = [h * i]
    s.append((w - i) * (h // 2))
    s.append((w - i) * (h - h // 2))
    ans = min(ans, max(s) - min(s))
print(ans)

Solution (Python):
from itertools import permutations
from math import ceil, floor

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

res = h * w
for l in permutations((h, w), 2):
    for i in range(1, l[0]):
        s1 = i * l[1]
        for m in permutations((l[0] - i, l[1]), 2):
            s2 = floor(m[0] / 2) * m[1]
            s3 = ceil(m[0] / 2) * m[1]
            val = max(s1, s2, s3) - min(s1, s2, s3)
            if val < res:
                res = val

print(res)

Solution (Python):
h,w=map(int,input().split())
ans=((h+2)//3-h//3)*w
ans=min(ans,((w+2)//3-w//3)*h)
for i in range(1,h):
  s=[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]
  ans=min(ans,max(s)-min(s))
for i in range(1,w):
  s=[i*h,(w-i)*(h//2),(w-i)*(h-h//2)]
  ans=min(ans,max(s)-min(s))
print(ans)

Solution (Python):
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;D=math.ceil(c/2)*d
                ans=min(ans,max(A,C,D)-min(A,C,D))
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
INF = 10**20
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)

def main():
    h,w = map(int,input().split())

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

    ans = min(h,w)

    h2 = h//2
    for i in range(1,w):
        S = [h2 * i, (h - h2) * i, h * (w - i)]
        S.sort()
        ans = min(ans,S[-1] - S[0])
    
    w2 = w//2
    for i in range(1,h):
        S = [w2 * i, (w - w2) * i, w * (h - i)]
        S.sort()
        ans = min(ans,S[-1] - S[0])
    
    print(ans)
if __name__=='__main__':
    main()

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

Solution (Python):
import sys
input = sys.stdin.readline
H, W = map(int, input().split())

ans = 10**10
S = [0,0,0]
for h in range(H) :
    S[0] = h*W
    S[1] = (H-h)*(W//2)
    S[2] = (H-h)*((W+1)//2)
    ans = min(ans, max(S)-min(S))

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

for w in range(W) :
    S[0] = w*H
    S[1] = (W-w)*(H//2)
    S[2] = (W-w)*((H+1)//2)
    ans = min(ans, max(S)-min(S))

for w in range(W) :
    S[0] = w*H
    S[1] = ((W-w)//2)*H
    S[2] = ((W-w+1)//2)*H
    ans = min(ans, max(S)-min(S))

print(ans)

Solution (Python):
def main():
  h, w = map(int, input().split())
  S = h*w
  if S%3==0:
    return 0

  ds = [h, w]
  
  h1 = (h+1)//3
  s1 = h1*w; s2 = w//2*(h-h1); s3 = S-s1-s2
  ds.append(max(s1,s2,s3)-min(s1,s2,s3))
  
  w1 = (w+1)//3
  s1 = w1*h; s2 = h//2*(w-w1); s3 = S-s1-s2
  ds.append(max(s1,s2,s3)-min(s1,s2,s3))
  
  return min(ds)

print(main())

Solution (Python):
H, W = map(int, input().split())
S = H*W
ans = 10**10
for i in range(1,H):
  if (H-i)*W%2==0:
    m = (H-i)*W//2
    ans = min(ans, abs(i*W-m))
  else:
    if (H-i)<W:
      m1,m2 = (H-i)*(W//2), (H-i)*(W//2+1)
      x = max(abs(m1-m2), abs(m2-i*W), abs(m1-i*W))
      ans = min(ans,x)
    else:
      m1,m2 = W*((H-i)//2), W*((H-i)//2+1)
      x = max(abs(m1-m2), abs(m2-i*H), abs(m1-i*H))
      ans = min(ans,x)

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

print(ans)

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

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

    if w == 2:
        a = 1
        a1 = a * h
        b = h // 2
        a2 = (w-a) * b
        a3 = (w-a) * (h-b)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
    else:
        for i in range(3):
            a = w // 3 + i
            a1 = a * h
            b = h // 2
            a2 = (w-a) * b
            a3 = (w-a) * (h-b)
            ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))

    if h == 2:
        a = 1
        a1 = a * w
        b = w // 2
        a2 = (h-a) * b
        a3 = (h-a) * (w-b)
        ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
    else:
        for i in range(3):
            a = h // 3 + i
            a1 = a * w
            b = w // 2
            a2 = (h-a) * b
            a3 = (h-a) * (w-b)
            ans = min(ans, max(a1, a2, a3) - min(a1, a2, a3))
    
    print(ans)

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


d1 = (H + 2) // 3 * W - H // 3 * W

d2 = (W + 2) // 3 * H - W // 3 * H

d3 = 10**18
for i in range(W + 1):
    d = max(H * i, (H + 1) // 2 * (W - i), H // 2 * (W - i)) - min(H * i, (H + 1) // 2 * (W - i), H // 2 * (W - i))
    d3 = min(d3, d)

d4 = 10**18
for i in range(H + 1):
    d = max(W * i, (W + 1) // 2 * (H - i), W // 2 * (H - i)) - min(W * i, (W + 1) // 2 * (H - i), W // 2 * (H - i))
    d3 = min(d3, d)
    
print(min(d1, d2, d3, d4))

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()

ans = INF

for i in range(2):
    if i == 1:
        H, W = W, H
    for a in range(1, H):
        bc = H - a
        b = int(bc/2)
        c = bc - b
        sa, sb, sc = a * W, b * W, c * W
        s = max(sa, sb, sc) - min(sa, sb, sc)
        ans = min(s, ans)
    for a in range(1, H):
        bc = H - a
        b = int(W/2)
        c = W - b
        sa, sb, sc = a * W, b * bc, c * bc
        s = max(sa, sb, sc) - min(sa, sb, sc)
        ans = min(s, ans)
print(ans)

Solution (Python):

import sys

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


def check(h,w):
    area_diff = h*w

    for i in range(1,h//2+1):
        
        area = [ i*w , ((h-i)//2)*w , ((h-i)//2 + (h-i)%2)*w ]
        area.sort()
        diff = area[2] - area[0]
        area_diff = min( area_diff , diff )
        
        
        area = [ i*w , (h-i)*(w//2) , (h-i)*(w//2 + w%2) ]
        area.sort()
        diff = area[2] - area[0]
        area_diff = min( area_diff , diff )

    return area_diff



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

res1 = check(H,W)
res2 = check(W,H)
print( min(res1,res2) )

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7


H, W = MI()

pieces = H * W


memo = dict()
def divide(h, w):
    if memo.get((h, w)) is not None:
        return memo[(h, w)]
    elif memo.get((w, h)) is not None:
        return memo[(w, h)]
    p = h * w
    if w > 1:
        a1 = w // 2 * h
        a2 = p - a1
        memo[(h, w)] = (a1, a2)
    if h > 1:
        b1 = h // 2 * w
        b2 = p - b1
        if memo.get((h, w)) is not None:
            a1, a2 = memo[(h, w)]
            if abs(a1 - a2) > abs(b1 - b2):
                memo[(h, w)] = (b1, b2)
    return memo[(h, w)]


ans = float('inf')
for i in range(1, W):
    p1 = i * H
    p2, p3 = divide(W - i, H)
    ans = min(ans, max(p1, p2, p3) - min(p1, p2, p3))
for i in range(1, H):
    p1 = i * W
    p2, p3 = divide(H - i, W)
    ans = min(ans, max(p1, p2, p3) - min(p1, p2, p3))
print(ans)

Solution (Python):
a,b=map(int,input().split())
ans=a
for i in range(a):
   x=i*b
   a2=a-i
   if a2*b%2==0:
     y=a2*b//2
   else:
     y=min(a2*(b+1)//2,(a2+1)*b//2)
   z=a2*b-y
   diff=max(x,y,z)-min(x,y,z)
   ans=min(ans,diff)
for i in range(b):
   x=i*a
   b2=b-i
   if b2*a%2==0:
     y=b2*a//2
   else:
     y=min(b2*(a+1)//2,(b2+1)*a//2)
   z=b2*a-y
   diff=max(x,y,z)-min(x,y,z)
   ans=min(ans,diff)
print(ans)

Solution (Python):
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))

    
    ans = min(ans, (H//3+1)*W - (H//3)*W)
    ans = min(ans, (W//3+1)*H - (W//3)*H)

    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

H, W = [int(x) for x in input().split()]
S = H * W
res1 = 10 ** 10
res2 = 10 ** 10


for i in range(1, H):
    S1 = i * W
    x = W
    y = H - i

    
    S2 = (y // 2) * x
    S3 = S - S1 - S2
    res1 = min(res1, max(S1, S2, S3) - min(S1, S2, S3))

    
    S2 = y * (x // 2)
    S3 = S - S1 - S2
    res1 = min(res1, max(S1, S2, S3) - min(S1, S2, S3))


for j in range(1, W):
    S1 = H * j
    x = W - j
    y = H

    
    S2 = (y // 2) * x
    S3 = S - S1 - S2
    res2 = min(res2, max(S1, S2, S3) - min(S1, S2, S3))

    
    S2 = y * (x // 2)
    S3 = S - S1 - S2
    res2 = min(res2, max(S1, S2, S3) - min(S1, S2, S3))

print(min(res1, res2))

Solution (Python):
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*W)%3==0:
        print(0)
    else:
        def tw(h,w):
            
            if (h*w)%2==0:
                return h*w//2,h*w//2
            else:
                a=h//2*w
                b=h*w - a
                c=w//2*h
                d=h*w - c
                if abs(a-b)<=abs(c-d):
                    return a,b
                else:
                    return c,d
                
            
        ans=10**10
        s=[0,0,0]
        
        for i in range(1,H):
            s[0]=i*W
            s[1],s[2]=tw(H-i,W)
            temp=max(s)-min(s)
            ans=min(ans,temp)
            
        for j in range(1,W):
            s[0]=j*H
            s[1],s[2]=tw(H,W-j)
            temp=max(s)-min(s)
            ans=min(ans,temp)
        print(ans)
            
    
            

main()

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

Solution (Python):
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 - i)
        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 - j)
        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)

Solution (Python):
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 - i)
        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 - j)
        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)

Solution (Python):

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

Solution (Python):
H,W = map(int,input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()

ans = float('inf')
for _ in range(2):
    for i in [H//3,H//3+1]:
        a = i*W
        b = ((H - i)//2)*W
        c = ((H - i + 1)//2)*W
        ans = min(ans, abs(max(a, b, c) - min(a, b, c)))

        b = (H-i)*(W//2)
        c = (H-i)*((W + 1)//2)
        ans = min(ans, abs(max(a, b, c) - min(a, b, c)))
    H, W = W, H
print(ans)

Solution (Python):
def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        return 0
    m = 10**9
    
    m = min(H, m)
    
    m = min(W, m)
    
    g = H * (W // 3)
    b = (W - (W // 3)) * (H // 2)
    r = H * W - g - b
    m = min(m, max(abs(g-b), abs(g-r), abs(b-r)))
    
    g = H * ((W // 3) + 1)
    b = (W - (W // 3) - 1) * (H // 2)
    r = H * W - g - b
    m = min(m, max(abs(g-b), abs(g-r), abs(b-r)))
    
    g = W * (H // 3)
    b = (H - (H // 3)) * (W // 2)
    r = H * W - g - b
    m = min(m, max(abs(g-b), abs(g-r), abs(b-r)))
    
    g = W * ((H // 3) + 1)
    b = (H - (H // 3)-1) * (W // 2)
    r = H * W - g - b
    m = min(m, max(abs(g-b), abs(g-r), abs(b-r)))
    return m
print(main())

Solution (Python):
H, W = map(int, input().split())
areas = [[] for _ in range(4)]

h1 = round(H/3)
areas[0].append(h1*W)
areas[1].append(h1*W)
H -= h1
h2 = round(H/2)
w2 = round(W/2)
areas[0].append(h2*W)
areas[0].append((H-h2)*W)
areas[1].append(H*w2)
areas[1].append(H*(W-w2))

H += h1

w1 = round(W/3)
areas[2].append(H*w1)
areas[3].append(H*w1)
W -= w1
h2 = round(H/2)
w2 = round(W/2)
areas[2].append(h2*W)
areas[2].append((H-h2)*W)
areas[3].append(H*w2)
areas[3].append(H*(W-w2))

ans = H*W
for area in areas:
    area.sort()
    ans = min(ans, area[2] - area[0])
print(ans)

Solution (Python):
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)
        
    
    for x, y in [[h, w], [w, h]]:
        ans = min(ans, x*(y//3+1) - x*(y//3))
    
print(ans)

Solution (Python):
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)

if w%2==0:
  ans.append(abs(h1*w-(h-h1)*w//2))
  ans.append(abs(h2*w-(h-h2)*w//2))

if h%2==0:
  ans.append(abs(w3*h-(w-w3)*h//2))
  ans.append(abs(w4*h-(w-w4)*h//2))
  
if h%3==0 or w%3==0:
  ans.append(0)
  
print(min(ans))

Solution (Python):
h, w = map(int, input().split())
 
if h%3 == 0 or w%3 == 0:
    print(0)
    exit()
 

min_ = float('inf')
for i in range(1, h):
    s1 = w*i
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w-w//2)
    d = max([s1, s2, s3]) - min([s1, s2, s3])
    
    min_ = min(min_, d)
 
    s2 = ((h-i)//2)*w
    s3 = ((h-i) - (h-i)//2)*w
    d = max([s1, s2, s3]) - min([s1, s2, s3])
    
    min_ = min(min_, d)
 

 
for i in range(1, w):
    s1 = h*i
    s2 = (w-i)*(h//2)
    s3 = (w-i)*(h-h//2)
    d = max([s1, s2, s3]) - min([s1, s2, s3])
    
    min_ = min(min_, d)
 
    s2 = ((w-i)//2)*h
    s3 = ((w-i) - (w-i)//2)*h
    d = max([s1, s2, s3]) - min([s1, s2, s3])
    
    min_ = min(min_, d)
 
print(min_)

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

def f(a, b):
	mod = b % 3
	if mod == 0:
		return 0
	else:
		return 1 



dif = H * f(H, W)
ans = min(ans, dif)




dif = W * f(W, H)
ans = min(ans, dif)




for i in range(W):
	s_1 = H * i
	aft_W = W - i
	
	s_2 = aft_W * (H // 2)
	s_3 = aft_W * (H - H // 2)
	
	dif = max(s_1, s_2, s_3) - min(s_1, s_2, s_3)
	ans = min(ans, dif)
	

	
	

for i in range(H):
	s_1 = W * i
	aft_H = H - i
	
	s_2 = aft_H * (W // 2)
	s_3 = aft_H * (W - W // 2)
	
	dif = max(s_1, s_2, s_3) - min(s_1, s_2, s_3)
	ans = min(ans, dif)
	
	
print(ans)

Solution (Python):
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:
    ans0 = min(h, w)
    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(ans0, ans1, ans2)
print(ans)

Solution (Python):
h,w = map(int,input().split())
ass = 10**9
if h%3 == 0:
    ass = 0
else:
    ass = w

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


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

if w % 2 == 0:
    for i in range(1,h):
        ass = min(ass,abs(w*i-(h-i)*w//2))
else:
    for i in range(1,h):
        ass = min(ass,max(abs(w*i-(w-1)*(h-i)//2),abs(w*i-(w+1)*(h-i)//2),h-i))
print(ass)

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

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

ans=10**100**2
    
if h<w:
    comp=h
    h=w
    w=comp
ans=min(ans,(h//3+1)*w-h//3*w)


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)
    
   
    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)
    
   
    ans=min(ans,ma-mi)

print(ans)

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

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

Solution (Python):
H, W = map(int, input().split())
 
res = min(H, W)
for h in range(H // 3, H // 3 + 3):
    w = W // 2
    s1 = h*W
    s2 = (H-h)*w
    s3 = (H-h)*(W-w)
    res = min(res, max(s1,s2,s3) - min(s1,s2,s3))
for w in range(W // 3, W // 3 + 3):
    h = H // 2
    s1 = w*H
    s2 = (W-w)*h
    s3 = (W-w)*(H-h)
    res = min(res, max(s1,s2,s3) - min(s1,s2,s3))
if H % 3 == 0 or W % 3 == 0:
    res = 0
print(res)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def main():
    H,W = map(int,input().split())
    if (H%3 != 0 and W%3 != 0):
        ans = min(H,W)
        a = H//3
        b = (H-a)*(W//2)
        c = (H-a)*(-(-W//2))
        d = max(a*W,b,c)-min(a*W,b,c)
        a1 = a+1
        b1 = (H-a1)*(W//2)
        c1 = (H-a1)*(-(-W//2))
        d1 = max(a1*W,b1,c1)-min(a1*W,b1,c1)
        s = W//3
        t = (W-s)*(H//2)
        u = (W-s)*(-(-H//2))
        v = max(s*H,t,u)-min(s*H,t,u)
        s1 = s+1
        t1 = (W-s1)*(H//2)
        u1 = (W-s1)*(-(-H//2))
        v1 = max(s1*H,t1,u1)-min(s1*H,t1,u1)
        ans = min(ans,d,d1,v,v1)
        print(ans)
    else:
        print(0)

if __name__ == "__main__":
    main()

Solution (Python):
H,W=map(int,input().split())
if (H*W)%3==0:
  ret=0
else:
  ret=min(H,W)
  S=[0]*3
  for h in range(1,H):
    S=[(W//2)*h, (W-W//2)*h, W*(H-h)]
    ret=min(ret,max(S)-min(S))
  for w in range(1,W):
    S=[(H//2)*w, (H-H//2)*w, H*(W-w)]
    ret=min(ret,max(S)-min(S))
print(ret)

Solution (Python):
H, W = map(int, input().split())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    for i in range(1, H):
        kari = W * i
        if (H - i) % 2 == 0 or W % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            kari_2 = (W // 2 + 1) * (H-i)
            kari_3 = (W // 2) * (H-i)
            ans = min(ans, max(kari, kari_2, kari_3)-min(kari, kari_2, kari_3))
    for j in range(1, W):
        kari = H * j
        if (W - j) % 2 == 0 or H % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:

            kari_2 = (H // 2 + 1) * (W-j)
            kari_3 = (H // 2) * (W-j)
            ans = min(ans, max(kari, kari_2, kari_3)-min(kari, kari_2, kari_3))
    ans = min(ans, W, H)

    print(ans)

Solution (Python):
H,W=map(int, input().split())
a=10**10
S=H*W
s=[None]*3
for i in range(W):
    s[0]=H*i
    s[1]=(W-i)//2*H
    s[2]=S-s[0]-s[1]
    a=min(a,max(s)-min(s))
    s[1]=H//2*(W-i)
    s[2]=S-s[0]-s[1]
    a=min(a,max(s)-min(s))
for i in range(H):
    s[0]=W*i
    s[1]=(H-i)//2*W
    s[2]=S-s[0]-s[1]
    a=min(a,max(s)-min(s))
    s[1]=W//2*(H-i)
    s[2]=S-s[0]-s[1]
    a=min(a,max(s)-min(s))
print(a)

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

Solution (Python):
def areadifference(h , w):
    ans = 0
    count1 = 10000000000
    count2 = 10000000000
    if h % 3 == 0 or w % 3 == 0:
        return ans

    for i in range(1 , h + 1):
        hcount = max( i * w , (((h - i)//2) * w) , ((-(-(h - i)//2)) * w)) - min( i * w , (((h - i)//2) * w) , ((-(-(h - i)//2)) * w))
        wcount = max( i * w , ((h - i) * (w//2)) , ((h - i) * (-(-w//2)))) - min( i * w , ((h - i) * (w//2)) , ((h - i) * (-(-w//2))))
        count1 = min(count1 , hcount , wcount)

    for i in range(1 , w + 1):
        hcount = max( i * h , (((w - i)//2) * h) , ((-(-(w - i)//2)) * h)) - min( i * h , (((w - i)//2) * h) , ((-(-(w - i)//2)) * h))
        wcount = max( i * h , ((w - i) * (h//2)) , ((w - i) * (-(-h//2)))) - min( i * h , ((w - i) * (h//2)) , ((w - i) * (-(-h//2))))
        count2 = min(count2 , hcount , wcount)

    ans = min(count1 , count2)

    return ans

def main():
    h , w = map(int , input().split())
    print(areadifference(h , w))

if __name__ == '__main__':
    main()

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