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())

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

Solution (Python):
def calc(h,w):
  ans = w
  for i in range(1,h):
    s1 = w*i
    s2 = (h-i)*(w//2)
    s3 = (h-i)*(w-w//2)
    s = [s1,s2,s3]
    ds = max(s)-min(s)
    ans = min([ans,ds])
  return ans

h,w = map(int,input().split())
if (h*w)%3==0:
  print(0)
else:
  print(min([calc(h,w),calc(w,h)]))

Solution (Python):

import sys
INF = float("inf")


def argmin(a):
    m, n = 1 << 31, -1
    for i, v in enumerate(a):
        if m > v:
            m, n = v, i
    return m, n


def ternary_search(f, x0, x3):
    """
    3分探索を行う。下に凸な関数fの、区間[x0, x3]内にある極値を探し、(x, f(x))を返す。
    なければ、区間の端点が返る。
    fは整数引数、x0,x3も整数引数とする。
    """
    if x0 + 2 == x3:
        xs = [x0, x0+1, x0+2]
        v, i = argmin(list(map(f, xs)))
        return xs[i], v

    x1 = (2*x0+x3)//3
    x2 = (x0+2*x3)//3

    if f(x1) >= f(x2):
        return ternary_search(f, x1, x3)
    else:
        return ternary_search(f, x0, x2)


def solve(H: int, W: int):
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return

    cand = INF
    y = 1

    def g(x, y):
        return max(x*y, W*(H-y), y*(W-x))-min(x*y, W*(H-y), y*(W-x))

    def h(x, y):
        return max(x*y, x*(H-y), H*(W-x))-min(x*y, x*(H-y), H*(W-x))

    if H == W == 2:
        print(1)
        return
    else:
        cand = min(cand, min(H, W))

    y0, y3 = 1, H-1
    x = W//2
    while y0 + 2 < y3:
        y1 = (2*y0+y3)//3
        y2 = (y0+2*y3)//3
        S1 = g(x, y1)
        S2 = g(x, y2)
        if S1 >= S2:
            y0 = y1
        else:
            y3 = y2
    ys = [y0, y0+1, y0+2]
    for y in ys:
        S = g(x, y)
        cand = min(S, cand)

    x0, x3 = 1, W-1
    y = H//2
    while x0 + 2 < x3:
        x1 = (2*x0+x3)//3
        x2 = (x0+2*x3)//3
        S1 = h(x1, y)
        S2 = h(x2, y)
        if S1 >= S2:
            x0 = x1
        else:
            x3 = x2
    xs = [x0, x0+1, x0+2]
    for x in xs:
        S = h(x, y)
        cand = min(S, cand)
    print(cand)
    return


def main():

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


if __name__ == '__main__':
    main()

Solution (Python):
def tsep(x,y):
    m=x*y
    for i in range(1,y):
        s=[i*x,(y-i)*(x-x//2),(y-i)*(x//2)]
        m=min(m,max(s)-min(s))
    return m
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)
    exit()
print(min(W,H,tsep(H,W),tsep(W,H)))

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 




 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    H,W = inputMap()
    
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        sys.exit()
        
    if H <= 2 and W <= 2:
        print(1)
        sys.exit()
        
    a = -(-W // 3)
    b = -(-H // 2)
    s1 = a * H
    s2 = (W-a) * b
    s3 = (W-a) * (H-b)
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a -= 1
    s1 = a * H
    s2 = (W-a) * b
    s3 = (W-a) * (H-b)
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a += 2
    s1 = a * H
    s2 = (W-a) * b
    s3 = (W-a) * (H-b)
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    ans = min(ans1,ans2,ans3)
    
    tmp = W // 3
    tmp2 = (W - tmp) // 2
    s1 = tmp * H
    s2 = tmp2 * H
    s3 = (W - tmp- tmp2) * H
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp += 1
    s1 = tmp * H
    s2 = tmp2 * H
    s3 = (W - tmp- tmp2) * H
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp -= 2
    s1 = tmp * H
    s2 = tmp2 * H
    s3 = (W - tmp- tmp2) * H
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    ansans = min(ans,ans1,ans2,ans3)
    
    a = -(-H // 3)
    b = -(-W // 2)
    s1 = a * W
    s2 = (H-a) * b
    s3 = (H-a) * (W-b)
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a -= 1
    s1 = a * W
    s2 = (H-a) * b
    s3 = (H-a) * (W-b)
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a += 2
    s1 = a * W
    s2 = (H-a) * b
    s3 = (H-a) * (W-b)
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    ans = min(ans1,ans2,ans3)
    
    tmp = H // 3
    tmp2 = (H - tmp) // 2
    s1 = tmp * W
    s2 = tmp2 * W
    s3 = (H - tmp- tmp2) * W
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp += 1
    s1 = tmp * W
    s2 = tmp2 * W
    s3 = (H - tmp- tmp2) * W
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp -= 2
    s1 = tmp * W
    s2 = tmp2 * W
    s3 = (H - tmp- tmp2) * W
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    print(min(ans,ans1,ans2,ans3,ansans))
	
if __name__ == "__main__":
	main()

Solution (Python):
x,y = map(int,input().split())
ans = x*y
def compare(s1,s2,s3):
    s = [s1,s2,s3]
    s.sort()
    global ans
    ans = min(ans,abs(s[0]-s[2]))

def divide2(s1,h,w):
    compare(s1,h//2*w,(h-h//2)*w)
    compare(s1,(h//2+1)*w,(h-(h//2+1))*w)
    compare(s1,h*(w//2),h*(w-w//2))
    compare(s1,h*(w//2+1),h*(w-(w//2+1)))


def divide3(h,w):
    divide2((h//3)*w,h-h//3,w)
    divide2((h//3+1)*w,h-(h//3+1),w)
    divide2((w//3)*h,h,w-(w//3))
    divide2((w//3+1)*h,h,w-(w//3+1))

divide3(x,y)
print(ans)

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

def diff(a, b, c) :
    return max([a, b, c]) - min([a, b, c])

def div2(H, W, s) :
    h2, w2 = H//2, W//2
    m = float('inf')
    m = min(m, diff(s, h2*W, (H-h2)*W))
    m = min(m, diff(s, w2*H, (W-w2)*H))
    return m
    

def div3(H, W) :
    m = float('inf')
    h3, w3 = H//3, W//3
    m = min(m, div2(H-h3, W, h3*W))
    m = min(m, div2(H-(h3+1), W, (h3+1)*W))
    m = min(m, div2(H, W-w3, w3*H))
    m = min(m, div2(H, W-(w3+1), (w3+1)*H))
    return m
    
print(div3(H, W))

Solution (Python):
h,w = map(int,input().split())
can = []
hm = h//3
hp = hm+1
wm = w//3
wp = wm+1
now = [hm*w]
if (h-hm) % 2 == 0 or w % 2 == 0:
  now.append((h-hm)*w//2)
  now.append((h-hm)*w//2)
else:
  maxi = max((h-hm),w)
  mini = min((h-hm),w)
  now.append((maxi//2)*mini)
  now.append((maxi//2+1)*mini)
can.append(max(now)-min(now))
now = [hp*w]
if (h-hp) % 2 == 0 or w % 2 == 0:
  now.append((h-hp)*w//2)
  now.append((h-hp)*w//2)
else:
  maxi = max((h-hp),w)
  mini = min((h-hp),w)
  now.append((maxi//2)*mini)
  now.append((maxi//2+1)*mini)
can.append(max(now)-min(now))
now = [wm*h]
if (w-wm) % 2 == 0 or h % 2 == 0:
  now.append((w-wm)*h//2)
  now.append((w-wm)*h//2)
else:
  maxi = max((w-wm),h)
  mini = min((w-wm),h)
  now.append((maxi//2)*mini)
  now.append((maxi//2+1)*mini)
can.append(max(now)-min(now))
now = [wp*h]
if (w-wp) % 2 == 0 or h % 2 == 0:
  now.append((w-wp)*h//2)
  now.append((w-wp)*h//2)
else:
  maxi = max((w-wp),h)
  mini = min((w-wp),h)
  now.append((maxi//2)*mini)
  now.append((maxi//2+1)*mini)
can.append(max(now)-min(now))
print(min(can))

Solution (Python):
import math
H, W = map(int, input().split())
ans = 10**10
for h in range(1, H):
    A = h * W
    
    B = W*math.ceil((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    
    B = (H-h)*math.ceil(W/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)


H, W = W, H
for h in range(1, H):
    A = h * W
    
    B = W*math.ceil((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    
    B = (H-h)*math.ceil(W/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
print(ans)

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

for h in range(1,H+1):
  S1 = h*W
  S2 = (H-h)*(W//2)
  S3 = (H-h)*(W-W//2)
  Smax = max(S1,S2,S3)
  Smin = min(S1,S2,S3)
  tmp1 = Smax-Smin
  S4 = (H-h)//2*W
  S5 = ((H-h)-(H-h)//2)*W
  Smax = max(S1,S4,S5)
  Smin = min(S1,S4,S5)
  tmp2 = Smax-Smin
  ans = min(ans,tmp1,tmp2)
for w in range(1,W+1):
  S1 = H*w
  S2 = (W-w)*(H//2)
  S3 = (W-w)*(H-H//2)
  Smax = max(S1,S2,S3)
  Smin = min(S1,S2,S3)
  tmp1 = Smax-Smin
  S4 = (W-w)//2*H
  S5 = ((W-w)-(W-w)//2)*H
  Smax = max(S1,S4,S5)
  Smin = min(S1,S4,S5)
  tmp2 = Smax-Smin
  ans = min(ans,tmp1,tmp2)
print(ans)

Solution (Python):
def cut(h,w):
    d = 10 ** 9
    for i in range(1,h):
        s1 = i * w
        hh = h - i
        if hh > 1:
            j = hh // 2
            s2 = j * w
            s3 = (hh - j) * w
            smax = max(s1,s2,s3)
            smin = min(s1,s2,s3)
            if smax - smin < d:
                d = smax - smin
        
        if w >= 2:
            k = w // 2
            s2 = k * hh
            s3 = (w - k) * hh
            smax = max(s1,s2,s3)
            smin = min(s1,s2,s3)
            if smax - smin < d:
                d = smax - smin
    return d

x,y = map(int,input().split())
ans1 = cut(x,y)
ans2 = cut(y,x)
print(min(ans1,ans2))

Solution (Python):

from itertools import accumulate


def divide_bar(A: int, B: int) -> int:
    
    
    ret = set()
    area = A * B
    for i, x in enumerate(accumulate([A] * (B // 2)), 1):
        y = area - x
        y1 = y * (A // 2) // A
        y2 = y - y1
        y3 = y * ((B - i) // 2) // (B - i)
        y4 = y - y3
        ret |= {max(x, y1, y2) - min(x, y1, y2)}
        ret |= {max(x, y3, y4) - min(x, y3, y4)}
    return min(ret)


def main():
    H, W = tuple(map(int, input().rstrip().split()))
    ans = min(divide_bar(H, W), divide_bar(W, H))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
H, W = map(int, input().split())
ans = min(((W+2)//3 - W//3)*H, ((H+2)//3 - H//3)*W)
S = H * W
s = [0]*3

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

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

print(ans)

Solution (Python):
from sys import stdin
H, W = [int(_) for _ in stdin.readline().rstrip().split()]
ans = 1e10 
for _ in range(2):
    for i in range(1, H):
        a = i*W
        b = (W//2) * (H-i)
        c = ((W+1)//2 * (H-i))
        ans = min(ans, abs(max([a,b,c]) - min([a,b,c])))
        b = ((H-i)//2 * W)
        c = ((H-i+1)//2 * W)
        ans = min(ans, abs(max([a,b,c]) - min([a,b,c])))
    H, W = W, H

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
ans=10**10
if h>=3:
    if h%3==0:
        ans=0
    else:
        ans=min(w,ans)
if w>=3:
    if w%3==0:
        ans=0
    else:
        ans=min(h,ans)
for i in range(1,w):
    a=h*i
    if h%2==0:
        b=(w-i)*h//2
        c=b
    else:
        b=(w-i)*((h+1)//2)
        c=(w-i)*h-b
    temp=max(a,b,c)-min(a,b,c)
    ans=min(ans,temp)
for i in range(1,h):
    a=w*i
    if w%2==0:
        b=(h-i)*w//2
        c=b
    else:
        b=(h-i)*((w+1)//2)
        c=(h-i)*h-b
    temp=max(a,b,c)-min(a,b,c)
    ans=min(ans,temp)
print(ans)

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

m=0 if h%3==0 else w
n=0 if w%3==0 else h
m=min(m,n)
for i in range(1,w):
    s=sorted([h*i,(h//2)*(w-i),h*w-h*i-(h//2)*(w-i)])
    m=min(m,s[2]-s[0])
for i in range(1,h):
    s=sorted([w*i,(w//2)*(h-i),h*w-w*i-(w//2)*(h-i)])
    m=min(m,s[2]-s[0])
print(m)

Solution (Python):
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    s1 = round(h / 3) * w
    S1 = h * round(w / 3)
    h2 = h - round(h / 3)
    w2 = w - round(w / 3)
    if h2 % 2 == 0 or w % 2 == 0:
        s2 = h2 * w // 2
        s3 = s2
    else:
        h2, w = min(h2, w), max(h2, w)
        s2 = h2 * (w // 2)
        s3 = h2 * (w // 2 + 1)
    if h % 2 == 0 or w2 % 2 == 0:
        S2 = h * w2 // 2
        S3 = S2
    else:
        h, w2 = min(h, w2), max(h, w2)
        S2 = h * (w2 // 2)
        S3 = h * (w2 // 2 + 1)
    print(min(max(s1, s2, s3) - min(s1, s2, s3), max(S1, S2, S3) - min(S1, S2, S3)))

Solution (Python):
def solve(h,w):
    ans1 = float("inf")
    ans2 = float("inf")
    for i in range(1,h):
        w_mid = w//2
        s1_max = max(i*w,(h-i)*w_mid,(h-i)*(w-w_mid))
        s1_min = min(i*w,(h-i)*w_mid,(h-i)*(w-w_mid))
        ans1 = min(ans1,s1_max - s1_min)
    for i in range(1,h):
        h_mid = (h-i)//2
        s2_max = max(i*w,h_mid*w,(h-i-h_mid)*w)
        s2_min = min(i*w,h_mid*w,(h-i-h_mid)*w)
        ans2 = min(ans2,s2_max-s2_min)
    return min(ans1,ans2)
def main():
    h,w = map(int,input().split())
    print(min(solve(h,w),solve(w,h)))
if __name__=='__main__':
    main()

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    x, y = LI()
    a = [1, 3, 5, 7, 8, 10, 12]
    b = [4, 6, 9, 11]
    c = [2]
    if x in a and y in a:
        print("Yes")
        return
    if x in b and y in b:
        print("Yes")
        return
    if x in c and y in c:
        print("Yes")
        return
    print("No")
    return


def B():
    h, w = LI()
    print("
    for _ in range(h):
        a = ["
        for s in S():
            a.append(s)
        a.append("
        print("".join(a))
    print("
    return


def C():
    h, w = LI()
    ans = inf
    for i in range(1, h):
        ans = min(ans, max(i * w, (h - i) * (w // 2), (h - i) * (w - w // 2)) - min(i * w, (h - i) * (w // 2), (h - i) * (w - w // 2)))
        ans = min(ans, max(i * w, (h - i) // 2 * w, (h - i - (h - i) // 2) * w) - min(i * w, (h - i) // 2 * w, (h - i - (h - i) // 2) * w))
    h, w = w, h
    for i in range(1, h):
        ans = min(ans, max(i * w, (h - i) * (w // 2), (h - i) * (w - w // 2)) - min(i * w, (h - i) * (w // 2), (h - i) * (w - w // 2)))
        ans = min(ans, max(i * w, (h - i) // 2 * w, (h - i - (h - i) // 2) * w) - min(i * w, (h - i) // 2 * w, (h - i - (h - i) // 2) * w))
    print(ans)
    return


def D():
    return



if __name__ == '__main__':
    C()

Solution (Python):
import math

def main(H,W):
    ans = float('inf')
    w = math.floor(W / 2)
    for i in range(H):
        Smin = min(i * W, w * (H - i), (W - w) * (H - i))
        Smax = max(i * W, w * (H - i), (W - w) * (H - i))
        ans = min(ans, Smax-Smin)
    return min(H,ans)

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

Solution (Python):

H,W = map(int,input().split())
ans = H*W
for i in range(1,H):
    S0 = i*W
    h = (H-i)//2
    S1 = [S0,h*W,(H-i-h)*W]
    ans = min(ans,max(S1)-min(S1))
    h = H-i
    w = W//2
    S1 = [S0,h*w,h*(W-w)]
    ans = min(ans,max(S1)-min(S1))
for j in range(1,W):
    S0 = j*H
    w = (W-j)//2
    S1 = [S0,H*w,H*(W-j-w)]
    ans = min(ans,max(S1)-min(S1))
    w = W-j
    h = H//2
    S1 = [S0,h*w,(H-h)*w]
    ans = min(ans,max(S1)-min(S1))
print(ans)

Solution (Python):
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)

"""

def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
x,y = ips()

if x%3==0 or y%3 == 0:
    print(0)
    exit(0)
ans = min(x,y)
base = [y,y//2,y//2]
if y%2:base[2]+=1
total = [0,x*base[1],x*base[2]]
ans = min(ans,max(total)-min(total))
for i in range(x):
    total[0]+=y
    total[1]-=base[1]
    total[2]-=base[2]
    ans = min(ans,max(total)-min(total))

base = [x,x//2,x//2]
if x%2:base[2]+=1
total = [0,y*base[1],y*base[2]]
ans = min(ans,max(total)-min(total))
for i in range(y):
    total[0]+=x
    total[1]-=base[1]
    total[2]-=base[2]
    ans = min(ans,max(total)-min(total))
print(ans)

Solution (Python):
def f(W, H):
    res = float('INF')
    w = W // 2
    for h in range(1, H):
        res = min(res, max(h * W, (H - h) * w,  (H - h) * (W - w)
                           ) - min(h * W, (H - h) * w,  (H - h) * (W - w)))
    return min(H, res)


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


main()

Solution (Python):



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

def solve(H,W):
    mid = math.floor(W/2)
    ans = float('inf')
    for i in range(1,H):
        Smin = min(i*W,mid*(H-i),(W-mid)*(H-i))
        Smax = max(i*W,mid*(H-i),(W-mid)*(H-i))
        ans = min(ans,Smax-Smin)
    return min(ans,H)
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    print(min(solve(h,w),solve(w,h)))

Solution (Python):



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

def solve(H,W):
    mid = math.floor(W/2)
    ans = float('inf')
    for i in range(1,H):
        Smin = min(i*W,mid*(H-i),(W-mid)*(H-i))
        Smax = max(i*W,mid*(H-i),(W-mid)*(H-i))
        ans = min(ans,Smax-Smin)
    return min(ans,H)
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    print(min(solve(h,w),solve(w,h)))

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

ans_wsplit = [max(abs((i * w) - (((h - i) + 1) // 2) * w), abs((i * w) - ((h - i) // 2) * w)) for i in range(1, h)]
ans_hsplit = [max(abs((i * h) - (((w - i) + 1) // 2) * h), abs((i * h) - ((w - i) // 2) * h)) for i in range(1, w)]

ans_wline = [max(abs((i * w) - ((w + 1) // 2) * (h - i)), abs((i * w) - (w // 2) * (h - i))) for i in range(1, h)]
ans_hline = [max(abs((i * h) - ((h + 1) // 2) * (w - i)), abs((i * h) - (h // 2) * (w - i))) for i in range(1, w)]

print(min(ans_wsplit + ans_hsplit + ans_wline + ans_hline))

Solution (Python):
import math
def calc(h, w):
    case1 = (0 if h % 3 == 0 else h)
    mid = math.floor(w/2)
    case2 = float("inf")
    for i in range(1, h):
        Smin = min(i*w, mid*(h-i), (w-mid)*(h-i))
        Smax = max(i*w, mid*(h-i), (w-mid)*(h-i))
        case2 = min(Smax-Smin, case2)
    return min(case1, case2)
H, W = map(int, input().split())
print(min(calc(H,W), calc(W,H)))

Solution (Python):

def main():
    high, width = map(int, input().split())
    ans = high * width

    for i in range(1, high):
        a = i * width

        b1 = (high - i) * (width // 2)
        c1 = high * width - a - b1

        b2 = ((high - i) // 2) * width
        c2 = high * width - a - b2

        kari = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2))

        ans = min(ans, kari)

    for i in range(1, width):
        a = i * high

        b1 = (width - i) * (high // 2)
        c1 = high * width - a - b1

        b2 = ((width - i) // 2) * high
        c2 = high * width - a - b2

        kari = min(max(a, b1, c1) - min(a, b1, c1), max(a, b2, c2) - min(a, b2, c2))

        ans = min(ans, kari)


    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
H,W = map(int,input().split())
MIN = 10**11
S1 = 0
S2 =  0
S3 = 0
if H%3 == 0 or W %3==0:
    print(0)
else:
    for i in range(1,H):
        S1 = i * W
        S2 = (H-i)*(W//2)
        S3 = (H-i)*(W - W//2)
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    for i in range(1,W):
        S1 = i * H
        S2 = (W-i)*(H//2)
        S3 = (W-i)*(H - H//2)
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    for i in range(1,H):
        S1 = i*W
        S2 = ((H-i)//2)*W
        S3 = H*W - S1-S2
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
    
    for i in range(1,H):
        S1 = i*H
        S2 = ((W-i)//2) *H
        S3 = H*W - S1-S2
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    print(MIN)

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

for i in range(1,h):
    if w%2==0:
        l=[i*w,(h-i)*(w//2),(h-i)*(w//2)]
    else:
        l=[i*w,(h-i)*(w//2),(h-i)*(w//2+1)]

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

    if (w-i)%2==0:
        ll=[i*h,((w-i)//2)*h,((w-i)//2)*h]
    else:
        ll=[i*h,((w-i)//2)*h,((w-i)//2+1)*h]
    p=min(max(l)-min(l),max(ll)-min(ll))
    ans=min(ans,p)
print(ans)

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

Solution (Python):
import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

H, W = list(map(int, sys.stdin.readline().split()))


ans = IINF

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

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

w = 1
while w < W:
    a = (W - w) * H
    b = w * (H // 2)
    c = w * H - b
    ans = min(ans, max(a, b, c) - min(a, b, c))
    w += 1

h = 1
while h < H:
    a = (H - h) * W
    b = h * (W // 2)
    c = h * W - b
    ans = min(ans, max(a, b, c) - min(a, b, c))
    h += 1
print(ans)

Solution (Python):
H,W=map(int,input().split())
ans=10**9
if H%3==0 or W%3==0:
    ans=0
else:
    if ans>H or ans>W:
        ans=min(H,W)
for i in range(H):
    a=(i+1)*W
    if W%2==0:
        b=(H-(i+1))*(W//2)
        c=(H-(i+1))*(W//2)
    else:
        b=(H-(i+1))*(W//2)
        c=(H-(i+1))*((W//2)+1)
    if ans>(max(a,b,c)-min(a,b,c)):
        ans=(max(a,b,c)-min(a,b,c))
W,H=H,W
for i in range(H):
    a=(i+1)*W
    if W%2==0:
        b=(H-(i+1))*(W//2)
        c=(H-(i+1))*(W//2)
    else:
        b=(H-(i+1))*(W//2)
        c=(H-(i+1))*((W//2)+1)
    if ans>(max(a,b,c)-min(a,b,c)):
        ans=(max(a,b,c)-min(a,b,c))
print(ans)

Solution (Python):
h,w=map(int,input().split())
ans=50000
for a,b in [(w,h),(h,w)]:
    for i in range(b):
        for c,d in [(a,b-i), (b-i,a)]:
            l=[i*a,c*(d//2),c*(d-d//2)]
            ans=min(ans,max(l)-min(l))
print(ans)

Solution (Python):

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


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

else:

    ans = float("inf")
    A = 0
    B = 0
    C = 0

    

    

    A = (W//3) * H

    B = (W - (W//3)) * (H // 2)
    C = W*H - (A+B)

    ans = min( ans , max(A,B,C)-min(A,B,C) )

    B = ((W - (W//3)) // 2 ) * H
    C = W*H - (A+B)

    ans = min( ans , max(A,B,C)-min(A,B,C) )

    A = (W//3 + 1) * H
    B = (W - (W//3 + 1)) * (H//2)
    C = (W*H) - (A+B)

    ans = min( ans , max(A,B,C)-min(A,B,C) )

    newH = W
    newW = H

    H = newH
    W = newW

    A = (W//3) * H

    B = (W - (W//3)) * (H // 2)
    C = W*H - (A+B)

    ans = min( ans , max(A,B,C)-min(A,B,C) )

    B = ((W - (W//3)) // 2 ) * H
    C = W*H - (A+B)

    ans = min( ans , max(A,B,C)-min(A,B,C) )

    A = (W//3 + 1) * H
    B = (W - (W//3 + 1)) * (H//2)
    C = (W*H) - (A+B)

    ans = min( ans , max(A,B,C)-min(A,B,C) )


    print (ans)

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

for i in range(1,h-1):
    nokori=h-i
    two=nokori//2
    three=nokori-two
    if two==0 or three==0:
        continue
    ans=min(ans,max(i*w, two*w, three*w)-min(i*w, two*w, three*w))

for i in range(1,h):
    nokori=h-i
    two=w//2
    three=w-two
    if two==0 or three==0:
        continue
    ans=min(ans,max(i*w, nokori*two, nokori*three)-min(i*w, nokori*two, nokori*three))

tmp=w
w=h
h=tmp



for i in range(1,h-1):
    nokori=h-i
    two=nokori//2
    three=nokori-two
    if two==0 or three==0:
        continue
    ans=min(ans,max(i*w, two*w, three*w)-min(i*w, two*w, three*w))

for i in range(1,h):
    nokori=h-i
    two=w//2
    three=w-two
    if two==0 or three==0:
        continue
    
    
    
    
    ans=min(ans,max(i*w, nokori*two, nokori*three)-min(i*w, nokori*two, nokori*three))

print(ans)

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

def sub(a,k,b,l):
    list = sorted([a*l,(k-a)*b,(k-a)*(l-b)])
    return list[2]-list[0]

if H*W % 3 == 0:
    print(0)
else:
    a = W//3
    b = H//2
    g1 = 99999999999999999999999999
    for i in [a,a+1]:
        if H%2==0:
            g1 = min(g1,sub(i,W,H//2,H))
        for j in [b,b+1]:
            g1 = min(g1,sub(i,W,j,H))

    c = H//3
    d = W//2
    g2 = 99999999999999999999999999
    for i in [c,c+1]:
        if W%2==0:
            g2 = min(g2,sub(i,H,W//2,W))
        for j in [b,b+1]:
            g2 = min(g2,sub(i,H,j,W))
    print(min(g1,g2,H,W))

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

if h %3 == 0 or w%3 == 0:
    print(0)
elif h == 1 or w == 1:
    print(1)
else:
    s1 = round(h / 3)*w
    h1 = h - round(h/3)
    if w % 2 == 0 or h1 % 2 == 0:
        s2 = h1*w//2
        s3 = s2
    else:
        s2 = max(w, h1)//2 * min(w, h1)
        s3 = h1*w-s2
    ans = max([s1, s2, s3])-min([s1, s2, s3])
    s1 = round(w / 3)*h
    w1 = w - round(w/3)
    if w1 % 2 == 0 or h % 2 == 0:
        s2 = h*w1//2
        s3 = s2
    else:
        s2 = max(w1, h)//2 * min(w1, h)
        s3 = h*w1-s2
    ans = min(max([s1, s2, s3])-min([s1, s2, s3]), ans)
    print(ans)

Solution (Python):
H,W=map(int,input().split())
if H*W%3==0:print(0)
else:
    a,b,c=(W//3)*H,(H//2)*(W-W//3),(H-H//2)*(W-W//3)
    H1=max(a,b,c)-min(a,b,c)
    d,e,f=((W//3)+1)*H,(H//2)*(W-(W//3)-1),(H-H//2)*(W-(W//3)-1)
    H2=max(d,e,f)-min(d,e,f)
    p,q,r=(H//3)*W,(W//2)*(H-H//3),(W-W//2)*(H-H//3)
    W1=max(p,q,r)-min(p,q,r)
    s,t,u=((H//3)+1)*W,(W//2)*(H-(H//3)-1),(W-W//2)*(H-(H//3)-1)
    W2=max(s,t,u)-min(s,t,u)
    print(min(H1,H2,W1,W2,H,W))

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

tmp = 999999999999999


def calc_area(H, W):
    piece_1 = i * W
    piece_2 = int(W / 2) * (H - i)
    piece_3 = int(W / 2 + 0.9) * (H - i)
    holi = max(piece_1, piece_2, piece_3) - min(piece_1, piece_2, piece_3)

    piece_2 = W * int((H - i) / 2)
    piece_3 = W * int((H - i) / 2 + 0.9)
    ver = max(piece_1, piece_2, piece_3) - min(piece_1, piece_2, piece_3)
    return min(tmp, holi, ver)


for i in range(0, H + 1):
    holi = calc_area(H, W)
    ver =  calc_area(W, H)
    tmp = min(tmp, holi, ver)


for i in range(0, W + 1):
    holi = calc_area(H, W)
    ver =  calc_area(W, H)
    tmp = min(tmp, holi, ver)

print(tmp)

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

def calc(h, w):
    h1 = round(h / 3)
    h2 = (h - h1) // 2
    h3 = h - h1 - h2
    hr = h2 + h3
    w1 = w // 2
    w2 = w - w1

    harr = [h1 * w, h2 * w, h3 * w]
    warr = [h1 * w, hr * w1, hr * w2]
    a = max(harr) - min(harr) if h2 else float('inf')
    b = max(warr) - min(warr) if w1 else float('inf')

    return min(a, b)

def main():
    print(min(calc(H, W), calc(W, H)))
    
if __name__ == '__main__':
    main()

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

def ep(H,W,h):
    S1=h*W
    S2=(H-h)*(W//2)
    S3=(H-h)*(W-W//2)
    c1=max(S1,S2,S3)-min(S1,S2,S3)

    T1=h*W
    T2=((H-h)//2)*W
    T3=(H-h-((H-h)//2))*W
    c2=max(T1,T2,T3)-min(T1,T2,T3)

    return min(ans,c1,c2)




if H!=1:
    for h in range(1,H):
        ans=ep(H,W,h)

H,W=W,H
if H!=1:
    for h in range(1,H):
        ans=ep(H,W,h)

print(ans)