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:
#!/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:
        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()

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
d = h * w
S = []
if (h % 3 == 0) or (w % 3 == 0):
    print(0)
else:
    i = 1
    a = 0
    b = 1
    while i < h:
        a = w * i
        b = (w // 2 + w % 2) * (h - i)
        c = (w // 2) * (h - i)
        abc = [a, b, c]
        S += [max(abc) - min(abc)]
        i += 1

    i = 1
    a = 0
    b = 1
    while i < w:
        a = h * i
        b = (h // 2 + h % 2) * (w - i)
        c = (h // 2) * (w - i)
        abc = [a, b, c]
        S += [max(abc) - min(abc)]
        i += 1

    print(min(S))

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
d = h * w
S = []
if (h % 3 == 0) or (w % 3 == 0):
    print(0)
else:

    i = 1
    a = 0
    b = 1
    while a < b:
        a = w * i
        b = (w // 2 + w % 2) * (h - i)
        c = (w // 2) * (h - i)
        abc = [a, b, c]
        S += [max(abc) - min(abc)]
        i += 1

    i = 1
    a = 0
    b = 1
    while a < b:
        a = h * i
        b = (h // 2 + h % 2) * (w - i)
        c = (h // 2) * (w - i)
        abc = [a, b, c]
        S += [max(abc) - min(abc)]
        i += 1

    print(min(S))

Submission status: Wrong Answer

Solution code:
import sys

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

A = 0
B = 0
C = 0
Aw = 0
Bw = 0
Cw = 0
Ah = 0
Bh = 0
Ch = 0
Sa = 0
Sb = 0
Sc = 0
Smax = 0
Smin = 0
Sm = 0

#1
Aw = W//3
Bw = (W - Aw) // 2
Cw = Aw - Bw
if Aw == Bw and Bw == Cw:
    print(0)
    sys.exit()
else:
    Sm = (max([Aw,Bw,Cw]) - min([Aw,Bw,Cw])) * H

#2
Ah = H//3
Bh = (H - Ah) // 2
Ch = H - Ah - Bh
if Ah == Bh and Bh == Ch:
    print(0)
    sys.exit()
else:
    if (max([Ah,Bh,Ch]) - min([Ah,Bh,Ch])) * W < Sm:
        Sm = (max([Ah,Bh,Ch]) - min([Ah,Bh,Ch])) * W

#3
Ah = H//2
Bh = H - Ah
Ch = H
for i in range(W):
    Aw = i
    Bw = i
    Cw = W - Aw
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

#4
Aw = W//2
Bw = W - Aw
Cw = W
for j in range(H):
    Ah = j
    Bh = j
    Ch = H - Ah
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

print(Sm)

Submission status: Wrong Answer

Solution code:
import sys

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

A = 0
B = 0
C = 0
Aw = 0
Bw = 0
Cw = 0
Ah = 0
Bh = 0
Ch = 0
Sa = 0
Sb = 0
Sc = 0
Smax = 0
Smin = 0
Sm = 0

#1
Aw = W//3
Bw = (W - Aw) // 2
Cw = Aw - Bw
if Aw == Bw and Bw == Cw:
    print(0)
    sys.exit()
else:
    Sm = (max([Aw,Bw,Cw]) - min([Aw,Bw,Cw])) * H

#2
Ah = H//3
Bh = (H - Ah) // 2
Ch = H - Ah - Bh
if Ah == Bh and Bh == Ch:
    print(0)
    sys.exit()
else:
    if (max([Ah,Bh,Ch]) - min([Ah,Bh,Ch])) * W < Sm:
        Sm = (max([Ah,Bh,Ch]) - min([Ah,Bh,Ch])) * W

#3
Ah = H//2
Bh = H - Ah
Ch = H
for i in range(W):
    Aw = i
    Bw = i
    Cw = W - Aw
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

#4
Aw = W//2
Bw = W - Aw
Cw = W
for i in range(H):
    Ah = i
    Bh = i
    Ch = H - Ah
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

print(Sm)

Submission status: Wrong Answer

Solution code:
import sys

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

A = 0
B = 0
C = 0
Aw = 0
Bw = 0
Cw = 0
Ah = 0
Bh = 0
Ch = 0
Sa = 0
Sb = 0
Sc = 0
Smax = 0
Smin = 0
Sm = 0

#1
Aw = W//3
Bw = (W - Aw) // 2
Cw = Aw - Bw
if Aw == Bw and Bw == Cw:
    print(0)
    sys.exit()
else:
    Sm = abs(Cw - Aw) * H

#2
Ah = H//3
Bh = (H - Ah) // 2
Ch = H - Ah - Bh
if Ah == Bh and Bh == Ch:
    print(0)
    sys.exit()
else:
    if abs(Ch - Ah) * W < Sm:
        Sm = abs(Ch - Ah) * W

#3
Ah = H//2
Bh = H - Ah
Ch = H
for i in range(W):
    Aw = i
    Bw = i
    Cw = W - Aw
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

#4
Aw = W//2
Bw = W - Aw
Cw = W
for i in range(H):
    Ah = i
    Bh = i
    Ch = H - Ah
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

print(Sm)

Submission status: Wrong Answer

Solution code:
import sys

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

A = 0
B = 0
C = 0
Aw = 0
Bw = 0
Cw = 0
Ah = 0
Bh = 0
Ch = 0
Sa = 0
Sb = 0
Sc = 0
Smax = 0
Smin = 0
Sm = 0

#1
Aw = W//3
Bw = (W - Aw) // 2
Cw = Aw - Bw
if Aw == Bw and Bw == Cw:
    print(0)
    sys.exit()
elif Ah != 0 and Bh != 0 and Ch != 0:
    Sm = abs(Cw - Aw) * H

#2
Ah = H//3
Bh = (H - Ah) // 2
Ch = H - Ah - Bh
if Ah == Bh and Bh == Ch:
    print(0)
    sys.exit()
elif Ah != 0 and Bh != 0 and Ch != 0:
    if abs(Ch - Ah) * W < Sm:
        Sm = abs(Ch - Ah) * W

#3
Ah = H//2
Bh = H - Ah
Ch = H
for i in range(W):
    Aw = i
    Bw = i
    Cw = W - Aw
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

#4
Aw = W//2
Bw = W - Aw
Cw = W
for i in range(H):
    Ah = i
    Bh = i
    Ch = H - Ah
    Sa = Ah * Aw
    Sb = Bh * Bw
    Sc = Ch * Cw
    Smax = max([Sa,Sb,Sc])
    Smin = min([Sa,Sb,Sc])
    if Smax - Smin < Sm:
        Sm = Smax - Smin
    if Sm == 0:
        print(0)
        sys.exit()

print(Sm)

Submission status: Wrong Answer

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

def side(h, W):
    Sa = W * h
    Sb = W * ((H-h)//2)
    Sc = W * ((H-h+1)//2)
    # print (Sa, Sb, Sc)
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)

def vertical(h, W):
    Sa = W * h
    Sb = W//2 * (H-h)
    Sc = ((W+1)//2) * (H-h)
    # print (Sa, Sb, Sc)
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)


def judge(H, W):
    ans = H * W
    for h in range(0, H):
        # print (side(h, W), vertical(h, W))
        ans = min(ans, side(h, W), vertical(h, W))
        # print (h, W, ans)
    return ans

print (min(judge(H, W), judge(W, H)))

# print (judge(3, 5))

Submission status: Wrong Answer

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

def side(h, W):
    Sa = W * h
    Sb = W * ((H-h)//2)
    Sc = W * ((H-h+1)//2)
    # print (Sa, Sb, Sc)
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)

def vertical(h, W):
    Sa = W * h
    Sb = W//2 * (H-h)
    Sc = ((W+1)//2) * (H-h)
    # print (Sa, Sb, Sc)
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)


def judge(H, W):
    ans = H * W
    for h in range(1, H):
        # print (side(h, W), vertical(h, W))
        ans = min(ans, side(h, W), vertical(h, W))
        # print (h, W, ans)
    return ans

print (min(judge(H, W), judge(W, H)))

# print (judge(3, 5))

Submission status: Wrong Answer

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

def side(h, W):
    Sa = W * h
    Sb = W * ((H-h)//2)
    Sc = W * ((H-h+1)//2)
    # print (Sa, Sb, Sc)
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)

def vertical(h, W):
    Sa = W * h
    Sb = W//2 * (H-h)
    Sc = ((W+1)//2) * (H-h)
    # print (Sa, Sb, Sc)
    return max(Sa, Sb, Sc) - min(Sa, Sb, Sc)


def judge(H, W):
    ans = H * W
    for h in range(1, H-1):
        # print (side(h, W), vertical(h, W))
        ans = min(ans, side(h, W), vertical(h, W))
        # print (h, W, ans)
    return ans

print (min(judge(H, W), judge(W, H)))

# print (judge(3, 5))

Submission status: Wrong Answer

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

if H%3 == 0 or W%3 == 0:
    print (0)
elif H == W:
    if H%2 == 0:
        print (H//2)
    else:
        print (H-((2 * H)//6 - H//6))
elif H%3 == 2 and W%3 == 2:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        else:
            print (min(H, W))
elif H%3 == 1 and W%3 == 1:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        else:
            print (min(H,W))
else:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        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)
elif H == W:
    if H%2 == 0:
        print (H%2)
    else:
        print (H-((2 * H)//6 - H//6))
elif H%3 == 2 and W%3 == 2:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        else:
            print (min(H, W))
elif H%3 == 1 and W%3 == 1:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        else:
            print (min(H,W))
else:
    if H%2 == 0 and W%2 == 0:
        print (min(H//2, W//2))
    else:
        if H%2 == 0:
            print (H//2)
        elif W%2 == 0:
            print (W//2)
        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:
    if h%2==0 and w%2==0:h,w=max(h,w),min(h,w)
    elif h%2==0:h,w=w,h
    elif w%2==1:h,w=min(h,w),max(h,w)
    half_w=w//2
    rest_w=w-half_w
    ans=10**10
    for i in range(1,h):
        s=w*i
        t=half_w*(h-i)
        u=(w-half_w)*(h-i)
        sub=max(s,t,u)-min(s,t,u)
        if sub < ans:
            ans=sub
        else:break
    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:
    if h%2==0 and w%2==0:h,w=max(h,w),min(h,w)
    elif h%2==0:h,w=w,h
    elif w%2==1:h,w=max(h,w),min(h,w)
    half_w=w//2
    rest_w=w-half_w
    ans=10**10
    for i in range(1,h):
        s=w*i
        t=half_w*(h-i)
        u=(w-half_w)*(h-i)
        sub=max(s,t,u)-min(s,t,u)
        if sub < ans:
            ans=sub
        else:break
    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:
    if h%2==0 and w%2==0:h,w=max(h,w),min(h,w)
    elif h%2==0:h,w=w,h
    half_w=w//2
    rest_w=w-half_w
    ans=10**10
    for i in range(1,h):
        s=w*i
        t=half_w*(h-i)
        u=(w-half_w)*(h-i)
        sub=max(s,t,u)-min(s,t,u)
        if sub < ans:
            ans=sub
        else:break
    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:
    if h%2==0:h,w=w,h
    half_w=w//2
    rest_w=w-half_w
    ans=10**10
    for i in range(1,h):
        s=w*i
        t=half_w*(h-i)
        u=(w-half_w)*(h-i)
        sub=max(s,t,u)-min(s,t,u)
        if sub < ans:
            ans=sub
        else:break
    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:
    half_w=w//2
    rest_w=w-half_w
    ans=10**10
    for i in range(1,h):
        s=w*i
        t=half_w*(h-i)
        u=(w-half_w)*(h-i)
        sub=max(s,t,u)-min(s,t,u)
        if sub < ans:
            ans=sub
        else:break
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

H,W = IL()

ans = MAX_INT
if H%3 == 0 or W%3 == 0:
  print(0)
else:
  for i in range(1,W):
    a = H*i
    b = (W-i)*(H//2)
    c = H*W-(a+b)
    ans = min(ans,max(a,b,c)-min(a,b,c))
  for i in range(1,H):
    a = W*i
    b = (H-i)*(W//2)
    c = H*W-(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()))

if (H*W)%3 == 0:
  print(0)
  exit()
  
if H == W == 2:
  print(1)
  exit()

x = []  
for i in range(1,H-1):
  a = i * W
  b = (H - i) * (W // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

for j in range(1,W-1):
  a = j * H
  b = (W - j) * (H // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

print(min(x))

Submission status: Wrong Answer

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

if (H*W)%3 == 0:
  print(0)
  exit()
  
elif H < 3 and W < 3:
  print(0)
  exit()

x = []  
for i in range(1,H-1):
  a = i * W
  b = (H - i) * (W // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

for j in range(1,W-1):
  a = j * H
  b = (W - j) * (H // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

print(min(x))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    x = []
    for h in range(1, H):
        x.append([h * W, (H - h) * (W // 2), (H - h) * (W - W // 2)])

    for w in range(1, W):
        x.append([H * w, (W - w) * (H // 2), (W - w) * (H - H // 2)])
    print(min(max(v) - min(v) for v in x))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    s = min(H, W)
    l = max(H, W)
    print(abs(s * (l // 3) - (s // 2) * (l - l // 3)))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    s = min(H, W)
    l = max(H, W)
    print(s * (l // 2) - (s // 2) * (l - l // 2))

Submission status: Wrong Answer

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

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

area = h * w / 3
min_diff = float("inf")

for h1 in range(1, h):
    if area // h1 > w:
        continue
    else:
        w1s = [int(area // h1), int(area // h1) + 1]

    for w1 in w1s:
        if w1 == w:
            continue
        else:
            h2 = h - h1
            w2s = [w1, w]

        for w2 in w2s:
            w3 = w - w1
            if w2 == w1:
                h3 = h
            else:
                h3 = h1

            areas = [h1 * w1, h2 * w2, h3 * w3]
            if max(areas) - min(areas) < min_diff:
                min_diff = max(areas) - min(areas)

print(min_diff)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
ans = 10 ** 10
for x in range(1, w):
    blocks = [h * x, h // 2 * (w - x), (h - h // 2) * (w - x)]
    ans = min(ans, max(blocks) - min(blocks))
for y in range(1, h):
    blocks = [w * y, w // 2 * (h - y), (w - w // 2) * (h - y)]
    ans = min(ans, max(blocks) - min(blocks))
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()
ans = min(h % 3 * w , w % 3 * h)
for x in range(1, w):
    blocks = [h * x, h // 2 * (w - x), (h - h // 2) * (w - x)]
    ans = min(ans, max(blocks) - min(blocks))
for y in range(1, h):
    blocks = [w * y, w // 2 * (h - y), (w - w // 2) * (h - y)]
    ans = min(ans, max(blocks) - min(blocks))
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()
ans = min(h % 3 * w , w % 3 * h)
for x in range(1, w):
    blocks = [h * x, (h // 2 + h % 2) * (w - x), h // 2 * (w - x)]
    ans = min(ans, max(blocks) - min(blocks))
for y in range(1, h):
    blocks = [w * y, (w // 2 + w % 2) * (h - y), w // 2 * (h - y)]
    ans = min(ans, max(blocks) - min(blocks))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

ans = 10**10

for i in range(L):
    a = (i+1) * S
    b = (L-i-1) * (S//2)
    c = (L-i-1) * (S//2 + S%2)
    diff = max(abs(a-b), abs(a-c), c-b)
    ans = min(ans, diff)

ans = min(ans, S)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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


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

ans = 10**10

for i in range(L):
    a = (i+1) * S
    b = (L-i-1) * (S//2)
    c = (L-i-1) * (S//2 + S%2)
    diff = max(abs(a-b), abs(a-c), c-b)
    ans = min(ans, diff)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if n*m % 3 == 0:
  print("0")
elif n == 1 or m == 1:
  print("1")
else:
  a = n//3+1
  b = m//3+1
  c = a*m-float(n*m/3)
  d = b*n-float(n*m/3)
  e = c-m
  f = d-n
  if c<d:
    smax = a*m
    if m % 2 == 1:
      smin = (n-a)*(m-1)/2
    else:
      smin = (n-a)*(m/2)
  else:
    smax = b*n
    if n % 2 == 1:
      smin = (m-b)*(n-1)/2
    else:
      smin = (m-b)*(n/2)
  if f<e:
    smin2 = smax - m
    if m % 2 == 1:
      smax2 = (n-a+1)*(m+1)/2
    else:
      smax2 = (n-a+1)*(m/2)
  else:
    smin2 = smax - n
    if m % 2 == 1:
      smax2 = (m-b+1)*(n+1)/2
    else:
      smax2 = (m-b+1)*n/2
  print (min(min(n,m),min(int(smax-smin),int(smax2-smin2))))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if n*m % 3 == 0:
  print("0")
elif n == 1 or m == 1:
  print("1")
else:
  a = n//3+1
  b = m//3+1
  c = a*m-float(n*m/3)
  d = b*n-float(n*m/3)
  e = c-m
  f = d-n
  if c<d:
    smax = a*m
    if m % 2 == 1:
      smin = (n-a)*(m-1)/2
    else:
      smin = (n-a)*(m/2)
  else:
    smax = b*n
    if n % 2 == 1:
      smin = (m-b)*(n-1)/2
    else:
      smin = (m-b)*(n/2)
  if f<e:
    smin2 = smax - m
    if m % 2 == 1:
      smax2 = (n-a+1)*(m+1)/2
    else:
      smax2 = (n-a+1)*(m/2)
  else:
    smin2 = smax - n
    if m % 2 == 1:
      smax2 = (m-b+1)*(n+1)/2
    else:
      smax2 = (m-b+1)*n/2
  print (min(int(smax-smin),int(smax2-smin2)))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if n*m % 3 == 0:
  print("0")
elif n == 1 or m == 1:
  print("1")
else:
  a = n//3+1
  b = m//3+1
  c = a*m-float(n*m/3)
  d = b*n-float(n*m/3)
  e = c-m
  f = d-n
  if c<d:
    smax = a*m
    if m % 2 == 1:
      smin = (n-a)*(m-1)/2
    else:
      smin = (n-a)*(m/2)
  else:
    smax = b*n
    if n % 2 == 1:
      smin = (m-b)*(n-1)/2
    else:
      smin = (m-b)*(n/2)
  if f<e:
    smin2 = smax - m
    if m % 2 == 1:
      smax2 = (n-a+1)*(m+1)/2
    else:
      smax2 = (n-a+1)*(m/2)
  else:
    smin2 = smax - n
    if m % 2 == 1:
      smax2 = (m-b+1)*(n+1)/2
    else:
      smax2 = (m-b+1)*n/2
  print (int(smax-smin))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if n*m % 3 == 0:
  print("0")
elif n == 1 or m == 1:
  print("1")
else:
  a = n//3+1
  b = m//3+1
  c = a*m-float(n*m/3)
  d = b*n-float(n*m/3)
  if c<d:
    smax = a*m
    if m % 2 == 1:
      smin = (n-a)*(m-1)/2
    else:
      smin = (n-a)*(m/2)
  else:
    smax = b*n
    if n % 2 == 1:
      smin = (m-b)*(n-1)/2
    else:
      smin = (m-b)*(n/2)
  print (int(smax-smin))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if n*m % 3 == 0:
  print("0")
else:
  a = n//3+1
  b = m//3+1
  c = a*m-float(n*m/3)
  d = b*n-float(n*m/3)
  if c<d:
    smax = a*m
    if m % 2 == 1:
      smin = (n-a)*(m-1)/2
    else:
      smin = (n-a)*(m/2)
  else:
    smax = b*n
    if n % 2 == 1:
      smin = (m-b)*(n-1)/2
    else:
      smin = (m-b)*(n/2)
  print (int(smax-smin))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
if n*m % 3 == 0:
  print("0")
else:
  a = n//3+1
  b = m//3+1
  c = float(n*m/3)-a*m
  d = float(n*m/3)-b*n
  if c<d:
    smax = a*m
    if m % 2 == 1:
      smin = (n-a)*(m-1)/2
    else:
      smin = (n-a)*(m/2)
  else:
    smax = b*n
    if n % 2 == 1:
      smin = (m-b)*(n-1)/2
    else:
      smin = (m-b)*(n/2)
  print (int(smax-smin))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = n//3+1
b = m//3+1
c = float(n*m/3)-a*m
d = float(n*m/3)-b*n
if c>d:
  smax = a*m
  if m % 2 == 1:
    smin = (n-a)*(m-1)/2
  else:
    smin = (n-a)*(m/2)
else:
  smax = b*n
  if n % 2 == 1:
    smin = (m-b)*(n-1)/2
  else:
    smin = (m-b)*(n/2)
print (int(smax-smin))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = n//3+1
b = m//3+1
c = float(n*m/3)-a*m
d = float(n*m/3)-b*n
if c<d:
  smax = a*m
  if m % 2 == 1:
    smin = (n-a)*(m-1)/2
  else:
    smin = (n-a)*(m/2)
else:
  smax = b*n
  if n % 2 == 1:
    smin = (m-b)*(n-1)/2
  else:
    smin = (m-b)*(n/2)
print (int(smax-smin))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = n//3+1
b = m//3+1
c = float(n*m/3)-a*m
d = float(n*m/3)-b*n
if c<d:
  smax = a*m
  if m % 2 == 1:
    smin = (n-a)*(m-1)/2
  else:
    smin = (n-a)*(m/2)
else:
  smax = b*n
  if m % 2 == 1:
    smin = (n-a)*(m-1)/2
  else:
    smin = (n-a)*(m/2)
print(smax-smin)

Submission status: Wrong Answer

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

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

ans = float('inf')
a, b, c, d = 0, 0, 0, 0

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

for i in range(-100, 100):
    a = W // 3 + i
    b = W - a
    if a > 0 and b > 0:
        pqr = [a * H, b * c, b * d]
        ans = min(ans, max(pqr) - min(pqr))

H, W = W, H

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

for i in range(-100, 100):
    a = W // 3 + i
    b = W - a
    if a > 0 and b > 0:
        pqr = [a * H, b * c, b * d]
        ans = min(ans, max(pqr) - min(pqr))

print(int(ans))

Submission status: Wrong Answer

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

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

ans = float('inf')
a, b, c, d = 0, 0, 0, 0

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

for i in range(-2, 3):
    a = W // 3 + i
    b = W - a
    if a > 0 and b > 0:
        pqr = [a * H, b * c, b * d]
        ans = min(ans, max(pqr) - min(pqr))

H, W = W, H

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

for i in range(-2, 3):
    a = W // 3 + i
    b = W - a
    if a > 0 and b > 0:
        pqr = [a * H, b * c, b * d]
        ans = min(ans, max(pqr) - min(pqr))

print(int(ans))

Submission status: Wrong Answer

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

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

ans = float('inf')
a, b, c, d = 0, 0, 0, 0

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3 - 1
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))
H, W = W, H

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3 - 1
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))

a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))

print(int(ans))

Submission status: Wrong Answer

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

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

ans = float('inf')
a, b, c, d = 0, 0, 0, 0

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3 - 1
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))
H, W = W, H

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3 - 1
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))

a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))

print(int(ans))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = float('inf')
a, b, c, d = 0, 0, 0, 0

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3 - 1
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))
H, W = W, H

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3 - 1
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))

a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))

print(int(ans))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = float('inf')
a, b, c, d = 0, 0, 0, 0

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = max(pqr) - min(pqr)
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))
H, W = W, H

if H % 2 == 0:
    c, d = H / 2, H / 2
else:
    c, d = H // 2, H // 2 + 1

a = W // 3
b = W - a
if a != 0:
    pqr = [a * H, b * c, b * d]
    ans = min(ans, max(pqr) - min(pqr))
a = W // 3 + 1
b = W - a
pqr = [a * H, b * c, b * d]
ans = min(ans, max(pqr) - min(pqr))

print(int(ans))

Submission status: Wrong Answer

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

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

#min_p = None
min_diff = 2 ** 31 - 1

def func(A, B):
	global min_p
	global min_diff
	for a in range(1, A + 1):
		area = []
		area += [a * B]
		area += [(A - a) * (B // 2)]
		area += [(A - a) * (B - B // 2)]
		if max(area) - min(area) < min_diff:
			min_diff = max(area) - min(area)
			#min_p = [A, B, a, area]

func(H, W)
func(W, H)

#print(min_p)
print(min_diff)

Submission status: Wrong Answer

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

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

min_diff = 2 ** 31 - 1

def func(A, B):
	global min_diff
	for a in range(1, A + 1):
		area = []
		area += [a * B]
		area += [(A - a) * (B // 2)]
		area += [(A - a) * (B - B // 2)]
		#print(a, area)
		min_diff = min(min_diff, (max(area) - min(area)))

func(H, W)
func(W, H)

print(min_diff)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7



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

ans = 0
if area % 3 == 0:
    ans = 0
    print(ans)
    exit()

space = area/3

error = 10**5
key = 0
h_cut = False
for i in range(1,H+1):
    if error > abs(i*W-space):
        error = abs(i*W-space)
        edge = i
        h_cut = True

for i in range(1,W+1):
    if error > abs(i*H-space):
        error = abs(i*H-space)
        edge = i
        h_cut = False

if h_cut:
    H -= edge
    areas = [edge*W]
else:
    W -= edge
    areas = [edge*H]

area = H*W
space = area/2

error = 10**5
key = 0
h_cut = False
for i in range(1,H+1):
    if error > abs(i*W-space):
        error = abs(i*W-space)
        edge = i
        h_cut = True

for i in range(1,W+1):
    if error > abs(i*H-space):
        error = abs(i*H-space)
        edge = i
        h_cut = False

if h_cut:
    areas.append(edge*W)
    areas.append((H-edge)*W)
else:
    areas.append(edge*H)
    areas.append((W-edge)*H)
print(max(areas)-min(areas))

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    ans = 0
else:
    ans = H*W
    for _ in range(2):
        for h in range(H//3 - 1, H//3 + 3):
            for w in range(W//2 - 1, W//2 + 2):
                M = max(h * W, (H-h)*w, (H-h)*(W-w))
                m = min(h * W, (H-h)*w, (H-h)*(W-w))
                if m != 0:
                    ans = min(ans, M-m)

        H, W = W, H

print(ans)

Submission status: Wrong Answer

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

if (H * W) % 3 == 0:
    ans = 0
else:
    ans = H*W
    for _ in range(2):
        for h in range(H//3,H//3 + 3):
            for w in range(W//2, W//2 + 2):
                M = max(h * W, (H-h)*w, (H-h)*(W-w))
                m = min(h * W, (H-h)*w, (H-h)*(W-w))
                if m != 0:
                    ans = min(ans, M-m)

        H, W = W, H

print(ans)