Problem:


Score : 400 points

Problem Statement

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

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

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

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

Sample Input 1

3 5

Sample Output 1

0

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

Sample Input 2

4 5

Sample Output 2

2

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

Sample Input 3

5 5

Sample Output 3

4

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

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000



Sample input:
3 5


Sample output:
0

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
else:
    middle = w // 2
    ans = 10 ** 10
    for i in range(1,h + 1):
        area = [i * w, (h - i) * middle, (h - i) * (w - middle)]
        ans = min(ans, max(area) - min(area))
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
else:
    middle = w // 2
    ans = 10 ** 10
    for i in range(h):
        area = [i * w, (h - i) * middle, (h - i) * (w - middle)]
        ans = min(ans, max(area) - min(area))
print(ans)

Submission status: Wrong Answer

Solution code:
def solve(h, w):
  ans = 10**10
  if h >= 3:
    if h % 3 == 0:
      return 0
    else:
      ans = w
  if h == 2:
    ans = min(ans, w-w//2)
  else:
    t = h//3
    for d in range(h%3+1):
      a = 2*t+d
      b = W//2
      M = max(a*b, a*(w-b), (h-a)*w)
      m = min(a*b, a*(w-b), (h-a)*w)
      if M-m < ans:
        ans = M-m
  return ans
H, W = map(int, input().split())
print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h
    ans=min(a,b)

    for i in range(h+1):
        s1=i*w
        s2=(w//2)*(h-i)
        s3=(w-(w//2))*(h-i)
        ans=min(ans,max(s1,s2,s3)-min(s1,s2,s3))
    for i in range(w+1):
        s1=i*h
        s2=(h//2)*(w-i)
        s3=(h-(h//2))*(w-i)
        ans=min(ans,max(s1,s2,s3)-min(s1,s2,s3))
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h
    ans=min(a,b)

    for i in range(1,h):
        s1=i*w
        s2=(w//2)*(h-i)
        s3=(w-(w//2))*(h-i)
        ans=min(ans,max(s1,s2,s3)-min(s1,s2,s3))
    for i in range(1,w):
        s1=i*h
        s2=(h//2)*(w-i)
        s3=(h-(h//2))*(w-i)
        ans=min(ans,max(s1,s2,s3)-min(s1,s2,s3))
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h

    m=float('inf')
    for i in range(1,h):
        s1=i*w
        s2=(w//2)*(h-i)
        s3=(w-(w//2))*(h-i)
        m=min(m,max(s1,s2,s3)-min(s1,s2,s3))
    c=m
    n=float('inf')
    for i in range(1,w):
        s1=i*h
        s2=(h//2)*(w-i)
        s3=(h-(h//2))*(w-i)
        n=min(n,max(s1,s2,s3)-min(s1,s2,s3))
    d=n
    print(min(a,b,c,d))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h

    m=float('inf')
    for i in range(1,h):
        s1=i*w
        s2=(w//2)*(h-i)
        s3=(w-(w//2))*(h-i)
        x=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    c=m
    m=float('inf')
    for i in range(1,w):
        s1=i*h
        s2=(h//2)*(w-i)
        s3=(h-(h//2))*(w-i)
        x=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    d=m
    print(min(a,b,c,d))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h

    m=float('inf')
    for i in range(1,h//3+1):
        s1=i*w
        s2=(w//2)*(h-i)
        s3=(w-(w//2))*(h-i)
        x=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    c=m
    m=float('inf')
    for i in range(1,w//3+1):
        s1=i*h
        s2=(h//2)*(w-i)
        s3=(h-(h//2))*(w-i)
        x=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    d=m
    print(min(a,b,c,d))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h

    m=float('inf')
    for i in range(1,h//2+1):
        s1=i*w
        s2=(w//2)*(h-i)
        s3=(w-(w//2))*(h-i)
        x=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    c=m
    m=float('inf')
    for i in range(1,w//2+1):
        s1=i*h
        s2=(h//2)*(w-i)
        s3=(h-(h//2))*(w-i)
        x=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    d=m
    print(min(a,b,c,d))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h

    m=float('inf')
    for i in range(1,h//2):
        s1=i*w
        s2=(w//2)*(h-i)
        s3=(w-(w//2))*(h-i)
        x=min(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    c=m
    m=float('inf')
    for i in range(1,w//2):
        s1=i*h
        s2=(h//2)*(w-i)
        s3=(h-(h//2))*(w-i)
        x=min(abs(s1-s2),abs(s2-s3),abs(s3-s1))
        m=min(m,x)
    d=m
    print(min(a,b,c,d))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    a=(h%3)*w
    b=(w%3)*h

    x=h//3+1
    y=h-x
    n=w//2
    m=w-n
    s1=x*w
    s2=y*n
    s3=y*m
    c=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))

    x=h//3
    y=h-x
    n=w//2
    m=w-n
    s1=x*w
    s2=y*n
    s3=y*m
    d=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))

    x=w//3+1
    y=w-x
    n=h//2
    m=h-n
    s1=x*h
    s2=y*n
    s3=y*m
    c1=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))

    x=w//3
    y=w-x
    n=h//2
    m=h-n
    s1=x*h
    s2=y*n
    s3=y*m
    d1=max(abs(s1-s2),abs(s2-s3),abs(s3-s1))
    print(min(a,b,c,d,c1,d1))



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
H, W = [int(x) for x in stdin.readline().rstrip().split()]
sa = 0
sb = 0
sc = 0
ans = 10**9

for h in range(1, H):
    sa = h*W
    sb = (H-h)*(W//2)
    sc = (H-h)*(W-(W//2))
    tmp = max(sa, sb, sc)-min(sa, sb, sc)
    ans = min(ans, tmp)

for w in range(1, W):
    sa = H*W
    sb = (W-w)*(H//2)
    sc = (W-w)*(H-(H//2))
    tmp = max(sa, sb, sc)-min(sa, sb, sc)
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    sq = 0
else:
    sq = min((H // 3 + 1 - H // 3) * W, (W // 3 + 1 - W // 3) * H)
for H, W in [[W, H], [W, H]]:
    for h in range(1, H + 1):
        sq21 = h * W
        sq22 = (H - h) * -(-W // 2)
        sq23 = (H - h) * (W // 2)
        sq = min(sq, max(sq21, sq22, sq23) - min(sq21, sq22, sq23))
print(sq)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

import math
#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#scipy.sparse import csr_matrix
#scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def devide(a,b,target,m):
    t1=(math.ceil(a/m))*b
    t2=(math.floor(a/m))*b
    t3=(math.ceil(b/m))*a
    t4=(math.floor(b/m))*a

    t=min(abs(t1-target),abs(t2-target),abs(t3-target),abs(t4-target))

    if t==abs(t1-target):
        return a-math.ceil(a/m),b,t1
    elif t==abs(t2-target):
        return a-math.floor(a/m),b,t2
    elif t==abs(t3-target):
        return a,b-math.ceil(b/m),t3
    elif t==abs(t4-target):
        return a,b-math.floor(b/m),t4


def main():
    H,W=map(int,input().split())
    ans=0
    li=[]
    if (H%3)*(W%3)==0:
        ans=0
    else:
        H1,W1,t=devide(H,W,H*W/3,3)
        li.append(t)
        H2,W2,t=devide(H1,W1,H*W/3,2)
        li.append(t)
        li.append(H*W-sum(li))
        ans=max(li)-min(li)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  sm=h*w

  if h%3==0 or w%3==0:
    return 0

  ans=inf

  for i in range(h):
    a=i*w
    b=(h-i)*(w//2)
    c=(h-i)*(w-(w//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  for i in range(w):
    a=i*h
    b=(w-i)*(h//2)
    c=(w-i)*(h-(h//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  a=h//3*w
  ans=min(ans,abs(a-(sm-2*a)))

  b=w//3*h
  ans=min(ans,abs(b-(sm-2*b)))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  sm=h*w

  if h%3==0 or w%3==0:
    return 0

  ans=inf

  for i in range(h):
    a=i*w
    b=(h-i)*(w//2)
    c=(h-i)*(w-(w//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  for i in range(w):
    a=i*h
    b=(w-i)*(h//2)
    c=(w-i)*(h-(h//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  for i in range(h):
    a=i*w
    b=(h-i)*(w//2-1)
    c=(h-i)*(w-(w//2-1))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  for i in range(w):
    a=i*h
    b=(w-i)*(h//2-1)
    c=(w-i)*(h-(h//2-1))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  sm=h*w

  if h%3==0 or w%3==0:
    return 0

  ans=inf

  for i in range(h):
    a=i*w
    b=(h-i)*(w//2)
    c=(h-i)*(w-(w//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  for i in range(w):
    a=i*h
    b=(w-i)*(h//2)
    c=(w-i)*(h-(h//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  sm=h*w

  if h%3==0 or w%3==0:
    return 0

  ans=inf

  for i in range(1,h):
    a=i*w
    b=(h-i)*(w//2)
    c=(h-i)*(w-(w//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  for i in range(1,w):
    a=i*h
    b=(w-i)*(h//2)
    c=(w-i)*(h-(h//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  sm=h*w

  if h%3==0 or w%3==0:
    return 0

  ans=inf

  for i in range(1,h-1):
    a=i*w
    b=(h-i)*(w//2)
    c=(h-i)*(w-(w//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  for i in range(1,w-1):
    a=i*h
    b=(w-i)*(h//2)
    c=(w-i)*(h-(h//2))
    ans=min(ans,max(a,b,c)-min(a,b,c))

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))
ans = float("inf")
if H % 3 == 0 or W % 3 == 0:
    ans = 0
else:
    for i in range(1, W):
        a = H * i
        b = (W - i) * (H // 2)
        c = H * W - a - b
        S_min = min(a, min(b, c))
        S_max = max(a, max(b, c))
        ans = min(ans, S_max - S_min)
    for i in range(1, H):
        a = W * i
        b = (H - i) * (W // 2)
        c = H * W - a - b
        S_min = min(a, min(b, c))
        S_max = max(a, max(b, c))
        ans = min(ans, S_max - S_min)

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:
    H,W = max(H,W),min(H,W)
    if H%3==1:
        L = H//3
        if W%2==0:
            print(W//2)
        else:
            Smax=max(L*W, (2*L+1)*(W//2+1),(2*L+1)*(W//2))
            Smin=min(L*W, (2*L+1)*(W//2+1),(2*L+1)*(W//2))
            print(Smax-Smin)
    else:
        L = H//3
        if W%2==0:
            print(W//2)
        else:
            Smax=max((L+1)*W, (2*L+1)*(W//2+1),(2*L+1)*(W//2))
            Smin=min((L+1)*W, (2*L+1)*(W//2+1),(2*L+1)*(W//2))
            print(Smax-Smin)

Submission status: Wrong Answer

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

a = 0
b = 0
c = 0
ans = 1e+9

for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha) * (w//2)
    c = (h-ha) * math.ceil(w/2)
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

for ha in range(1, h-1):  # bとcを縦に分割するパターン
    a = ha * w
    b = (h-ha)//2 * w
    c = math.ceil((h-ha)/2) * w
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

h, w = w, h

for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha) * (w//2)
    c = (h-ha) * math.ceil(w/2)
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha)//2 * w
    c = math.ceil((h-ha)/2) * w
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

print(ans)

Submission status: Wrong Answer

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

a = 0
b = 0
c = 0
ans = 1e+9

for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha) * (w//2)
    c = (h-ha) * math.ceil(w/2)
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

for ha in range(1, h-1):  # bとcを縦に分割するパターン
    a = ha * w
    b = (h-ha)//2 * w
    c = math.ceil((h-ha)/2) * w
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

# h, w = w, h
#
#
# for ha in range(1, h-1):  # bとcを横に分割するパターン
#     a = ha * w
#     b = (h-ha) * w//2
#     c = (h-ha) * math.ceil(w/2)
#     smax = max(a, max(b, c))
#     smin = min(a, min(b, c))
#     ans = min(ans, smax-smin)
#
# for ha in range(1, h-1):  # bとcを横に分割するパターン
#     a = ha * w
#     b = (h-ha)//2 * w
#     c = math.ceil((h-ha)/2) * w
#     smax = max(a, max(b, c))
#     smin = min(a, min(b, c))
#     ans = min(ans, smax-smin)

print(ans)

Submission status: Wrong Answer

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

a = 0
b = 0
c = 0
ans = 1e+9

for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha) * w//2
    c = (h-ha) * math.ceil(w/2)
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha)//2 * w
    c = math.ceil((h-ha)/2) * w
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

h, w = w, h


for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha) * w//2
    c = (h-ha) * math.ceil(w/2)
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

for ha in range(1, h-1):  # bとcを横に分割するパターン
    a = ha * w
    b = (h-ha)//2 * w
    c = math.ceil((h-ha)/2) * w
    smax = max(a, max(b, c))
    smin = min(a, min(b, c))
    ans = min(ans, smax-smin)

print(ans)

Submission status: Wrong Answer

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

def d_min(s1,s2,s3):
  return max(s1,s2,s3)-min(s1,s2,s3)

tmp=[]
tmp.append(d_min(H//3*W,H//3*W,(H-H//3*2)*W))
tmp.append(d_min(W//3*H,W//3*H,(W-W//3*2)*H))

tmp.append(d_min(H//3*W,(H-H//3)*(W//2),(H-H//3)*(W-W//2)))
tmp.append(d_min((H//3+1)*W,(H-H//3-1)*(W//2),(H-H//3-1)*(W-W//2)))

tmp.append(d_min(W//3*H,(W-W//3)*(H//2),(W-W//3)*(H-H//2)))
tmp.append(d_min((W//3+1)*H,(W-W//3-1)*(H//2),(W-W//3-1)*(H-H//2)))

#print(tmp)
print(min(tmp))

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
c=min(a,b)
d=max(a,b)
if d%3==0 or c%3==0:print(0)
else:
  if d%3==1:
    m=d//3*c
    print(abs(m-(d-d//3)*(c//2)))
  elif d%3==2:
    m=(d//3+1)*c
    print(abs(m-(d-d//3-1)*(c//2)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 切り方は3通りです

# 1. 縦または横に大体三等分する
# 2. 縦または横に三分の一未満で最大切り取ったあと、
#   横または縦に大体二等分する
# 3. 縦または横に三分の一以上で最大切り取ったあと、
#   横または縦に大体二等分する


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

if (H%3)*(W%3)==0:
  print(0)
else:
  d1 = min(H, W)
  ah = H//3
  h = H-ah
  d2 = abs(((W+1)//2)*h - ah*W)
  aw = W//3
  w = W-aw
  d3 = abs(((H+1)//2)*w - aw*H)
  ah = (H+2)//3
  h = H-ah
  d4 = abs((W//2)*h - ah*W)
  aw = (W+2)//3
  w = W-aw
  d5 = abs((H//2)*w - aw*H)

  print((d1,d2,d3,d4,d5))

Submission status: Wrong Answer

Solution code:
# 切り方は3通りです

# 1. 縦または横に大体三等分する
# 2. 縦または横に三分の一未満で最大切り取ったあと、
#   横または縦に大体二等分する
# 3. 縦または横に三分の一以上で最大切り取ったあと、
#   横または縦に大体二等分する


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

if (H%3)*(W%3)==0:
  print(0)
else:
  d1 = min(H, W)
  ah = H//3
  h = H-ah
  d2 = abs(((W+1)//2)*h - ah)
  aw = W//3
  w = W-aw
  d3 = abs(((H+1)//2)*w - aw)
  ah = (H+2)//3
  h = H-ah
  d4 = abs((W//2)*h - ah)
  aw = (W+2)//3
  w = W-aw
  d5 = abs((H//2)*w - aw)

  print(min(d1,d2,d3,d4,d5))

Submission status: Wrong Answer

Solution code:
def main():
    h, w = map(int, input().split())
    if min(h % 3, w % 3) == 0:
        print(0)
        return
    s = h * w
    ans = min(h, w)
    for wcut in range(1, (w + 1) // 2):
        s1 = wcut * h
        s2 = (w - wcut) * (h // 2)
        s3 = s - s1 - s2
        ans = min(ans, max(s1, s3) - min(s1, s2))
    for hcut in range(1, (h + 1) // 2):
        s1 = hcut * w
        s2 = (h - hcut) * (w // 2)
        s3 = s - s1 - s2
        ans = min(ans, max(s1, s3) - min(s1, s2))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
li = [int(x) for x in input().split()]
H,W = max(li),min(li)

S0 = [0]*3
S1 = [0]*3
S2 = [(H//3)*W]*3
S3 = [(W//3)*H]*3

S0[0] = (H-H//3)*(W//2)
S0[1] = (H-H//3)*(W//2+(W%2))
S0[2] = (H//3)*W

S1[0] = (H*2//3)*(W//2)
S1[1] = (H*2//3)*(W//2+(W%2))
S1[2] = (H-H*2//3)*W

if(H%3!=0):
    S2[2] += W

if(W%3!=0):
    S3[2] += H
print(min(max(S0)-min(S0),max(S1)-min(S1),max(S2)-min(S2),max(S3)-min(S3)))

Submission status: Wrong Answer

Solution code:
li = [int(x) for x in input().split()]
H,W = max(li),min(li)

S0 = [0]*3
S1 = [0]*3
S2 = [(H//3)*W]*3
S3 = [0]*3
S4 = [0]*3

S0[0] = (H-H//3)*(W//2)
S0[1] = (H-H//3)*(W//2+(W%2))
S0[2] = (H//3)*W

S1[0] = (H*2//3)*(W//2)
S1[1] = (H*2//3)*(W//2+(W%2))
S1[2] = (H-H*2//3)*W

S3[0] = (W*2//3)*(H//2)
S3[1] = (W*2//3)*(H//2+(H%2))
S3[2] = (W-W*2//3)*H

S4[0] = (W*2//3)*(H//2)
S4[1] = (W*2//3)*(H//2+(H%2))
S4[2] = (W-W*2//3)*H

if(W%3==0):
    S2 = [(W//3)*H]*3
elif(H%3==1):
    S2[2] += W
elif(H%3==2):
    S2[1] += W
    S2[2] += W
print(min(max(S0)-min(S0),max(S1)-min(S1),max(S2)-min(S2),max(S3)-min(S3),max(S4)-min(S4)))

Submission status: Wrong Answer

Solution code:
li = [int(x) for x in input().split()]
H,W = max(li),min(li)

S0 = [0]*3
S1 = [0]*3
S2 = [(H//3)*W]*3

S0[0] = (H-H//3)*(W//2)
S0[1] = (H-H//3)*(W//2+(W%2))
S0[2] = (H//3)*W

S1[0] = (H*2//3)*(W//2)
S1[1] = (H*2//3)*(W//2+(W%2))
S1[2] = (H-H*2//3)*W

if(W%3==0):
    S2 = [(W//3)*H]*3
elif(H%3==1):
    S2[2] += W
elif(H%3==2):
    S2[1] += W
    S2[2] += W
print(min(max(S0)-min(S0),max(S1)-min(S1),max(S2)-min(S2)))

Submission status: Wrong Answer

Solution code:
import math
c, f = math.ceil, math.floor

def diff(arr):
    return max(arr) - min(arr)


def solve(h, w):
    if h%3 == 0 or w%3 == 0:
        return 0

    ps = [
        [h*c(w/3), c(h/2)*(w-c(w/3)), (h-c(h/2))*(w-c(w/3))],
        [h*c(w/3), f(h/2)*(w-c(w/3)), (h-f(h/2))*(w-c(w/3))],
        [h*f(w/3), c(h/2)*(w-f(w/3)), (h-c(h/2))*(w-f(w/3))],
        [h*f(w/3), f(h/2)*(w-f(w/3)), (h-f(h/2))*(w-f(w/3))],
        [w*c(h/3), c(w/2)*(h-c(h/3)), (w-c(w/2))*(h-c(h/3))],
        [w*c(h/3), f(w/2)*(h-c(h/3)), (w-f(w/2))*(h-c(h/3))],
        [w*f(h/3), c(w/2)*(h-f(h/3)), (w-c(w/2))*(h-f(h/3))],
        [w*f(h/3), f(w/2)*(h-f(h/3)), (w-f(w/2))*(h-f(h/3))],
    ]
    values = [diff(p) for p in ps]
    return min(values)

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

Submission status: Wrong Answer

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

if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    ans = 10**12
    s = h * w
    for p in [h//3, h//3 + 1]:
        s1 = w * p
        s2 = (w // 2) * (h - p)
        s3 = (w - (w // 2)) * (h - p)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))

    for p in [w//3, w//3 + 1]:
        s1 = h * p
        s2 = (h // 2) * (w - p)
        s3 = (h - (h // 2)) * (w - p)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))

    if h % 3 == 1:
        s1 = w * (h//3)
        s2 = w * (h//3)
        s3 = w * (h//3+1)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))

    elif h % 3 == 2:
        s1 = w * (h//3)
        s2 = w * (h//3+1)
        s3 = w * (h//3+1)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))

    if w % 3 == 1:
        s1 = h * (w//3)
        s2 = h * (w//3)
        s3 = h * (w//3+1)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))

    elif h % 3 == 2:
        s1 = h * (w//3)
        s2 = h * (w//3+1)
        s3 = h * (w//3+1)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))
print(ans)

Submission status: Wrong Answer

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

if h % 3 == 0 or w % 3 == 0:
    ans = 0
else:
    ans = 10**12
    s = h * w
    for p in [h//3, h//3 + 1]:
        s1 = w * p
        s2 = (w // 2) * (h - p)
        s3 = (w - (w // 2)) * (h - p)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))

    for p in [w//3, w//3 + 1]:
        s1 = h * p
        s2 = (h // 2) * (w - p)
        s3 = (h - (h // 2)) * (w - p)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))
print(ans)

Submission status: Wrong Answer

Solution code:

h, w = [ int(v) for v in input().split() ]

if h % 3 == 0 or w % 3 == 0:
    ans = (h*w//3)
else:
    ans = 10**12
    s = h * w
    for p in [h//3, h//3 + 1]:
        s1 = w * p
        s2 = (w // 2) * (h - p)
        s3 = (w - (w // 2)) * (h - p)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))

    for p in [w//3, w//3 + 1]:
        s1 = h * p
        s2 = (h // 2) * (w - p)
        s3 = (h - (h // 2)) * (w - p)
        ans = min(ans,(max(s1,s2,s3)-min(s1,s2,s3)))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h<w:
	h,w=w,h
if h%3==0 or w%3==0:
	print(0)
else:
	temp=[]
	temp2=[]
	n=h*w/3
	if h%3==1:
		hh=h//3
	else:
		hh=h//3+1
	temp.append(w*hh)
	temp2.append(w*hh)
	h=h-hh
	if h<w:
		h,w=w,h
	hh=h//2
	temp.append(w*hh)
	temp.append(w*(h-hh))
	temp=max(temp)-min(temp)
	if w%2==0:
		temp2.append(w//2*h)
		temp2=max(temp2)-min(temp2)
		print(min(temp,temp2))
	else:
		print(temp)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
    exit()
mn = min(H,W)
mx = max(H,W)
tmp = (mx // 3)+mx%2
a = tmp * mn
b = (mx - tmp) * (mn // 2)
c = (mx - tmp) * (mn // 2 + mn % 2)
print(max(a,b,c) - min(a,b,c))

Submission status: Wrong Answer

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


def diff(L):
    return(max(L)-min(L))

def solve(H: int, W: int):
    N = 3
    if H % N == 0 or W % N == 0:
        print(0)
        return
    
    h, w = H, W
    S = []
    
    for i in range(N-1):
        n = N - i
        err = H * W
        for d in range(n):
            h_temp = h // n + d
            w_temp = w
            s = h_temp * w_temp
            if s > h * w:
                break
            if abs((H * W) / N - s) < err:
                err = abs((H * W) / N - s)
                h_best = h_temp
                w_best = w_temp
        for d in range(n):
            h_temp = h
            w_temp = w // n + d
            s = h_temp * w_temp
            if s > h * w:
                break
            if abs((H * W) / N - s) < err:
                err = abs((H * W) / N - s)
                h_best = h_temp
                w_best = w_temp
        S.append(h_best * w_best)
        if h_best == h:
            h, w = h, w - w_best
        else:
            h, w = h - h_best, w
    S.append(h * w)
    print(diff(S))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def diff(L):
    return(max(L)-min(L))

def solve(H: int, W: int):
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return
    
    if H % 3 == 2:
        res_vertical = diff([(H // 3 + 1) * W, (H - H // 3 - 1) * (W // 2), (H - H // 3 - 1) * (W - W // 2)])
    else:
        res_vertical = diff([(H // 3) * W, (H - H // 3) * (W // 2), (H - H // 3) * (W - W // 2)])
    
    if W % 3 == 2:
        res_horizontal = diff([(W // 3 + 1) * H, (W - W // 3 - 1) * (H // 2), (W - W // 3 - 1) * (H - H // 2)])
    else:
        res_horizontal = diff([(W // 3) * H, (W - W // 3) * (H // 2), (W - W // 3) * (H - H // 2)])
    
    print(min(res_vertical, res_horizontal))

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(H, W)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def main(h,w):
    a=h
    for i in range(1,h//2+1):
        l=[i*w,(h-i)*(w//2),(h-i)*(w-w//2)]
        a=min(max(l)-min(l),a)
    return a
print(min(main(h,w),main(w,h)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def main(h,w):
    a=h
    for i in range(1,h//2+1):
        l=[i*w,(h-i)*w//2,(h-i)*(w-w//2)]
        a=min(max(l)-min(l),a)
    return a
print(min(main(h,w),main(w,h)))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
def main(h,w):
    a=h*w
    for i in range(1,h//2+1):
        l=[i*w,(h-i)*w//2,(h-i)*(w-w//2)]
        a=min(max(l)-min(l),a)
    return a
print(min(main(h,w),main(w,h)))

Submission status: Wrong Answer

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

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

a = []
a.append([H//3*W, (H-H//3)*(W//2), (H-H//3)*(W//2+W%2)])
a.append([(H//3 + 1)*W, (H-H//3-1)*(W//2), (H-H//3-1)*(W//2+W%2)])
print(H//3*W)
t = W
W = H
H = t
a.append([H//3*W, (H-H//3)*(W//2), (H-H//3)*(W//2+W%2)])
a.append([(H//3 + 1)*W, (H-H//3-1)*(W//2), (H-H//3-1)*(W//2+W%2)])

ans = min(W, H)
for i in range(len(a)):
    if  not 0 in a[i]:
        ans = min(ans, max(a[i]) - min(a[i]))


print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=h*w
for i in range(1,h):
  a=i*w
  b=(h-i)*int(w/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)
for i in range(1,h):
  a=i*w
  b=w*int((h-i)/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)

for i in range(1,w):
  a=i*h
  b=(w-i)*int(h/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)
for i in range(1,w):
  a=i*h
  b=h*int((w-1)/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=h*w
for i in range(1,h-1):
  a=i*w
  b=(h-i)*int(w/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)
for i in range(1,h-1):
  a=i*w
  b=w*int((h-i)/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)

for i in range(1,w-1):
  a=i*h
  b=(w-i)*int(h/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)
for i in range(1,w-1):
  a=i*h
  b=h*int((w-1)/2)
  c=h*w-a-b
  if ans>(max(a,b,c)-min(a,b,c)):
    ans=max(a,b,c)-min(a,b,c)
print(ans)

Submission status: Wrong Answer

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

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

H, W = max(H, W), min(H, W)
harfh, harfw = H // 2, W // 2

min_area = 10 ** 12
for harfh in range(harfh, 0, -1):
	min_area = min(min_area, max(abs(harfh * W - harfw * (H - harfh)), abs(harfh * W - (W - harfw) * (H - harfh))))
	#print(min_area, harfh, W, harfw, H - harfh)
print(min_area)