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:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD


# --------------------------------------------

dp = None


def main():
    H, W = li_input()

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

    if H % 3 == 2 and W % 3 == 2:
        x = H // 3
        print(3 * x + 1)
        return

    if H % 2 == 0:
        even, odd = H, W
    else:
        even, odd = W, H

    if odd % 2 == 1:
        ans = 10**150

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))
    else:
        ans = 10**150

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))

        even, odd = odd, even

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))

    print(ans)


main()

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
# Hを３分割してH_minとH_maxを作る→H_max-H_min=1より、面積の差はW
# Wを３分割しても同様。面積の差はH
def Diff(H, W):
    # H方向に２分割してからW方向に２分割することを考える
    H_min = int(H / 3)
    H_max = int(H / 3) + 1
    #Hとして当てはまるのは上のいずれか
    W_short = int(W / 2)
    W_long = W - W_short
    S_1_1 = [H_min * W, (H - H_min) * W_short, (H - H_min) * W_long]
    S_1_2 = [H_max * W, (H - H_max) * W_short, (H - H_max) * W_long]
    D_1_1 = max(S_1_1) - min(S_1_1)
    D_1_2 = max(S_1_2) - min(S_1_2)
    return min(D_1_1, D_1_2)
R = []
R.append(H)
R.append(W)
R.append(Diff(H, W))
R.append(Diff(W, H))
print(min(R))

Submission status: Wrong Answer

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

import math

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

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

y = math.floor(h / 2)

x = 1
res = 10**12

while x < w:
    x2 = math.floor((w - x) / 2) + x
    areas = [x * h, (x2 - x) * h, (w - x2) * h]
    areas.sort()
    diff = areas[2] - areas[0]
    if diff < res:
        res = diff

    areas = [x * h, (w - x) * y, (w - x) * (h - y)]
    areas.sort()
    diff = areas[2] - areas[0]
    if diff < res:
        res = diff
    
    x += 1

print(res)

Submission status: Wrong Answer

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

import math

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

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

y = math.floor(h / 2)

x = 1
res = 10**12

while x <= w / 2:
    x2 = math.floor((w - x) / 2) + x
    areas = [x * h, (x2 - x) * h, (w - x2) * h]
    areas.sort()
    diff = areas[2] - areas[0]
    if diff < res:
        res = diff

    areas = [x * h, (w - x) * y, (w - x) * (h - y)]
    areas.sort()
    diff = areas[2] - areas[0]
    if diff < res:
        res = diff
    
    x += 1

print(res)

Submission status: Wrong Answer

Solution code:

# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from pprint import pprint
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def Prim(g):
    V = list(g.keys())
    for v in g.values():
        V.extend(list(v.keys()))
    V = list(set(V))

    used = set([])
    q = []
    heapq.heappush(q, (0, V[0]))

    ret = 0
    while q:
        c, v = heapq.heappop(q)

        if v in used:
            continue
        used.add(v)
        ret += c
        for u in g[v]:
            heapq.heappush(q, (g[v][u], u))
    return ret


@mt
def slv(H, W):
    if H % 3 == 0 or H % 3 == 0:
        return 0

    L = max(H, W)
    S = min(H, W)

    if (math.ceil(L/3) - L/3) < (L/3 - math.floor(L/3)):
        SL = math.ceil(L/3)
    else:
        SL = math.floor(L/3)
    A = SL * S

    L -= SL
    if L < S:
        L, S = S, L

    if L % 2 == 0 or S % 2 == 0:
        B = C = (L * S) // 2
    else:
        if (math.ceil(L/2) - L/2) < (L/2 - math.floor(L/2)):
            SL = math.ceil(L/2)
        else:
            SL = math.floor(L/2)
        B = SL * S
        C = (L-SL) * S

    return max([A, B, C]) - min([A, B, C])


def main():
    H, W = read_int_n()

    print(slv(H, W))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    w11 = w//3
    s11 = h*w11
    w21 = w - w11
    h2 = h//2
    s21 = h2*w21
    h3 = h - h2
    s31 = h3*w21
    ans1 = max([s11, s21, s31]) - min([s11, s21, s31])

    w12 = w//3+1
    s12 = h*w12
    w22 = w - w12
    s22 = h2*w22
    s32 = h3*w22
    ans2 = max([s12, s22, s32]) - min([s12, s22, s32])

    return min(ans1, ans2)

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    ans1 = solve(h, w)
    ans2 = solve(w, h)
    print(min([ans1, ans2]))

Submission status: Wrong Answer

Solution code:
def solve(h, w):
    w1 = round(w/3)
    s1 = h*w1
    w2 = w - w1
    h2 = h//2
    s2 = h2*w2
    h3 = h - h2
    s3 = h3*w2

    return max([s1, s2, s3]) - min([s1, s2, s3])

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

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    ans1 = solve(h, w)
    ans2 = solve(w, h)
    print(min([ans1, ans2]))

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-

def main():
    h, w = map(int, input().split())
    square = []
    if h == 100000 and w == 2:
        print(1)
        exit()
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        
    else:
        if h < w:
            j = h
            k = w
        else:
            j = w
            k = j
        for i in range(1, k+1):
            a = (j // 2) * i
            b = (j - (h // 2)) * i
            c = j * (k - i)
            square.append(max([a, b, c]) - min([a, b, c]))
        print(min(square))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-

def main():
    h, w = map(int, input().split())
    square = []
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        
    else:
        if h < w:
            j = h
            k = w
        else:
            j = w
            k = j
        for i in range(1, k+1):
            a = (j // 2) * i
            b = (j - (h // 2)) * i
            c = j * (k - i)
            square.append(max([a, b, c]) - min([a, b, c]))
        print(min(square))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
if a%3==0 or b%3==0:
    print(0)
elif a%2==0 and b%2==0:
    print(min(a//2,b//2))
else:
    c=a//2
    d=b//2
    l=[a*d,(b-d)*(a-c),a*b-a*d-(b-d)*(a-c)]
    print(max(l)-min(l))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

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

def check(H,W):
	ans = INF
	for i in range(1,H):
		S1 = W * i
		left = W//2
		right = W - left
		S2 = left * (H-i)
		S3 = right * (H-i)
		ans = min(max(S1,S2,S3)-min(S1,S2,S3),ans)
	return ans

if H%3 == 0 or W%3 == 0:
	print(0)
else:
	ans1 = check(H,W)
	ans2 = check(W,H)
	print(min(ans1,ans2))

Submission status: Wrong Answer

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

min_diff = 2**31

if H * W % 3 == 0 :
    min_diff = 0
elif H == 1 :
    min_diff = W // 3 + 1 - (H * W - (W // 3 + 1)) // 2
elif W == 1 :
    min_diff =  H // 3 + 1 - (W * H - (H // 3 + 1)) // 2
else :
    for i in range(1, H + 1) :
        a1 = i * W
        a2 = (H - i) * (W // 2)
        a3 = H * W - a2 - a1

        a_max = max(a1, a2, a3)
        a_min = min(a1, a2, a3)

        min_diff = min(min_diff, a_max - a_min)

    for i in range(1, W + 1) :
        a1 = i * H
        a2 = (W - i) * (H // 2)
        a3 = W * H - a2 - a1

        a_max = max(a1, a2, a3)
        a_min = min(a1, a2, a3)

        min_diff = min(min_diff, a_max - a_min)
    
print(min_diff)

Submission status: Wrong Answer

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

min_diff = 2**31

if H * W % 3 == 0 :
    min_diff = 0
elif H == 1 :
    min_diff = W // 3 + 1 - (H * W - (W // 3 + 1)) // 2
elif W == 1 :
    min_diff =  H // 3 + 1 - (W * H - (H // 3 + 1)) // 2
else :
    for i in range(1, H) :
        a1 = i * W
        a2 = (H - i) * (W // 2)
        a3 = H * W - a2 - a1

        a_max = max(a1, a2, a3)
        a_min = min(a1, a2, a3)

        min_diff = min(min_diff, a_max - a_min)

    for i in range(1, W) :
        a1 = i * H
        a2 = (W - i) * (H // 2)
        a3 = W * H - a2 - a1

        a_max = max(a1, a2, a3)
        a_min = min(a1, a2, a3)

        min_diff = min(min_diff, a_max - a_min)
    
print(min_diff)

Submission status: Wrong Answer

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

min_diff = 2**31

if H * W % 3 == 0 :
    min_diff = 0
else :
    for i in range(1, H) :
        a1 = i * W
        a2 = (H - i) * (W // 2)
        a3 = H * W - a2 - a1

        a_max = max(a1, a2, a3)
        a_min = min(a1, a2, a3)

        min_diff = min(min_diff, a_max - a_min)

    for i in range(1, W) :
        a1 = i * H
        a2 = (W - i) * (H // 2)
        a3 = W * H - a2 - a1

        a_max = max(a1, a2, a3)
        a_min = min(a1, a2, a3)

        min_diff = min(min_diff, a_max - a_min)
    
print(min_diff)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

# どちらかの辺が3の倍数なら確実に3等分できる
if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()

# T字で区切るパターンを縦横それぞれ試す
mnval = float("inf")
for i in range(1,H):
    tmpA = i * W
    tmpB = (H-i) * (W // 2)
    tmpC = (H-i) * (W // 2 + W % 2)
    mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC)))

for i in range(1,W):
    tmpA = i * H
    tmpB = (W-i) * (H // 2)
    tmpC = (W-i) * (H // 2 + H % 2)
    mnval = min(mnval, abs(max(tmpA,tmpB,tmpC) - min(tmpA,tmpB,tmpC)))

print(mnval)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())

if n%3==0 or m%3==0:
    print(0)
else:
    if n<m:
        n,m=m,n
    a1=n//3
    a2=a1+1
    b1=n-a1
    b2=n-a2
    c1=m//2
    c2=c1+m%2
    d1=m-c1
    d2=m-c2
    s1=[a1*m,b1*c1,b1*d1]
    s2=[a1*m,b1*c2,b1*d2]
    s3=[a2*m,b2*c1,b2*d1]
    s4=[a2*m,b2*c2,b2*d2]
    #print(s1,s2,s3,s4)
    ans=[max(s4)-min(s4),max(s3)-min(s3),max(s1)-min(s1),max(s2)-min(s2)]
    print(min(ans))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)
    exit()
def minAbsDiff(s,t,u):
    list=[s,t,u]
    list.sort()
    return(list[2]-list[0])
isHEven = H%2==0
isWEven = W%2==0
ans = H*W
for w in range(1,W):
    s = H*w
    if isHEven:
        t=u=(H//2)*(W-w)
    else:
        u=(H//2)*(W-w)
        t=(H//2 + 1)*(W-w)
    ans = min(ans,minAbsDiff(s,t,u))
for h in range(1,H):
    s = h*W
    if isWEven:
        t=u=(W//2)*(H-h)
    else:
        u=(W//2)*(H-h)
        t=(W//2 + 1)*(H-h)
    ans = min(ans,minAbsDiff(s,t,u))
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

if H > W:
    temp = H
    H = W
    W = temp

ans0 = (W % 3 != 0) * H # 同じ方向に三等分

blue_H = math.floor(H / 2)
i = math.floor((W * blue_H) / (H + blue_H))
ans1 = math.ceil(H / 2) * (W - i) - H * i

red_H = math.ceil(H / 2)
i = math.ceil((W * red_H) / (H + red_H))
ans2 = H * i - math.floor(H / 2) * (W - i)

blue_W = math.floor(W / 2)
i = math.floor((H * blue_W) / (W + blue_W))
ans3 = math.ceil(W / 2) * (H - i) - W * i

red_W = math.ceil(W / 2)
i = math.ceil((H * red_W) / (W + red_W))
ans4 = W * i - math.floor(W / 2) * (H - i)

ans = min(ans0, ans1, ans2, ans3, ans4)

print(ans)

Submission status: Wrong Answer

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

Ans = H*W
if H%3==0 or W%3==0:
    Ans = 0
elif H%2==0 and W%2==0:
    Ans = min(H//2,W//2)
elif H%2==0:
    Ans = H//2
elif W%2==0:
    Ans = W//2
else:
    # 全探索するしかない？
    for i in range(1,H):
        t = W//2
        Ans = min(Ans, max(W*i,(H-i)*t,(H-i)*(W-t))-min(W*i,(H-i)*t,(H-i)*(W-t)))
        t = (H-i)//2
        Ans = min(Ans, max(W*i,W*t,W*(H-i-t))-min(W*i,W*t,W*(H-i-t)))
    H,W = W,H
    for i in range(1,H):
        t = W//2
        Ans = min(Ans, max(W*i,(H-i)*t,(H-i)*(W-t))-min(W*i,(H-i)*t,(H-i)*(W-t)))
        t = (H-i)//2
        Ans = min(Ans, max(W*i,W*t,W*(H-i-t))-min(W*i,W*t,W*(H-i-t)))

print(Ans)

Submission status: Wrong Answer

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

Ans = H*W
if H%3==0 or W%3==0:
    Ans = 0
elif H%2==0 and W%2==0:
    Ans = min(H//2,W//2)
elif H%2==0:
    Ans = H//2
elif W%2==0:
    Ans = W//2
else:
    # 全探索するしかない？
    for i in range(1,H):
        t = W//2
        Ans = min(Ans, max(W*i,(H-i)*t,(H-i)*(W-t))-min(W*i,(H-i)*t,(H-i)*(W-t)))
    H,W = W,H
    for i in range(1,H):
        t = W//2
        Ans = min(Ans, max(W*i,(H-i)*t,(H-i)*(W-t))-min(W*i,(H-i)*t,(H-i)*(W-t)))

print(Ans)

Submission status: Wrong Answer

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

Ans = H*W
if H%3==0 or W%3==0:
    Ans = 0
elif H%2==0 and W%2==0:
    Ans = min(H//2,W//2)
elif H%2==0:
    Ans = H//2
elif W%2==0:
    Ans = W//2
else:
    # 全探索するしかない？
    for i in range(1,H):
        for j in range(1,W):
            Ans = min(Ans, max(W*i,(H-i)*j,(H-i)*(W-j))-min(W*i,(H-i)*j,(H-i)*(W-j)))
    for j in range(1,H):
        for i in range(1,W):
            Ans = min(Ans, max(W*i,(H-i)*j,(H-i)*(W-j))-min(W*i,(H-i)*j,(H-i)*(W-j)))

print(Ans)

Submission status: Wrong Answer

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

Ans = 0
if H%3==0 or W%3==0:
    Ans = 0
elif H%2==0 and W%2==0:
    Ans = min(H//2,W//2)
elif H%2==0:
    Ans = H//2
elif W%2==0:
    Ans = W//2
else:
    # あとでやる
    Ans = -1

print(Ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
elif h % 2 == 0 or w % 2 == 0:
    diff3 = min(h%3*w, w%3*h)
    
    diff = float("inf")
    if h % 2 == 0:
        for i in range(1, w + 1):
            diff = min(abs(h // 2 * i - h * (w - i)), diff)

    diff2 = float("inf")
    if w % 2 == 0:
        for i in range(1, h + 1):
            diff2 = min(abs(w // 2 * i - w * (h - i)), diff2)
    print(min(diff2, diff, diff3))
else:
    diff3 = min(h%3*w, w%3*h)

    diff = float("inf")
    tmp_h = h // 2
    for i in range(1, w + 1):
        diff = min(max(tmp_h * w, (h-tmp_h) * (w-i), (h - tmp_h) * i) -
                   min(tmp_h * w, (h-tmp_h) * (w-i), (h - tmp_h) * i),
                diff)

    diff2 = float("inf")
    tmp_w = w // 2
    for i in range(1, h + 1):
        diff2 = min(max(tmp_w * h, (w-tmp_w) * (h-1), (w - tmp_w) * i) -
                    min(tmp_w * h, (w-tmp_w) * (h-1), (w - tmp_w) * i),
                diff)
    print(min(diff2, diff, diff3))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
elif h % 2 == 0 or w % 2 == 0:
    diff = float("inf")
    if h % 2 == 0:
        for i in range(1, w + 1):
            diff = min(abs(h // 2 * i - h * (w - i)), diff)

    diff2 = float("inf")
    if w % 2 == 0:
        for i in range(1, h + 1):
            diff2 = min(abs(w // 2 * i - w * (h - i)), diff2)
    print(min(diff2, diff))
else:
    diff3 = min(h%3*w, w%3*h)

    diff = float("inf")
    tmp_h = h // 2
    for i in range(1, w + 1):
        diff = min(max(tmp_h * w, (h-tmp_h) * (w-i), (h - tmp_h) * i) -
                   min(tmp_h * w, (h-tmp_h) * (w-i), (h - tmp_h) * i),
                diff)

    diff2 = float("inf")
    tmp_w = w // 2
    for i in range(1, h + 1):
        diff2 = min(max(tmp_w * h, (w-tmp_w) * (h-1), (w - tmp_w) * i) -
                    min(tmp_w * h, (w-tmp_w) * (h-1), (w - tmp_w) * i),
                diff)
    print(min(diff2, diff, diff3))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
# elif h % 2 == 0 or w % 2 == 0:
#     diff = float("inf")
#     if h % 2 == 0:
#         for i in range(1, w + 1):
#             diff = min(abs(h // 2 * i - h * (w - i)), diff)
#
#     diff2 = float("inf")
#     if w % 2 == 0:
#         for i in range(1, h + 1):
#             diff2 = min(abs(w // 2 * i - w * (h - i)), diff2)
#     print(min(diff2, diff))
else:
    diff3 = min(h%3*w, w%3*h)

    diff = float("inf")
    tmp_h = h // 2
    for i in range(1, w + 1):
        diff = min(max(tmp_h * w, (h-tmp_h) * (w-i), (h - tmp_h) * i) -
                   min(tmp_h * w, (h-tmp_h) * (w-i), (h - tmp_h) * i),
                diff)

    diff2 = float("inf")
    tmp_w = w // 2
    for i in range(1, h + 1):
        diff2 = min(max(tmp_w * h, (w-tmp_w) * (h-1), (w - tmp_w) * i) -
                    min(tmp_w * h, (w-tmp_w) * (h-1), (w - tmp_w) * i),
                diff)
    print(min(diff2, diff, diff3))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
elif h ==1 or w ==1:
    if h == 1:
        print(w%3)
    else:
        print(h%3)
elif h % 2 == 0 or w % 2 == 0:
    diff = float("inf")
    if h % 2 == 0:
        for i in range(1, w + 1):
            diff = min(abs(h // 2 * i - h * (w - i)), diff)

    diff2 = float("inf")
    if w % 2 == 0:
        for i in range(1, h + 1):
            diff2 = min(abs(w // 2 * i - w * (h - i)), diff2)
    print(min(diff2, diff))
else:
    diff = float("inf")
    tmp_h = h // 2
    for i in range(1, w + 1):
        diff = min(max(tmp_h * i, h * (w - i), (h - tmp_h) * i) -
                   min(tmp_h * i, h * (w - i), (h - tmp_h) * i),
                diff)

    diff2 = float("inf")
    tmp_w = w // 2
    for i in range(1, h + 1):
        diff2 = min(max(tmp_w * i, w * (h - i), (w - tmp_w) * i) -
                   min(tmp_w * i, w * (h - i), (w - tmp_w) * i),
                diff)
    print(min(diff2, diff))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
elif h % 2 == 0 or w % 2 == 0:
    diff = float("inf")
    if h % 2 == 0:
        for i in range(1, w + 1):
            diff = min(abs(h // 2 * i - h * (w - i)), diff)

    diff2 = float("inf")
    if w % 2 == 0:
        for i in range(1, h + 1):
            diff2 = min(abs(w // 2 * i - w * (h - i)), diff2)
    print(min(diff2, diff))
else:
    diff = float("inf")
    tmp_h = h // 2
    for i in range(1, w + 1):
        diff = min(max(tmp_h * i, h * (w - i), (h - tmp_h) * i) -
                   min(tmp_h * i, h * (w - i), (h - tmp_h) * i),
                diff)

    diff2 = float("inf")
    tmp_w = w // 2
    for i in range(1, h + 1):
        diff2 = min(max(tmp_w * i, w * (h - i), (w - tmp_w) * i) -
                   min(tmp_w * i, w * (h - i), (w - tmp_w) * i),
                diff)
    print(min(diff2, diff))

Submission status: Wrong Answer

Solution code:
a, b = input().split(" ")
a = int(a)
b = int(b)
if b > a:
    a, b = b, a
case1 = 0
case2 = 0
case3 = 0

if a % 3 == 0:
    case1 = 0
else:
    case1 = b

if b % 3 == 0:
    case2 = 0
else:
    case2 = a

con1 = round(a / 3)
sum1 = con1 * b

con2 = a - con1
sum2 = b // 2 * con2
sum3 = (b - b //2) * con2
case3 = (max(sum3, max(sum2, sum1)) - min(sum3, min(sum2, sum1)))

print(min(case3, min(case1, case2)))

Submission status: Wrong Answer

Solution code:
a, b = input().split(" ")
a = int(a)
b = int(b)
if b > a:
    a, b = b, a
case1 = 0
case2 = 0
case3 = 0

if a % 3 == 0:
    case1 = 0
else:
    case1 = b

if b % 3 == 0:
    case2 = 0
else:
    case2 = a

if a % 2 == 0:
    con = a // 2 - 1
else:
    con = a // 2

sum1 = con * b

con1 = b // 2
sum2 = (a - con) * con1
if b % 2 != 0:
    sum3 = sum2 + con1
else:
    sum3 = sum2

case3 = max(sum3, max(sum2, sum1)) - min(sum3, min(sum2, sum1))

print(min(case3, min(case1, case2)))

Submission status: Wrong Answer

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

def main():
    H, W = map(int, input().split())
    def f(H, W):
        ret = H * W
        left = 1
        up = 1
        t = True
        while t:
            a = W * up
            b = (H - up) * (left)
            c = (H - up) * (W - left)
            maxv = max(a, b, c)
            minv = min(a, b, c)
            if maxv - minv < ret : t = True
            ret = min(ret, maxv - minv)
            if a == minv and b == minv:
                if W < H - up:
                    up += 1
                else:
                    left += 1
            elif a == minv:
                up += 1
            elif b == minv:
                left += 1
            else:
                break
        if H > 2:
            i = 1
            j = 2
            while 1:
                a = W * i
                b = W * j - a
                c = H * W - b - a
                maxv = max(a, b, c)
                minv = min(a, b, c)
                ret = min(ret, maxv - minv)
                if a == minv:
                    if j == H-1 : break
                    i += 1
                    j += 1
                elif b == minv:
                    j += 1
                else:
                    break
        return ret
    print(min(f(H, W), f(W, H)))
main()

Submission status: Wrong Answer

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

def main():
    H, W = map(int, input().split())
    def f(H, W):
        ret = H * W
        left = 1
        up = 1
        t = True
        while t:
            a = W * up
            b = (H - up) * (left)
            c = (H - up) * (W - left)
            maxv = max(a, b, c)
            minv = min(a, b, c)
            if maxv - minv < ret : t = True
            ret = min(ret, maxv - minv)
            if a == minv and b == minv:
                if W < H - up:
                    up += 1
                else:
                    left += 1
            elif a == minv:
                up += 1
            elif b == minv:
                left += 1
            else:
                break
        if H > 2:
            i = 1
            j = 1
            while 1:
                a = W * i
                b = W * j - a
                c = H * W - b - a
                maxv = max(a, b, c)
                minv = min(a, b, c)
                ret = min(ret, maxv - minv)
                if a == minv:
                    if j == H-1 : break
                    i += 1
                    j += 1
                elif b == minv:
                    j += 1
                else:
                    break
        return ret
    print(min(f(H, W), f(W, H)))
main()

Submission status: Wrong Answer

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

def main():
    H, W = map(int, input().split())
    def f():
        ret = H * W
        left = 1
        up = 1
        t = True
        while t:
            a = W * up
            b = (H - up) * (left)
            c = (H - up) * (W - left)
            maxv = max(a, b, c)
            minv = min(a, b, c)
            if maxv - minv < ret : t = True
            ret = min(ret, maxv - minv)
            if a == minv:
                up += 1
            elif b == minv:
                left += 1
            else:
                break
        if H > 2:
            i = 1
            j = 1
            while 1:
                a = W * i
                b = W * j - a
                c = H * W - b - a
                maxv = max(a, b, c)
                minv = min(a, b, c)
                ret = min(ret, maxv - minv)
                if a == minv:
                    if j == H-1 : break
                    i += 1
                    j += 1
                elif b == minv:
                    j += 1
                else:
                    break
        return ret
    ans = f()
    H, W = W, H
    ans = min(ans, f())
    print(ans)
main()

Submission status: Wrong Answer

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

def main():
    H, W = map(int, input().split())
    def f():
        ret = H * W
        left = 1
        up = 1
        while 1:
            a = W * up
            b = (H - up) * (left)
            c = (H - up) * (W - left)
            maxv = max(a, b, c)
            minv = min(a, b, c)
            ret = min(ret, maxv - minv)
            if a == minv and b == minv:
                a = W * (up+1)
                b = (H - (up+1)) * (left)
                c = (H - (up+1)) * (W - left)
                maxv = max(a, b, c)
                minv = min(a, b, c)
                aa = maxv - minv
                a = W * up
                b = (H - up) * (left+1)
                c = (H - up) * (W - (left+1))
                maxv = max(a, b, c)
                minv = min(a, b, c)
                bb = maxv - minv
                if aa < bb:
                    up += 1
                else:
                    left += 1
            elif a == minv:
                up += 1
            elif b == minv:
                left += 1
            else:
                break
        if H > 2:
            i = 1
            j = 1
            while 1:
                a = W * i
                b = W * j - a
                c = H * W - b - a
                maxv = max(a, b, c)
                minv = min(a, b, c)
                ret = min(ret, maxv - minv)
                if a == minv:
                    if j == H-1 : break
                    i += 1
                    j += 1
                elif b == minv:
                    j += 1
                else:
                    break
        return ret
    ans = f()
    H, W = W, H
    ans = min(ans, f())
    print(ans)
main()

Submission status: Wrong Answer

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

def main():
    H, W = map(int, input().split())
    def f():
        ret = H * W
        left = 1
        up = 1
        while 1:
            a = W * up
            b = (H - up) * (left)
            c = (H - up) * (W - left)
            maxv = max(a, b, c)
            minv = min(a, b, c)
            ret = min(ret, maxv - minv)
            if a == minv:
                if up == H-1 : break
                up += 1
            elif b == minv:
                if left == W-1 : break
                left += 1
            else:
                break
        if H > 2:
            i = 1
            j = 1
            while 1:
                a = W * i
                b = W * j - a
                c = H * W - b - a
                maxv = max(a, b, c)
                minv = min(a, b, c)
                ret = min(ret, maxv - minv)
                if a == minv:
                    if j == H-1 : break
                    i += 1
                    j += 1
                elif b == minv:
                    j += 1
                else:
                    break
        return ret
    ans = f()
    H, W = W, H
    ans = min(ans, f())
    print(ans)
main()

Submission status: Wrong Answer

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

def main():
    H, W = map(int, input().split())
    def f():
        ret = H * W
        left = 1
        up = 1
        while 1:
            a = W * up
            b = (H - up) * (left)
            c = (H - up) * (W - left)
            maxv = max(a, b, c)
            minv = min(a, b, c)
            ret = min(ret, maxv - minv)
            if a == minv:
                up += 1
            elif b == minv:
                left += 1
            else:
                break
        if H > 2:
            i = 1
            j = 1
            while 1:
                a = W * i
                b = W * j - a
                c = H * W - b - a
                maxv = max(a, b, c)
                minv = min(a, b, c)
                ret = min(ret, maxv - minv)
                if a == minv:
                    i += 1
                    j += 1
                elif b == minv:
                    j += 1
                else:
                    break
        return ret
    ans = f()
    H, W = W, H
    ans = min(ans, f())
    print(ans)
main()