Problem:


Score : 400 points

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min}.

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

Print the minimum possible value of S_{max} - S_{min}.

Sample Input 1

3 5

Sample Output 1

0

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000



Sample input:
3 5


Sample output:
0

Submission status: Wrong Answer

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

h = min(H, W)
w = max(H, W)
s = h*w

res = 10**10

a = h//3
x = s-a
b = x//2
c = x-b

res = min(max(a, b, c)-min(a, b, c), res)

a = w//3
x = s-a
b = x//2
c = x-b

res = min(max(a, b, c)-min(a, b, c), res)

for i in range(w-1):
    a = h*i
    x = s-a
    b = x//2
    c = x-b

    res = min(max(a, b, c)-min(a, b, c), res)


for i in range(h-1):
    a = w*i
    x = s-a
    b = x//2
    c = x-b

    res = min(max(a, b, c)-min(a, b, c), res)

print(res)

Submission status: Wrong Answer

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

h = min(H, W)
w = max(H, W)
s = h*w

res = H*W

for i in range(w-1):
    a = h*i
    x = s-a
    b = x//2
    c = x-b

    res = min(max(a, b, c)-min(a, b, c), res)


for i in range(h-1):
    a = w*i
    x = s-a
    b = x//2
    c = x-b

    res = min(max(a, b, c)-min(a, b, c), res)

print(res)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    h,w=map(int,readline().split())

    if h*w%3==0:
        ans=0
    else:
        ans=float("inf")
        #縦方向に全探索
        s1=w//2*h
        s2=h*w-s1
        for i in range(1,h):
            s3=i*w
            s1-=w//2
            s2-=w-w//2
            ans=min(ans,max(s1,s2,s3)-min(s1,s2,s3))

        #横方向に全探索
        s1=h//2*w
        s2=h*w-s1
        for j in range(1,w):
            s3=j*h
            s1-=h//2
            s2-=h-h//2
            ans=min(ans,max(s1,s2,s3)-min(s1,s2,s3))

    print(ans)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if h > w:
        h, w = w, h
    ans = h
    for w1 in range(w//3-2, w//3+3):
        s1 = w1 * h
        w2 = w-w1
        for h1 in range(h//2 + 1):
            s2 = w2 * h1
            s3 = w2 * (h-h1)
            ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if h > w:
        h, w = w, h
    ans = h
    w1 = int(w / 3)
    s1 = w1 * h
    for h1 in range(h):
        s2 = (w-w1) * h1
        s3 = (w-w1) * (h-h1)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    w1 = int(w / 3) + 1
    s1 = w1 * h
    for h1 in range(h):
        s2 = (w-w1) * h1
        s3 = (w-w1) * (h-h1)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    w1 = int(w / 3) + 2
    s1 = w1 * h
    for h1 in range(h):
        s2 = (w-w1) * h1
        s3 = (w-w1) * (h-h1)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if h > w:
        h, w = w, h
    ans = h*w
    w1 = ceil(w / 3)
    s1 = w1 * h
    for h1 in range(1, h):
        s2 = (w-w1) * h1
        s3 = (w-w1) * (h-h1)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    for w2 in range(w):
        s2 = h * (w2-w1)
        s3 = h * (w-w2)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    w1 = int(w / 3)
    s1 = w1 * h
    for h1 in range(1, h):
        s2 = (w-w1) * h1
        s3 = (w-w1) * (h-h1)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    for w2 in range(w):
        s2 = h * (w2-w1)
        s3 = h * (w-w2)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if h > w:
        h, w = w, h
    ans = h*w
    w1 = ceil(w / 3)
    s1 = w1 * h
    for h1 in range(1, h):
        s2 = (w-w1) * h1
        s3 = (w-w1) * (h-h1)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    w1 = int(w / 3)
    s1 = w1 * h
    for h1 in range(1, h):
        s2 = (w-w1) * h1
        s3 = (w-w1) * (h-h1)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    for w2 in range(w):
        s2 = h * (w2-w1)
        s3 = h * (w-w2)
        ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    print(ans)

Submission status: Wrong Answer

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

long = int(max(h,w))
short = int(min(h,w))

dif = h*w

if long %3 == 0 or short %3==0:
  print(0)
  exit()

s1_side = long //3
s1 = s1_side * short
remain_long = max(long-s1_side,short)
remain_short = min(long-s1_side,short)
#s2_side = remain_long // 2 +1
s3_side = remain_long // 2
#s2 = s2_side*remain_short
s3 = s3_side*remain_short
_dif = max(s1,s3)-min(s1,s3)
if dif > _dif:
  dif = _dif
  
s1_side = long //3 + 1
s1 = s1_side * short
remain_long = max(long-s1_side,short)
remain_short = min(long-s1_side,short)
#s2_side = remain_long // 2 +1
s3_side = remain_long // 2
#s2 = s2_side*remain_short
s3 = s3_side*remain_short
_dif = max(s1,s3)-min(s1,s3)
if dif > _dif:
  dif = _dif

print(dif)

Submission status: Wrong Answer

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

long = int(max(h,w))
short = int(min(h,w))

dif = h*w

if long %3 == 0 or short %3==0:
  print(0)
  exit()
  
s1 = (long // 3 + 1) * short
if (long - (long // 3 + 1)) %2 == 0:
  s2 = (long - (long // 3 + 1))  / 2 * short
  _dif = max(s1,s2) - min(s1,s2)
else:
  s2 = (long - (long // 3 + 1))  // 2 * short
  s3 = ((long - (long // 3 + 1))  // 2 +1) * short
  _dif = max(s1,s2,s3) - min(s1,s2,s3)
if dif > _dif:
  dif = _dif
if short %2 == 0:
  s2 = short //2 * (long-(long // 3 + 1))
  _dif = max(s1,s2) - min(s1,s2)
else:
  s2 = short //2 * (long-(long // 3 + 1))
  s3 = (short //2 +1) * (long-(long // 3 + 1))
  _dif = max(s1,s2,s3) - min(s1,s2,s3)
if dif > _dif:
  dif = _dif
print(dif)

Submission status: Wrong Answer

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

long = int(max(h,w))
short = int(min(h,w))

dif = h*w

if long %3 == 0 or short %3==0:
  print(0)
  exit()

for i in range(long+1):
  s1 = short * i
  for j in range(short+1):
    s2 = (w-i) * j
    s3 = (w-i) * (h -j)
    _dif = max(s1,s2,s3) - min(s1,s2,s3)
    if dif > _dif:
      dif = _dif
print(dif)

Submission status: Wrong Answer

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

ans = float('inf')

# only need to consider the horizontal length of the cut at the beginning.
# the vertical length must be cut in the center. (if possible.)
# if you cut outside of center, you will be father away from the answer.
if H % 2 == 0:
  for x in range(1, W):
    S1 = x * H
    S2 = (W - x) * (H / 2)
    ans = min(ans, abs(S1 - S2))
if W % 2 == 0:
  for x in range(1, H):
    S1 = W * x
    S2 = (H - x) * (W / 2)
    ans = min(ans, abs(S1 - S2))
# both sides are odd.
else:
  # cut side.
  for x in range(1, W):
    S1 = x * H
    S2 = (H // 2) * (W - x)
    S3 = ((H // 2) + 1) * (W - x)
    # if both sides are odd, three number appear.
    # it is not clear which of the three numbers is the largest or smallest, it need to be caluculated.
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)
  # cut vertical.
  for x in range(1, H):
    S1 = W * x
    S2 = (W // 2) * (H - x)
    S3 = ((W // 2) + 1) * (H - x)
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)

# if the length or side can be trisected, then the answer is 0.
if H % 3 == 0 or W % 3 == 0:
  ans = 0

print(int(ans))

Submission status: Wrong Answer

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

ans = float('inf')

# only need to consider the horizontal length of the cut at the beginning.
# the vertical length must be cut in the center. (if possible.)
# if you cut outside of center, you will be father away from the answer.
if H % 2 == 0:
  for x in range(1, W):
    S1 = x * H
    S2 = (W - x) * (H / 2)
    ans = min(ans, abs(S1 - S2))
if W % 2 == 0:
  for x in range(1, H):
    S1 = W * x
    S2 = (H - x) * (W / 2)
    ans = min(ans, abs(S1 - S2))
else:
  # both sides are odd.
  for x in range(1, W):
    S1 = x * H
    S2 = (W - x) * (H // 2)
    S3 = (W - x) * ((H // 2) + 1)
    # if both sides are odd, three number appear.
    # it is not clear which of the three numbers is the largest or smallest, it need to be caluculated.
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)
  for x in range(1, H):
    S1 = W * x
    S2 = (H - x) * (W // 2)
    S3 = (H - x) * ((W // 2) + 1)
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)

# if the length or side can be trisected, then the answer is 0.
if H % 3 == 0 or W % 3 == 0:
  ans = 0

print(int(ans))

Submission status: Wrong Answer

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

ans = float('inf')

# only need to consider the horizontal length of the cut at the beginning.
# the vertical length must be cut in the center. (if possible.)
# if you cut outside of center, you will be father away from the answer.
if H % 2 == 0:
  for x in range(1, W):
    S1 = x * H
    S2 = (W - x) * (H / 2)
    ans = min(ans, abs(S1 - S2))
if W % 2 == 0:
  for x in range(1, H):
    S1 = W * x
    S2 = (H - x) * (W / 2)
    ans = min(ans, abs(S1 - S2))
else:
  # both sides are odd.
  for x in range(1, W):
    S1 = x * H
    S2 = (W - x) * (H // 2)
    S3 = (W - x) * ((H // 2) + 1)
    # if both sides are odd, three number appear.
    # it is not clear which of the three numbers is the largest or smallest, it need to be caluculated.
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)
  for x in range(1, H):
    S1 = W * x
    S2 = (H - x) * (W / 2)
    S3 = (H - x) * ((W // 2) + 1)
    # if both sides are odd, three number appear.
    # it is not clear which of the three numbers is the largest or smallest, it need to be caluculated.
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)

# if the length or side can be trisected, then the answer is 0.
if H % 3 == 0 or W % 3 == 0:
  ans = 0

print(int(ans))

Submission status: Wrong Answer

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

ans = float('inf')

# only need to consider the horizontal length of the cut at the beginning.
# the vertical length must be cut in the center. (if possible.)
# if you cut outside of center, you will be father away from the answer.
if H % 2 == 0:
  for x in range(W):
    S1 = x * H
    S2 = (W - x) * (H / 2)
    ans = min(ans, abs(S1 - S2))
if W % 2 == 0:
  for x in range(H):
    S1 = W * x
    S2 = (H - x) * (W / 2)
    ans = min(ans, abs(S1 - S2))
else:
  # both sides are odd.
  for x in range(W):
    S1 = x * H
    S2 = (W - x) * (H // 2)
    S3 = (W - x) * ((H // 2) + 1)
    # if both sides are odd, three number appear.
    # it is not clear which of the three numbers is the largest or smallest, it need to be caluculated.
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)

# if the length or side can be trisected, then the answer is 0.
if H % 3 == 0 or W % 3 == 0:
  ans = 0

print(int(ans))

Submission status: Wrong Answer

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

ans = float('inf')

# only need to consider the horizontal length of the cut at the beginning.
# the vertical length must be cut in the center. (if possible.)
# if you cut outside of center, you will be father away from the answer.
if H % 2 == 0:
  for x in range(W):
    S1 = x * H
    S2 = (W - x) * (H / 2)
    ans = min(ans, abs(S1 - S2))
if W % 2 == 0:
  for x in range(H):
    S1 = W * x
    S2 = (H - x) * (W / 2)
    ans = min(ans, abs(S1 - S2))
else:
  # both sides are odd.
  for x in range(W):
    S1 = x * H
    S2 = (W - x) * (H // 2)
    S3 = (W - x) * ((H // 2) + 1)
    # if both sides are odd, three number appear.
    # it is not clear which of the three numbers is the largest or smallest, it need to be caluculated.
    S_max = max(S1, S3)
    S_min = min(S1, S2)
    ans = min(ans, S_max - S_min)

print(int(ans))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
else:
    num1=max((h//3)*w,(h-(h//3))*(w//2),(h-(h//3))*(w-(w//2)))-min((h//3)*w,(h-(h//3))*(w//2),(h-(h//3))*(w-(w//2)))
    num2=max((w//3)*h,(w-(w//3))*(h//2),(w-(w//3))*(h-(h//2)))-min((w//3)*h,(w-(w//3))*(h//2),(w-(w//3))*(h-(h//2)))
    num3=max((h//3+1)*w,(h-(h//3+1))*(w//2),(h-(h//3+1))*(w-(w//2)))-min((h//3+1)*w,(h-(h//3+1))*(w//2),(h-(h//3+1))*(w-(w//2)))
    num4=max((w//3+1)*h,(w-(w//3+1))*(h//2),(w-(w//3+1))*(h-(h//2)))-min((w//3+1)*h,(w-(w//3+1))*(h//2),(w-(w//3+1))*(h-(h//2)))
    print(min(num1,num2,num3,num4))

Submission status: Wrong Answer

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

def Choco(h, w):
    s_a, s_b, s_c = 0, 0, 0
    
    mw = w // 2
    ans = 10 ** 12
    
    for i in range(1, H):
        s_a = i * w
        mh = (h-i) // 2
        
        for j in range(2):
            if j == 0: #横に２つに分割
                s_b = (h - i) * mw
                s_c = (h - i) * (w - mw)
            else: #縦に２つに分割
                s_b = mh * w
                s_c = (h - i - mh) * w
        
            s_max = max(s_a, s_b, s_c)
            s_min = min(s_a, s_b, s_c)
            
            ans = min(ans, s_max - s_min)
            
    return ans

print(min(Choco(H, W), Choco(W, H)))

Submission status: Wrong Answer

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

if (H%3==0) or (W%3==0):
  print(0)
else:
  ans = H * W
  H, W = min(H, W), max(H, W)
  for wi in range(1, W//2 + 1):
    S1 = H * wi
    if (H%2==0) or ((W-wi)%2==0):
      S2 = H * (W-wi) // 2
      S3 = S2
    else:
      S2 = min(H, W-wi) * (max(H, W-wi)//2)
      S3 = S2 + min(H, W-wi)
    ans = min(ans, max(S1, S2, S3) - min(S1, S2, S3))
  print(ans)

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

H,W = IL()

ans = INF
for i in range(1,H):
  a = i*W
  if H-i>=2:
    b = ((H-i)//2)*W
    c = (-(-H-i)//2)*W
    ans = min(ans,max(a,b,c)-min(a,b,c))
  if W >= 2:
    b = (W//2)*(H-i)
    c = (-(-W)//2)*(H-i)
    ans = min(ans,max(a,b,c)-min(a,b,c))
for i in range(1,W):
  a = i*H
  if W-i>=2:
    b = ((W-i)//2)*H
    c = (-(-W-i)//2)*H
    ans = min(ans,max(a,b,c)-min(a,b,c))
  if H >= 2:
    b = (H//2)*(W-i)
    c = (-(-H)//2)*(W-i)
    ans = min(ans,max(a,b,c)-min(a,b,c))
print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    ret = 1000 * 10
    for h in range(H // 2 + 1):
        if h == 0:
            if H % 3 == 1:
                a = W // 3 * H
                b = a
                c = (W // 3 + 1) * H
            else:
                a = (W // 3 + 1) * H
                b = a
                c = W // 3 * H
        else:
            a = h * W
            bw = W // 2
            cw = W // 2 + W % 2
            b = bw * (H - h)
            c = cw * (H - h)
        ret = min(ret, max(a, b, c) - min(a, b, c))

    for w in range(W // 2 + 1):
        if w == 0:
            if W % 3 == 1:
                a = H // 3 * W
                b = a
                c = (H // 3 + 1) * W
            else:
                a = (H // 3 + 1) * W
                b = a
                c = H // 3 * W
        else:
            a = H * w
            bh = H // 2
            ch = H // 2 + H % 2
            b = bh * (W - w)
            c = ch * (W - w)
        ret = min(ret, max(a, b, c) - min(a, b, c))
    print(ret)

Submission status: Wrong Answer

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

def calc(hh, ww):
    ans = float('inf')
    if hh % 3 == 0 or ww % 3 == 0:
        ans = 0
    else:
        if hh >= 3:
            for i in range(1, hh-2):
                for j in range(i + 1, hh-1):
                    area1 = i * ww
                    area2 = (j-i) * ww
                    area3 =  (hh-j) * ww
                    num1 = max(area1, area2, area3)
                    num2 = min(area1, area2, area3)
                    num = num1 - num2
                    ans = min(ans, num)

                for k in range(1, ww-1):
                    ano_area1 = i * ww
                    ano_area2 = (hh - i) * k
                    ano_area3 = (hh - i) * (ww - k)
                    ano_num1 = max(ano_area1, ano_area2, ano_area3)
                    ano_num2 = min(ano_area1, ano_area2, ano_area3)
                    re_num = ano_num1 - ano_num2
                    ans = min(ans, re_num)
    return ans

print(min(calc(H, W), calc(W, H)))

Submission status: Wrong Answer

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

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    def calc_T_form(h, w):
        left = w // 2
        right = w - left
        ans = inf
        for i in range(1, h):
            sa, sb, sc = i * w, (h-i) * left, (h-i) * right
            ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))
        return ans
    
    def calc_rotateT_form(h, w):
        up = h // 2
        down = h - h//2
        ans = inf
        for i in range(1, w):
            sa, sb, sc = i * h, (w-i) * up, (w-i) * down
            ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))
        return ans
    
    def calc_slice(h, w):
        sa, sb, sc = (h//3)*w, (h//3)*w, (h-(h//3)*2)*w
        ans = sc - sa
        sa, sb, sc = (w//3)*h, (w//3)*h, (w-(w//3)*2)*h
        ans = min(ans, sc - sa)
        return ans

    
    h, w = mi()
    if h * w % 3 == 0:
        print(0)
    else:
        print(min(calc_T_form(h, w), calc_rotateT_form(h, w), calc_slice(h, w)))



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    def calc_T_form(h, w):
        left = w // 2
        right = w - left
        ans = inf
        for i in range(1, h):
            sa, sb, sc = i * w, (h-i) * left, (h-i) * right
            ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))
        return ans
    
    def calc_rotateT_form(h, w):
        up = h // 2
        down = h - h//2
        ans = inf
        for i in range(1, w):
            sa, sb, sc = i * h, (w-i) * up, (w-i) * down
            ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))
        return ans

    
    h, w = mi()
    if h * w % 3 == 0:
        print(0)
    else:
        print(min(calc_T_form(h, w), calc_rotateT_form(h, w)))



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=h*w
for i in range(1,h):
    a=set()
    if w%2==0 or (h-i)%2==0:
        a.add(i*w)
        a.add((h-i)*w/2)
        ans=min(ans,max(a)-min(a))
    elif w<h-i:
        a.add(i*w)
        a.add(((h-i)//2)*w)
        a.add(((h-i)//2+1)*w)
        ans=min(ans,max(a)-min(a))
    else:
        a.add(i*w)
        a.add((w//2)*(h-i))
        a.add((w//2+1)*(h-i))
        ans=min(ans,max(a)-min(a))
for i in range(1,w):
    b=set()
    if (w-i)%2==0 or h%2==0:
        b.add(i*h)
        b.add((w-i)*h/2)
        ans=min(ans,max(b)-min(b))
    elif h<w-i:
        b.add(i*w)
        b.add(((w-i)//2)*h)
        b.add(((w-i)//2+1)*h)
        ans=min(ans,max(b)-min(b))
    else:
        b.add(i*w)
        b.add((h//2)*(h-w))
        b.add((h//2+1)*(h-w))
        ans=min(ans,max(b)-min(b))
print(ans)

Submission status: Wrong Answer

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


def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        print(0)
    else:
        ans = 10**18

        # Hを1:2で分割
        for i in range(3):
            d = (H // 3)- 1 + i
            if d > 0:
                choco = [d * W, (H-d) * (W//2), (H-d) * (W - W//2)]
                ans = min(ans, max(choco) - min(choco))
        
        # Wを1:2で分割
        for i in range(3):
            d = (W // 3)- 1 + i
            if d > 0:
                choco = [d * H, (W-d) * (H//2), (W-d) * (H - H//2)]
                ans = min(ans, max(choco) - min(choco))
        
        print(ans)


if __name__ == "__main__":
    main()