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


# 横に3分割する場合
if H % 3 == 0 or W % 3 == 0: print( 0 )
# 横と縦で3分割する場合
else:
    ans = 1e+9
    for h in range(1, H):
        S1 = h * W
        S2 = (H - h) * (W // 2)
        S3 = (H - h) * W - S2
        ans = min( ans, max(S1, S2, S3) - min(S1, S2, S3) )
    # ひっくり返してもう一回
    H, W = W, H
    for h in range(1, H):
        S1 = h * W
        S2 = (H - h) * (W // 2)
        S3 = (H - h) * W - S2
        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() )


# 横に3分割する場合
if H % 3 == 0 or W % 3 == 0: print( 0 )
# 横と縦で3分割する場合
else:
    ans = 1e+9
    for h in range(1, H):
        S1 = h * W
        S2 = (H - h) * (W // 2)
        S3 = (H - h) * (W - (W // 2))
        ans = min( ans, max(S1, S2, S3) - min(S1, S2, S3) )
    # ひっくり返してもう一回
    H, W = W, H
    for h in range(1, H):
        S1 = h * W
        S2 = (H - h) * (W // 2)
        S3 = (H - h) * (W - (W // 2))
        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() )


# 横に3分割する場合
if H % 3 == 0 or W % 3 == 0: print( 0 )
# 横と縦で3分割する場合
else:
    ans = 1e+9
    for h in range(1, H):
        S1 = h * W
        S2 = (H - h) * (W // 2)
        S3 = (H - h) * (W - (W // 2))
        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() )


# 横に3分割する場合
if H % 3 == 0 or W % 3 == 0: print( 0 )
# 横と縦で3分割する場合
else:
    ans = 1e+9
    for h in range(1, H):
        S1 = h * W
        S2 = (H - h) * (W // 2)
        S3 = (H - h) * (W - (W // 2))
        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 % 3 == 0 or W % 3 == 0:
    print(0)
else:
    ans = float("inf")
    for h in range(1, H):
        y = (H - h) * W
        x = h * (W // 2)
        z = h * W - x
        ans = min(ans, max(x, y, z) - min(x, y, z))
    t = H
    H = W
    W = t
    for h in range(1, H):
        y = (H - h) * W
        x = h * (W // 2)
        z = h * W - x
        ans = min(ans, max(x, y, z) - min(x, y, z))
    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 and W % 2:
    ans = float("inf")
    for h in range(1, H):
        y = (H - h) * W
        x = h * (W // 2)
        z = h * W - x
        ans = min(ans, max(x, y, z) - min(x, y, z))
    print(ans)
else:
    if W % 2:
        t = H
        H = W
        W = t
    ans = float("inf")
    for h in range(1, H):
        x = h * W // 2
        y = W * (H - h)
        ans = min(ans, abs(y - x))
    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 and W % 2:
    ans = float("inf")
    for h in range(1, H):
        y = (H - h) * W
        x = h * (W // 2)
        z = h * W - x
        ans = min(ans, max(abs(y - x), abs(x - z), abs(y - z)))
    print(ans)
else:
    if W % 2:
        t = H
        H = W
        W = t
    ans = float("inf")
    for h in range(1, H):
        x = h * W // 2
        y = H * W - 2 * x
        ans = min(ans, abs(y - x))
    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 and W % 2:
    ans = float("inf")
    for h in range(1, H):
        y = (H - h) * W
        for w in range(1, W//2 + 2):
            x = h * w
            z = h * W - x
            ans = min(ans, max(abs(y - x), abs(x - z), abs(y - z)))
    print(ans)
else:
    if W % 2:
        t = H
        H = W
        W = t
    ans = float("inf")
    for h in range(1, H):
        x = h * W // 2
        y = H * W - 2 * x
        ans = min(ans, abs(y - x))
    print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
  print(h*w//3)
else:
  print(min(h,w))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
else:
    ans=999999999
    for l in range(1,max(h,w)+1):
        s1=l*min(h,w)
        s2=(max(h,w)-l)*(min(h,w)//2)
        s3=h*w-s1-s2
        s4=((max(h,w)-l)//2)*min(h,w)
        s5=h*w-s1-s4
        list1=sorted([s1,s2,s3])
        list2=sorted([s1,s4,s5])
        s=min((list1[-1]-list1[0]),(list2[-1]-list2[0]))
        if ans>s:
            ans=s
    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:
    ans=99999999999
    for l in range(1,max(h,w)+1):
        s1=l*min(h,w)
        s2=(max(h,w)-l)*(min(h,w)//2)
        s3=h*w-s1-s2
        list=sorted([s1,s2,s3])
        s=list[-1]-list[0]
        if ans>s:
            ans=s
    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:
    ans=99999999
    for l in range(1,max(h,w)+1):
        s1=l*min(h,w)
        s2=(max(h,w)-l)*(min(h,w)//2)
        s3=h*w-s1-s2
        list=sorted([s1,s2,s3])
        s=list[-1]-list[0]
        if ans>s:
            ans=s
    print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A

#B

#C
h,w = LI()
ans = float("inf")
if h%3 == 0 or w%3 == 0:
    print(0)
    quit()
x = w//2
y = h//2
if not w%2:
    for i in range(1,h):
        ans = min(ans,abs(w*i-x*(h-i)))
else:
    for i in range(1,h):
        if w*i < x*(h-i):
            ans = min(ans,abs(w*i-(x+1)*(h-i)))
        elif w*i < (x+1)*(h-i):
            ans = min(ans,h-i)
        else:
            ans = min(ans,abs(w*i-x*(h-i)))
if not h%2:
    for i in range(1,w):
        ans = min(ans,abs(h*i-y*(w-i)))
else:
    for i in range(1,w):
        if h*i < y*(w-i):
            ans = min(ans,abs(h*i-(y+1)*(w-i)))
        elif h*i < (y+1)*(w-i):
            ans = min(ans,w-i)
        else:
            ans = min(ans,abs(h*i-y*(w-i)))
print(ans)

#D

#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(1, H):
    a = i * W
    b = ((H - i) // 2) * W
    c = ((H - i) - (H - i) // 2) * W
    tmp = max(a, b, c) - min(a, b, c)
    ans = min(ans, tmp)
    
for i in range(1, H):
    a = i * W
    b = (H - i) * (W // 2)
    c = (H - i) * (W - W // 2)
    tmp = max(a, b, c) - min(a, b, c)
    ans = min(ans, tmp)
    
for i in range(1, W):
    a = i * H
    b = (H // 2) * (W - i)
    c = (H - H // 2) * (W - i)
    tmp = max(a, b, c) - min(a, b, c)
    ans = min(ans, tmp)
    
for i in range(1, W):
    a = i * H
    b = ((W - i) // 2) * H
    c = ((W - i) - (W - i) // 2) * 2
    tmp = max(a, b, c) - min(a, b, c)
    ans = min(ans, tmp)    
    
print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(1, H):
    a = i * W
    b = (H - i) // 2 * W
    c = ((H - i) - (H - i) // 2) * W
    tmp = max(a, b, c) - min(a, b, c)
    ans = min(ans, tmp)
    
for i in range(1, H):
    a = i * W
    b = (H - i) * (W // 2)
    c = (H - i) * (W - W // 2)
    tmp = max(a, b, c) - min(a, b, c)
    ans = min(ans, tmp)
    
for i in range(1, W):
    a = i * H
    b = (H // 2) * (W - i)
    c = (H - H // 2) * (W - i)
    tmp = max(a, b, c) - min(a, b, c)
    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:
    print(0)
else:
    # pattern1は縦or横にぶった切るパターン
    pattern1 = min(w, h)
    # pattern2は縦と横に切るパターン(pattern2にも2パターンある)
    # (a, b)のセットと, cのブロックで考える
    a_w = w // 2
    b_w = w - a_w
    c_w = w
    pattern2_1 = float("inf")
    for tmp_h in range(1, h + 1):
        tmp_diff = max(abs(c_w * tmp_h - a_w * (h - tmp_h)), abs(c_w * tmp_h - b_w * (h - tmp_h)))
        if tmp_diff < pattern2_1:
            pattern2_1 = tmp_diff
        else:
            break
    a_h = h // 2
    b_h = h - a_h
    c_h = h
    pattern2_2 = float("inf")
    for tmp_w in range(1, h + 1):
        tmp_diff = max(abs(c_h * tmp_w - a_w * (w - tmp_w)), abs(c_h * tmp_w - b_h * (w - tmp_w)))
        if tmp_diff < pattern2_2:
            pattern2_2 = tmp_diff
        else:
            break
    print(min(pattern1, pattern2_1, pattern2_2))

Submission status: Wrong Answer

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

inf = float("inf")
ans = inf

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

case1 = [(H//3)  * W, (H-(H//3))*(W//2), (H-(H//3))*(W-W//2)]
case2 = [(H//3 + 1)  * W, (H-(H//3)-1)*(W//2), (H-(H//3)-1)*(W-W//2)]
case3 = [(H//3 + 2)  * W, (H-(H//3)-2)*(W//2), (H-(H//3)-2)*(W-W//2)]
case4 = [(W//3)  * H, (W-(W//3))*(H//2), (W-W//3) * (H-H//2)]
case5 = [(W//3 + 1)  * H, (W-(W//3)-1)*(H//2), (W-W//3 - 1) * (H-H//2)]
case6 = [(W//3 + 2)  * H, (W-(W//3)-2)*(H//2), (W-W//3 - 2) * (H-H//2)]

ans = min(ans,  max(case1) - min(case1))
ans = min(ans,  max(case2) - min(case2))
ans = min(ans,  max(case3) - min(case3))
ans = min(ans,  max(case4) - min(case4))
ans = min(ans,  max(case5) - min(case5))
ans = min(ans,  max(case6) - min(case6))

print(ans)

Submission status: Wrong Answer

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

inf = float("inf")
ans = inf

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

case1 = [(H//3)  * W, (H-(H//3))*(W//2), (H-(H//3))*(W-W//2)]
case1_2 = [(H//3)  * W, (H-(H//3))*(W//2+1), (H-(H//3))*(W-W//2-1)]
case2 = [(H//3 + 1)  * W, (H-(H//3)-1)*(W//2), (H-(H//3)-1)*(W-W//2)]
case2_2 = [(H//3 + 1)  * W, (H-(H//3)-1)*(W//2+1), (H-(H//3)-1)*(W-W//2-1)]
case3 = [(W//3)  * H, (W-(W//3))*(H//2), (W-W//3) * (H-H//2)]
case3_2 = [(W//3)  * H, (W-(W//3))*(H//2+1), (W-W//3) * (H-H//2-1)]
case4 = [(W//3 + 1)  * H, (W-(W//3)-1)*(H//2), (W-W//3 - 1) * (H-H//2)]
case4_2 = [(W//3 + 1)  * H, (W-(W//3)-1)*(H//2+1), (W-W//3 - 1) * (H-H//2-1)]

ans = min(ans,  max(case1) - min(case1))
ans = min(ans,  max(case2) - min(case2))
ans = min(ans,  max(case3) - min(case3))
ans = min(ans,  max(case4) - min(case4))
ans = min(ans,  max(case1_2) - min(case1_2))
ans = min(ans,  max(case2_2) - min(case2_2))
ans = min(ans,  max(case3_2) - min(case3_2))
ans = min(ans,  max(case4_2) - min(case4_2))

print(ans)

Submission status: Wrong Answer

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

inf = float("inf")
ans = inf

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

case1 = [(H//3)  * W, (H-(H//3))*(W//2), (H-(H//3))*(W-W//2)]
case2 = [(H//3 + 1)  * W, (H-(H//3)-1)*(W//2), (H-(H//3)-1)*(W-W//2)]
case3 = [(W//3)  * H, (W-(W//3))*(H//2), (W-W//3) * (H-H//2)]
case4 = [(W//3 + 1)  * H, (W-(W//3)-1)*(H//2), (W-W//3 - 1) * (H-H//2)]

ans = min(ans,  max(case1) - min(case1))
ans = min(ans,  max(case2) - min(case2))
ans = min(ans,  max(case3) - min(case3))
ans = min(ans,  max(case4) - min(case4))

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

if H%3 == 0 or W%3 == 0:
    print(0)
else:
    res = 10 ** 11
    for i in range(1, H):
        a = W*i
        b = math.ceil(W/2) * (H-i)
        m = W//2 * (H-i)

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

    for i in range(1, W):
        a = H*i
        b = math.ceil(H/2) * (W-i)
        m = H//2 * (W-i)
        res = min(res, max(a,b,m)-min(a,b,m))

               
    print(res)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if h%3==0:
  print(0)
elif h%3==0:
  print(0)
elif h%2==0 and w%2==0:
  ans=min(w,h)
  for i in range(w+1):
    ans = min(abs((h//2)*(w-i)-h*i),ans)
  for i in range(h+1):
    ans = min(abs((w//2)*(h-i)-w*i),ans)
  print(ans)
elif h%2==0 and w%2==1:
  ans=min(w,h)
  for i in range(w+1):
    ans = min(abs((h//2)*(w-i)-h*i),ans)
  for i in range(h+1):
    ans = min(max(abs((w//2)*(h-i)-w*i),abs((w//2+1)*(h-i)-w*i),abs(h-i)),ans)
  print(ans)
elif h%2==1 and w%2==0:
  ans=min(w,h)
  ans=w*h
  for i in range(w+1):
    ans = min(abs((h//2)*(w-i)-h*i),ans)
  for i in range(h+1):
    ans = min(max(abs((w//2)*(h-i)-w*i),abs((w//2+1)*(h-i)-w*i),abs(h-i)),ans)
  print(ans)
elif h%2==1 and w%2==1:
  ans=min(w,h)
  for i in range(w+1):
    ans = min(max(abs((h//2)*(w-i)-h*i),abs((h//2+1)*(w-i)-h*i),abs(w-i)),ans)
  for i in range(h+1):
    ans = min(max(abs((w//2)*(h-i)-w*i),abs((w//2+1)*(h-i)-w*i),abs(h-i)),ans)
  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()
if h < w:
    tmp = h
    h = w
    w = tmp
if h / 3 - h // 3 >= 0.5:
    h1 = h // 3 + 1
else:
    h1 = h // 3
s1 = h1 * w
h2 = h - h1

if h2 < w:
    tmp = h2
    h2 = w
    w = tmp
h3 = h2 // 2
s2 = h3 * w
h4 = h2 - h3
s3 = h4 * w
print(max([s1, s2, s3]) - min([s1, s2, s3]))

Submission status: Wrong Answer

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

from collections import defaultdict, Counter
from math import pi, sqrt
from bisect import bisect_left, bisect_right

def req_S(h, w):
    tmp_s = []
    a_w = w
    for a_h in range(1, h):
        # b, cが横のパターン
        b_h = (h-a_h)//2
        c_h = h-b_h
        b_w, c_w = w, w
        S = [a_w*a_h, b_w*b_h, c_w*c_h]
        tmp_s.append(max(S)-min(S))
        # b, cが縦のパターン
        b_w = w//2
        c_w = w-b_w
        b_h, c_h = h-a_h, h-a_h
        S = [a_w*a_h, b_w*b_h, c_w*c_h]
        tmp_s.append(max(S)-min(S))
    return tmp_s

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

all_S = req_S(h, w) + req_S(w, h)
print(min(all_S))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ans = float("inf")
    for i in range(h):
        choco = [i * w, (h - i) * (w // 2), (h - i) * (w - w // 2)]
        ans = min(ans, max(choco) - min(choco))
        print(i, choco, ans)
    for i in range(w):
        choco = [i * h, (w - i) * (h // 2), (w - i) * (h - h // 2)]
        ans = min(ans, max(choco) - min(choco))
        print(i, choco, ans)
    for i in range(h):
        choco = [i * w, w * (h - i) // 2, w * ((h - i) - (h - i) // 2)]
        ans = min(ans, max(choco) - min(choco))
    for i in range(w):
        choco = [i * h, h * (w - i) // 2, h * ((w - i) - (w - i) // 2)]
        ans = min(ans, max(choco) - min(choco))
    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:
    ans = float("inf")
    for i in range(h):
        choco = [i * w, (h - i) * (w // 2), (h - i) * (w - w // 2)]
        ans = min(ans, max(choco) - min(choco))
    for i in range(w):
        choco = [i * h, (w - i) * (h // 2), (w - i) * (h - h // 2)]
        ans = min(ans, max(choco) - min(choco))
    print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
ans = float("inf")
for i in range(h):
    choco = [i * w, (h - i) * (w // 2), (h - i) * (w - w // 2)]
    ans = min(ans, max(choco) - min(choco))
for i in range(w):
    choco = [i * h, (w - i) * (h // 2), (w - i) * (h - h // 2)]
    ans = min(ans, max(choco) - min(choco))
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=10**9
w1,w2=w//2,w-w//2
for i in range(1,h):
  s=i*w,w1*(h-i),w2*(h-i)
  ans=min(ans,max(s)-min(s))
h1,h2=h//2,h-h//2
for i in range(1,w):
  s=i*h,h1*(w-i),h2*(w-i)
  ans=min(ans,max(s)-min(s))
if h%3==0 or w%3==0:
  ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


def ceil(x):
    return int((-x)//(1)*(-1))


def solve(a, b):
    """
    a,b = H,W or W,H
    aの方を三等分ぽく分解する
    """
    upper = ceil(a/3*2)
    lower = a//3-1
    l = [0]*3
    ret = float('inf')
    for i in range(lower, upper+1):
        if b % 2 == 0:
            l[0] = i*b
            l[1] = (a-i)*b//2
            l[2] = (a-i)*b//2
        else:
            l[0] = i*b
            l[1] = (a-i)*(b//2)
            l[2] = (a-i)*(b//2+1)
        ret = min(max(l)-min(l), ret)
    return ret


if H % 3 == 0 or W % 3 == 0:
    ans = 0
else:
    ans = min(solve(H, W), solve(W, H))
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


def ceil(x):
    return int((-x)//(1)*(-1))


def solve(a, b):
    """
    a,b = H,W or W,H
    aの方を三等分ぽく分解する
    """
    upper = ceil(a/3*2)
    lower = a//3
    l = [0]*3
    ret = float('inf')
    for i in range(lower, upper+1):
        if b % 2 == 0:
            l[0] = i*b
            l[1] = (a-i)*b//2
            l[2] = (a-i)*b//2
        else:
            l[0] = i*b
            l[1] = (a-i)*(b//2)
            l[2] = (a-i)*(b//2+1)
        ret = min(max(l)-min(l), ret)
    return ret


if H % 3 == 0 or W % 3 == 0:
    ans = 0
else:
    ans = min(solve(H, W), solve(W, H))
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


def ceil(x):
    return int((-x)//(1)*(-1))


def solve(a, b):
    """
    a,b = H,W or W,H
    aの方を三等分ぽく分解する
    """
    upper = ceil(a/3)
    lower = a//3
    l = [0]*3
    ret = float('inf')
    for i in range(lower, upper+1):
        if b % 2 == 0:
            l[0] = i*b
            l[1] = (a-i)*b//2
            l[2] = (a-i)*b//2
        else:
            l[0] = i*b
            l[1] = (a-i)*(b//2)
            l[2] = (a-i)*(b//2+1)
        ret = min(max(l)-min(l), ret)
    return ret


if H % 3 == 0 or W % 3 == 0:
    ans = 0
else:
    ans = min(solve(H, W), solve(W, H))
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = list(map(int, input().split()))
print(int(h % 3 * w % 3))

Submission status: Wrong Answer

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

for h in range(1,H):
    l = H-h
    cand1 = [h*W]
    cand2 = [h*W]
    cand1.append(l//2*W)
    cand1.append((l-l//2)*W)
    cand2.append(l*(W//2))
    cand2.append(l*(W-W//2))
    candidate.append(max(cand1)-min(cand1))
    candidate.append(max(cand2)-min(cand2))

for w in range(1,W):
    m = W-w
    cand1 = [H*w]
    cand2 = [H*w]
    cand1.append(H*(m//2))
    cand1.append(H*(m-m//2))
    cand2.append(m*(H//2))
    cand2.append(m*(H-H//2))
    candidate.append(max(cand1)-min(cand1))
    candidate.append(max(cand2)-min(cand2))
    if max(cand1)-min(cand1) == 3 or max(cand2)-min(cand2) == 3:
        print(w)
        print(cand1)
        print(cand2)
        

print(min(candidate))

Submission status: Wrong Answer

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

if h % 3 == 0 and w % 3 == 0:
    print(0)
else:
    ans_h0 = h//2
    ans_h1 = max(h//2+w//3+1, w//3*2+1)
    ans_w0 = w//2
    ans_w1 = max(w//2+h//3+1, h//3*2+1)
    print(min((1-h % 2)*ans_h0+(h % 2)*ans_h1,
              (1-w % 2)*ans_w0+(w % 2)*ans_w1))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
print(min(h//2+h % 2, w//2+w % 2) if h % 3 and w % 3 else 0)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s, l = min(H, W), max(H, W)

ans = 10**6
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    for i in range(1, l):
        area1 = s*i
        area2 = s//2 * (l - i)
        area3 = area2 if s % 2 == 0 else (s//2 + 1) * (l - i)
        ans = min(ans, max(area1, area2, area3) - min(area1, area2, area3))
    for i in range(1, s):
        area1 = l*i
        area2 = l//2 * (s - i)
        area3 = area2 if l % 2 == 0 else (l//2 + 1) * (s - i)
        ans = min(ans, max(area1, area2, area3) - min(area1, area2, area3))
    print(ans)

Submission status: Wrong Answer

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

if h * w % 3 == 0:
    ans = 0
    
elif h == 1 or w == 1:
    ans = 1

else:
    ans1 = 10 ** 5
    for i in range(1, h + 1):
        a = i * w
        b = (h - i) * (w // 2)
        c = (h - i ) * (w - w // 2)
        tmp = max(a, b, c) - min(a, b, c)
        ans1 = min(ans1, tmp)
        
    ans2 = 10 ** 5
    for i in range(1, w + 1):
        a = i * h
        b = (w- i) * (h // 2)
        c = (w - i) * (h - h // 2)
        tmp = max(a, b, c) - min(a, b, c)
        ans2 = min(ans2, tmp)
        
    ans = min(ans1, ans2)
    
print(ans)

Submission status: Wrong Answer

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

if h * w % 3 == 0:
    ans = 0

else:
    ans1 = 10 ** 5
    for i in range(1, h + 1):
        a = i * w
        b = (h - i) * (w // 2)
        c = (h - i ) * (w - w // 2)
        tmp = max(a, b, c) - min(a, b, c)
        ans1 = min(ans1, tmp)
        
    ans2 = 10 ** 5
    for i in range(1, w + 1):
        a = i * h
        b = (w- i) * (h // 2)
        c = (w - i) * (h - h // 2)
        tmp = max(a, b, c) - min(a, b, c)
        ans2 = min(ans2, tmp)
        
    ans = min(ans1, ans2)
    
print(ans)

Submission status: Wrong Answer

Solution code:

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

ans = h*w
for i in range(1, w):
	s1 = int(h/2)*(w-i)
	s2 = i*h
	s3 = (h-int(h/2))*(w-i)
	S = max(s1,s2,s3)
	s = min(s1,s2,s3)
	#print(s1,s2,s3)
	if abs(S-s)<ans:
		ans = abs(S-s)

print(ans)

Submission status: Wrong Answer

Solution code:

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

ans = h*w
for i in range(1, w):
	s1 = int(h/2)*(w-i)
	s2 = i*h
	s3 = (h-int(h/2))*(w-i)
	S = max(s1,s2,s3)
	s = min(s1,s2,s3)
	if abs(S-s)<ans:
		ans = abs(S-s)

print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h > w:
	t = w
	w = h
	h = t

ans = h*w
for i in range(1, w):
	s1 = int(h/2)*(w-i)
	s2 = i*h
	s3 = (int(h/2)+1)*(w-i)
	S = max(s1,s2,s3)
	s = min(s1,s2,s3)
	if abs(S-s)<ans:
		ans = abs(S-s)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k =  map(int, input().split())
m = 10000000000
if n%3==0 or k%3==0:print(0)
else:
    for h in range(0,n):
        if k-h <0:continue
        a = n*h
        b = (n//2)*(k-h)
        c = (n-n//2)*(k-h)
        d = ((k-h)//2)*(k)
        e = (k-(k-h)//2)*(k)
        #print(a,b,c)
        m = min(m,max(a,b,c)-min(a,b,c),max(a,d,e)-min(a,d,e))
    n,k=k,n
    for h in range(0,n):
        if k-h <0:continue
        a = n*h
        b = (n//2)*(k-h)
        c = (n-n//2)*(k-h)
        d = ((k-h)//2)*(k)
        e = (k-(k-h)//2)*(k)
        #print(a,b,c)
        m = min(m,max(a,b,c)-min(a,b,c),max(a,d,e)-min(a,d,e))
    print(m)

Submission status: Wrong Answer

Solution code:
n,k =  map(int, input().split())
m = 10000000000
if n%3==0 or k%3==0:print(0)
else:
    for h in range(0,n):
        a = n*h
        b = (n//2)*(k-h)
        c = (n-n//2)*(k-h)
        #print(a,b,c)
        m = min(m,max(a,b,c)-min(a,b,c))
    n,k=k,n
    for h in range(0,n):
        a = n*h
        b = (n//2)*(k-h)
        c = (n-n//2)*(k-h)
        #print(a,b,c)
        m = min(m,max(a,b,c)-min(a,b,c))
    print(m)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
s = H*W
def func(h,w):
    tmp = float("inf")
    for x in range(1,w):
        s1 = h*x
        s2 = (w-x)*(h//2)
        s3 = s - s1 - s2
        d = max(s1,s2,s3) - min(s1,s2,s3)
        if d < tmp:
            tmp = d
    return tmp
ans = min(func(H,W),func(W,H),(W%3)*H,(H%3)*W)
print(ans)

Submission status: Wrong Answer

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

ans = 9999999999
# 高さの分割で全探索
for h in range(1,H-1):
    s1 = h*W
    # 残った領域をさらに高さで分割
    s2 = ((H-h)//2)*W 
    s3 = (H-h-(H-h)//2)*W
    ans = min(ans, max(s1,s2,s3)-min(s1,s2,s3))
    # 残った領域を幅で分割
    s2 = (W//2)*(H-h)
    s3 = (W-W//2)*(H-h)
    ans = min(ans, max(s1,s2,s3)-min(s1,s2,s3))


# 高さと幅を入れ替えて再実行
H,W = W,H
for h in range(1,H-1):
    s1 = h*W
    # 残った領域をさらに高さで分割
    s2 = ((H-h)//2)*W 
    s3 = (H-h-(H-h)//2)*W
    ans = min(ans, max(s1,s2,s3)-min(s1,s2,s3))
    # 残った領域を幅で分割
    s2 = (W//2)*(H-h)
    s3 = (W-W//2)*(H-h)
    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
ans = 0

if (H%3 == 0) or (W%3 == 0):
    ans = 0
elif H%3 == 1:
    smax = (H//3)*W
    h = H - H//3
    if (H%2 == 0) or (W%2 == 0):
        smin = h*W//2
    else:
        simn = max((h//2)*W, h*(W//2))
    ans = smax - smin
else:
    smax = (H//3 + 1)*W
    h = H - (H//3 + 1)
    if (H%2 == 0) or (W%2 == 0):
        smin = h*W//2
    else:
        smin = max((h//2)*W, h*(W//2))
    ans = smax - smin

print(abs(ans))

Submission status: Wrong Answer

Solution code:
from sys import stderr, setrecursionlimit, exit
from functools import reduce
from math import ceil, floor
from operator import add
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

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

min_dif = 10**11
for i in range(w):
    f = h*i
    s = (w-i)*floor(h/2)
    t = (w-i)*ceil(h/2)
    dif = max(abs(f-s),abs(s-t),abs(t-f))
    if dif < min_dif:
        min_dif = dif
for i in range(h):
    f = w*i
    s = (h-i)*floor(w/2)
    t = (h-i)*ceil(w/2)
    dif = max(abs(f-s),abs(s-t),abs(t-f))
    if dif < min_dif:
        min_dif = dif
print(min_dif)

Submission status: Wrong Answer

Solution code:
from sys import stderr, setrecursionlimit, exit
from functools import reduce
from math import ceil, floor
from operator import add
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

h, w = getInts()
min_dif = 10**11
for i in range(w):
    f = h*i
    s = (w-i)*floor(h/2)
    t = (w-i)*ceil(h/2)
    dif = max(abs(f-s),abs(s-t),abs(t-f))
    if dif < min_dif:
        min_dif = dif
for i in range(h):
    f = w*i
    s = (h-i)*floor(w/2)
    t = (h-i)*ceil(w/2)
    dif = max(abs(f-s),abs(s-t),abs(t-f))
    if dif < min_dif:
        min_dif = dif
print(min_dif)

Submission status: Wrong Answer

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

if h%3 == 0 or w%3 == 0:
	print(0)
else:
	h_r, w_r = round(h/3), round(w/3)
	# print(h, w, h_r, w_r)
	hw = [h_r*w, (h-h_r)*(w//2), (h-h_r)*(w-w//2)]
	wh = [w_r*h, (w-w_r)*(h//2), (w-w_r)*(h-h//2)]
	# print(hw, wh)
	print(min(max(hw)-min(hw), max(wh)-min(wh)))