Problem:


Score : 400 points

Problem Statement

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

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

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

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

Sample Input 1

3 5

Sample Output 1

0

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

Sample Input 2

4 5

Sample Output 2

2

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

Sample Input 3

5 5

Sample Output 3

4

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

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000



Sample input:
3 5


Sample output:
0

Submission status: Wrong Answer

Solution code:
import math
H, W = map(int, input().split())
ans = 10**8
for h in range(1, H-1):
    A = h * W
    #残りを横に割る
    B = W*math.floor((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    #残りを縦に割る
    B = (H-h)*math.floor(W/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)

# 縦横入れ替え
H, W = W, H
for h in range(1, H-1):
    A = h * W
    #残りを横に割る
    B = W*math.floor((H-h)/2)
    C = H*W - A - B
    tmp = max([A,B,C]) - min([A,B,C])
    ans = min(ans, tmp)
    #残りを縦に割る
    B = (H-h)*math.floor(W/2)
    C = H*W - A - B
    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())

d = 10**9

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

print(d)

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]
S = H*W
if H > W:
    H, W = W, H

def solveT(H, W):
    # H < W
    h0,h1 = H // 2, H //2 + H%2
    m = H
    for w in range(1, W):
        s0 = w*H
        w0 = W-w
        m = min([m, abs(s0 - w0*h0), abs(s0-w0*h1)])
    return m


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

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]
S = H*W
if H > W:
    H, W = W, H

def solveT(H, W):
    # H < W
    h0,h1 = H // 2, H //2 + H%2
    m = H
    for w in range(1, W):
        s0 = w*H
        w0 = W-w
        m = min([m, abs(s0 - w0*h0), abs(s0-w0*h1)])
    return m


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

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]
S = H*W
if H > W:
    H, W = W, H

def solveT(H, W):
    # H < W
    h0,h1 = H // 2, H //2 + H%2
    m = H
    for w in range(1, W):
        s0 = w*H
        w0 = W-w
        m = min([m, abs(s0 - w0*h0), abs(s0-w0*h1)])
    return m


if H % 3 == 0 or W % 3 == 0:
    print(0)
elif H == 1:
    print(1)
else:
    print(solveT(H, W), solveT(W, H))

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]
S = H*W
if H > W:
    H, W = W, H

def solveT(H, W):
    # H < W
    h0,h1 = H // 2, H //2 + H%2
    m = H
    for w in range(1, W):
        s0 = w*H
        w0 = W-w
        m = min([m, abs(s0 - w0*h0), abs(s0-w0*h1)])
    return m


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

Submission status: Wrong Answer

Solution code:
H, W = [int(x) for x in input().split()]
S = H*W
if H > W:
    H, W = W, H

def solveH(H, W):
    if H > W:
        H, W = W, H
    return H 

def solveT(H, W):
    # H < W
    h = H // 2
    return min(abs((w*H) - (W-w)*h) for w in range(1, W))


if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    print(min([solveH(H, W), solveT(H, W)]))

Submission status: Wrong Answer

Solution code:
# -*- coding: UTF-8 -*-
# ABC062_C
import math

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

if (H % 3 == 0 or W % 3 == 0):
    print(0)
elif(H == 2 and W == 2):
    print(1)
else:
    Smin = H * W
    for x in range(1, W - 1):
        y = math.ceil(H / 2)
        area = []
        area.append(x * H)
        area.append((W - x) * y)
        area.append((W - x) * (H - y))
        tmp_min = max(area) - min(area)
        if (tmp_min < Smin):
            Smin = tmp_min
    if (H != W):
        H, W = W, H
        for x in range(1, W - 1):
            y = math.ceil(H / 2)
            area = []
            area.append(x * H)
            area.append((W - x) * y)
            area.append((W - x) * (H - y))
            tmp_min = max(area) - min(area)
            if (tmp_min < Smin):
                Smin = tmp_min

    print(Smin)

Submission status: Wrong Answer

Solution code:
# -*- coding: UTF-8 -*-
# ABC062_C
import math

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

if (H % 3 == 0 or W % 3 == 0):
    print(0)
elif(H == 2 and W == 2):
    print(1)
else:
    Smin = H * W
    for x in range(1, math.ceil(W / 2)):
        y = math.ceil(H / 2)
        area = []
        area.append(x * H)
        area.append((W - x) * y)
        area.append((W - x) * (H - y))
        tmp_min = max(area) - min(area)
        if (tmp_min < Smin):
            Smin = tmp_min
    if (H != W):
        H, W = W, H
        for x in range(1, math.ceil(W / 2)):
            y = math.ceil(H / 2)
            area = []
            area.append(x * H)
            area.append((W - x) * y)
            area.append((W - x) * (H - y))
            tmp_min = max(area) - min(area)
            if (tmp_min < Smin):
                Smin = tmp_min

    print(Smin)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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 = min(h, w), max(h, w)
    s1 = h * round(w / 3)
    w -= round(w / 3)
    if h % 2 == 0 or w % 2 == 0:
        s2 = h * w // 2
        s3 = s2
    else:
        h, w = min(h, w), max(h, w)
        s2 = h * (w // 2)
        s3 = h * (w // 2 + 1)
    print(max(s1, s2, s3) - min(s1, s2, s3))

Submission status: Wrong Answer

Solution code:
import math
a,b = map(int, input().split())
H, W = min(a,b),max(a,b)

if H%3 == 0 or W%3 == 0:
  print(0)
  exit()
ans = H*W
for y in range(H//2+1):
  for x in range(W//2+1):
    Smin = y*x
    Smax = H * (W-x)
    tmp = Smax - Smin
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

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

Submission status: Wrong Answer

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


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


main()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:


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

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

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=h*w
for i in range(1,h):ans=min(ans,max(i*w,(h-i)*(w//2),(h-i)*((w-1)//2)+1)-min(i*w,(h-i)*(w//2),(h-i)*((w-1)//2)+1))
for i in range(1,w):ans=min(ans,max(i*h,(w-i)*(h//2),(w-i)*((h-1)//2)+1)-min(i*h,(w-i)*(h//2),(w-i)*((h-1)//2)+1))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=h*w
for i in range(1,h):ans=min(ans,max(i*w,(h-i)*w//2)-min(i*w,(h-i)*w//2))
for i in range(1,w):ans=min(ans,max(i*h,(w-i)*h//2)-min(i*h,(w-i)*h//2))
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")
    A = 0
    B = 0
    C = 0

    # W方向から切る場合

    ## 1/3より少し小さく切る

    A = (W//3) * H

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

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

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

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

    newH = W
    newW = H

    H = newH
    W = newW

    A = (W//3) * H

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

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

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

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


    print (ans)

Submission status: Wrong Answer

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

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

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

tmp=w
w=h
h=tmp
# print(w)
# print(h)

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

tmp=w
w=h
h=tmp
# print(w)
# print(h)

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=[]
if h==1 and w%3==0:
    print(0)
    exit()
elif w==1 and h%3==0:
    print(0)
    exit()
elif w==1 or h==1:
    print(1)
    exit()
for i in range(1,h):
    s=[]
    j=w//2
    s.append(i*w)
    s.append((h-i)*j)
    s.append((h-i)*(w-j))
    l.append(abs(max(s)-min(s)))
    ss=[]
    k=(h-i)//2
    ss.append(i*w)
    ss.append(w*k)
    ss.append(w*(h-i-k))
    l.append(abs(max(ss)-min(ss)))
for i in range(1,w):
    s=[]
    j=h//2
    s.append(i*h)
    s.append((w-i)*j)
    s.append((w-i)*(h-i))
    l.append(abs(max(s)-min(s)))
    ss=[]
    k=(w-i)//2
    ss.append(i*h)
    ss.append(h*k)
    ss.append(h*(w-i-k))
    l.append(abs(max(ss)-min(ss)))
print(min(l))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=[]
if h==1 or w==1:
    print(1)
    exit()
for i in range(1,h):
    s=[]
    j=w//2
    s.append(i*w)
    s.append((h-i)*j)
    s.append((h-i)*(w-j))
    l.append(abs(max(s)-min(s)))
    ss=[]
    k=(h-i)//2
    ss.append(i*w)
    ss.append(w*k)
    ss.append(w*(h-i-k))
    l.append(abs(max(ss)-min(ss)))
for i in range(1,w):
    s=[]
    j=h//2
    s.append(i*h)
    s.append((w-i)*j)
    s.append((w-i)*(h-i))
    l.append(abs(max(s)-min(s)))
    ss=[]
    k=(w-i)//2
    ss.append(i*h)
    ss.append(h*k)
    ss.append(h*(w-i-k))
    l.append(abs(max(ss)-min(ss)))
print(min(l))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
l=[]
for i in range(1,h):
    s=[]
    j=w//2
    s.append(i*w)
    s.append((h-i)*j)
    s.append((h-i)*(w-j))
    l.append(abs(max(s)-min(s)))
    ss=[]
    k=(h-i)//2
    ss.append(i*w)
    ss.append(w*k)
    ss.append(w*(h-i-k))
    l.append(abs(max(ss)-min(ss)))
for i in range(1,w):
    s=[]
    j=h//2
    s.append(i*h)
    s.append((w-i)*j)
    s.append((w-i)*(h-i))
    l.append(abs(max(s)-min(s)))
    ss=[]
    k=(w-i)//2
    ss.append(i*h)
    ss.append(h*k)
    ss.append(h*(w-i-k))
    l.append(abs(max(ss)-min(ss)))
print(min(l))

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

ans1=10**19
for i in range(1,h):
	s1=i*w	
	if w%2==0:
		s2=(h-i)*(w/2)
		s3=(h-i)*(w/2)
	else:
		s2=(h-i)*((w+1)/2)
		s3=(h-i)*((w-1)/2)
		
	if (h-i)%2==0:
		s4=((h-i)/2)*w
		s5=((h-i)/2)*w
	else:
		s4=(((h-i)+1)/2)*w
		s5=(((h-i)-1)/2)*w
		
		
	a1 = max([s1,s2,s3])
	b1 = min([s1,s2,s3])
	a2 = max([s1,s4,s5])
	b2 = min([s1,s4,s5])
	
	ans1 = min([ans1,a1-b1,a2-b2])

ans2=10**10
for i in range(1,w):
	s1=i*h
		
	if h%2==0:
		s2=(w-i)*(h/2)
		s3=(w-i)*(h/2)
	else:
		s2=(w-i)*((h+1)/2)
		s3=(w-i)*((h-1)/2)
		
	if (w-i)%2==0:
		s4=((w-i)/2)*h
		s5=((w-i)/2)*h
	else:
		s4=(((w-i)+1)/2)*h
		s5=(((w-i)+1)/2)*h
			
	a1=max([s1,s2,s3])
	b1=min([s1,s2,s3])
	a2=max([s1,s4,s5])
	b2=min([s1,s4,s5])
	ans2 = min([ans2,a1-b1,a2-b2])		
	
print(int(min(ans1,ans2)))

Submission status: Wrong Answer

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

tmp = 999999999999999


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

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


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

print(tmp)

Submission status: Wrong Answer

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

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

#n,m = ips()
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(m)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#a.pop(0)は10^5あるときTLEしやすい!注意!抜くなら逆ソート!

x,y = ips()
if x%3 == 0 or y%3 == 0:
    print(0)
    exit(0)
else:
    ans = min(x,y)
if x*y == 4:
    print(1)
    exit(0)
else:
    e = min(x,y)
    f = max(x,y)
    if e%2:
        a = e//2+1
        b = e//2
    else:
        a,b = e//2,e//2
    a*=f
    b*=f
c = 0
ans = rem
for i in range(f):
    c+=e
    if e%2:
        a-=e//2+1
        b-=e//2
    else:
        a-=e//2
        b-=e//2
    ans  = min(ans,max(a,b,c)-min(a,b,c))

e = max(x,y)
f = min(x,y)
if e%2:
    a = e//2+1
    b = e//2
else:
    a,b = e//2,e//2
a*=f
b*=f
c = 0
for i in range(f):
    c+=e
    if e%2:
        a-=e//2+1
        b-=e//2
    else:
        a-=e//2
        b-=e//2
    ans  = min(ans,max(a,b,c)-min(a,b,c))
print(ans)

Submission status: Wrong Answer

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

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

#n,m = ips()
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(m)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#a.pop(0)は10^5あるときTLEしやすい!注意!抜くなら逆ソート!

x,y = ips()
if x%3 == 0 or y%3 == 0:
    print(0)
    exit(0)
else:
    ans = min(x,y)
if x*y == 4:
    print(1)
    exit(0)
else:
    e = min(x,y)
    f = max(x,y)
    if e%2:
        a = e//2+1
        b = e//2
    else:
        a,b = e//2,e//2
    a*=f
    b*=f
c = 0
ans = rem
for i in range(f):
    c+=e
    if e%2:
        a-=e//2+1
        b-=e//2
    else:
        a-=e//2
        b-=e//2
    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())
#N=input()
ans=10**30
if H==2 and W==2:
    print(1)
    quit()
if H==2 or W==2:
    H,W=min(H,W),max(H,W)
    ans=max([W,W//2,W-W//2])-min([W,W//2,W-W//2])
    a=W//3
    b=(W-a)//2
    c=W-a-b
    ans=min(ans,2*max([a,b,c])-2*min([a,b,c]))
    print(ans)
    quit()
a=H//3
b=(H-a)//2
c=H-a-b

ans=min(ans,W*(max([a,b,c])-min([a,b,c])))
for a in range(1,H-1):
    h=H-a
    b=W//2
    c=W-b
    ans=min(ans,max([a*W,h*b,h*c])-min([a*W,h*b,h*c]))
W,H=H,W
a=H//3
b=(H-a)//2
c=H-a-b

ans=min(ans,W*(max([a,b,c])-min([a,b,c])))
for a in range(1,H-1):
    h=H-a
    b=W//2
    c=W-b
    ans=min(ans,max([a*W,h*b,h*c])-min([a*W,h*b,h*c]))

print(ans)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
#N=input()
ans=10**30
if H==2 and W==2:
    print(1)
    quit()
if H==2 or W==2:
    H,W=min(H,W),max(H,W)
    ans=max([W,W//2,W-W//2])-min([W,W//2,W-W//2])
    a=W//3
    b=(H-a)//2
    c=H-a-b
    ans=min(ans,2*(max([a,b,c])-min([a,b,c])))
    print(ans)
    quit()
a=H//3
b=(H-a)//2
c=H-a-b

ans=min(ans,W*(max([a,b,c])-min([a,b,c])))
for a in range(1,H-1):
    h=H-a
    b=W//2
    c=W-b
    ans=min(ans,max([a*W,h*b,h*c])-min([a*W,h*b,h*c]))
W,H=H,W
a=H//3
b=(H-a)//2
c=H-a-b

ans=min(ans,W*(max([a,b,c])-min([a,b,c])))
for a in range(1,H-1):
    h=H-a
    b=W//2
    c=W-b
    ans=min(ans,max([a*W,h*b,h*c])-min([a*W,h*b,h*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()

if 0 == H % 2 and 0 != W % 2:
	t = W
	W = H
	H = t

harf_h, harf_w = H // 2, W // 2

last_diff = abs(harf_h * harf_w - (H - harf_h) * W)
while harf_h < H:
	harf_h += 1
	diff = abs(harf_h * harf_w - (H - harf_h) * W)
	if last_diff < diff:
		print(last_diff)
		exit()
	last_diff = diff

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(H: int, W: int):
    if H % 3 == 0 or W % 3 == 0:
        ret = 0
    else:
        ret = H * W
        for w in range(1, W):
            a = w * H
            if H % 2 == 0:
                tmp = abs(a - (W - w) * H // 2)
            else:
                b = (W - w) * (H // 2)
                c = (W - w) * (H // 2 + 1)
                tmp = max(abs(a - b),
                          abs(a - c),
                          abs(c - b))
            #print(w, tmp)
            ret = min(ret, tmp)

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


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # 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
sys.setrecursionlimit(300000)


def solve(H: int, W: int):
    if H % 3 == 0 or W % 3 == 0:
        ret = 0
    else:
        ret = H * W
        for w in range(1, W):
            a = w * H
            if H % 2 == 0:
                tmp = abs(a - (W - w) * H // 2)
            else:
                tmp = max(abs(a - (W - w) * (H // 2)),
                          abs(a - (W - w) * (H // 2 + 1)))
            #print(w, tmp)
            ret = min(ret, tmp)

        W, H = H, W
        for w in range(1, W):
            a = w * H
            if H % 2 == 0:
                tmp = abs(a - (W - w) * H // 2)
            else:
                tmp = max(abs(a - (W - w) * (H // 2)),
                          abs(a - (W - w) * (H // 2 + 1)))
            #print(w, tmp)
            ret = min(ret, tmp)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # 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
sys.setrecursionlimit(300000)


def solve(H: int, W: int):
    if W < H:
        H, W = W, H

    if H % 3 == 0 or W % 3 == 0:
        ret = 0
    else:
        ret = H * W
        for w in range(1, W):
            a = w * H
            if H % 2 == 0:
                tmp = abs(a - (W - w) * H // 2)
            else:
                tmp = max(abs(a - (W - w) * (H // 2)),
                          abs(a - (W - w) * (H // 2 + 1)))
            #print(w, tmp)
            ret = min(ret, tmp)
        ##a = (W // 3) * H
        ##if H % 2 == 0:
        ##    tmp = abs(a - (W - (W // 3)) * (H // 2))
        ##else:
        ##    tmp = max(abs(a - (W - (W // 3)) * (H // 2)),
        ##              abs(a - (W - (W // 3)) * (H // 2 + 1)))

        ##tw = W // 3 + 1
        ##a = tw * H
        ##if H % 2 == 0:
        ##    tmp2 = abs(a - (W - tw) * (H // 2))
        ##else:
        ##    tmp2 = max(abs(a - (W - tw) * (H // 2)),
        ##               abs(a - (W - tw) * (H // 2 + 1)))
        ##ret = min(tmp, tmp2)
    print(ret)
    return


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

if __name__ == '__main__':
    main()