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())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    for i in range(1, H):
        kari = W * i
        if (H - i) % 2 == 0 or W % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            kari_2 = (W // 2 + 1) * (H-i)
            kari_3 = (W // 2) * (H-i)
            ans = min(ans, max(kari, kari_2, kari_3)-min(kari, kari_2, kari_3))
    for j in range(1, W):
        kari = W * j
        if (W - j) % 2 == 0 or H % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:

            kari_2 = (H // 2 + 1) * (W-j)
            kari_3 = (H // 2) * (W-j)
            ans = min(ans, max(kari, kari_2, kari_3)-min(kari, kari_2, kari_3))
    ans = min(ans, W, H)

    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    maxHW = max(H, W)
    minHW = min(H, W)
    for i in range(1, maxHW):
        kari = minHW * i
        if (maxHW - i) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - i, minHW)
            miniminHW = min(maxHW-i, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(kari, kari_2, kari_3)-min(kari, kari_2, kari_3))
    for j in range(1, minHW):
        kari = minHW * j
        if (minHW - j) % 2 == 0 or maxHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maxjmaxHW = max(minHW - j, maxHW)
            minjmaxHW = min(minHW-j, maxHW)
            kari_2 = (maxjmaxHW // 2 + 1) * minjmaxHW
            kari_3 = (maxjmaxHW // 2) * minjmaxHW
            ans = min(ans, max(kari, kari_2, kari_3)-min(kari, kari_2, kari_3))
    ans = min(ans, W, H)

    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    maxHW = max(H, W)
    minHW = min(H, W)
    for i in range(1, maxHW):
        kari = minHW * i
        if (maxHW - i) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - i, minHW)
            miniminHW = min(maxHW-i, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    for j in range(1, minHW):
        kari = minHW * j
        if (minHW - j) % 2 == 0 or maxHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maxjmaxHW = max(minHW - j, maxHW)
            minjmaxHW = min(minHW-j, maxHW)
            kari_2 = (maxjmaxHW // 2 + 1) * minjmaxHW
            kari_3 = (maxjmaxHW // 2) * minjmaxHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    ans = min(ans, W, H)

    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    maxHW = max(H, W)
    minHW = min(H, W)
    for i in range(1, maxHW):
        kari = minHW * i
        if (maxHW - i) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - i, minHW)
            miniminHW = min(maxHW-i, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    for j in range(1, minHW):
        kari = minHW * j
        if (maxHW - j) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - j, minHW)
            miniminHW = min(maxHW-j, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    ans = min(ans, W, H)

    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    maxHW = max(H, W)
    minHW = min(H, W)
    for i in range(1, maxHW):
        kari = minHW * i
        if (maxHW - i) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - i, minHW)
            miniminHW = min(maxHW-i, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    for j in range(1, minHW):
        kari = minHW * j
        if (maxHW - j) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - j, minHW)
            miniminHW = min(maxHW-j, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    maxHW = max(H, W)
    minHW = min(H, W)
    for i in range(1, maxHW + 1):
        kari = minHW * i
        if (maxHW - i) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - i, minHW)
            miniminHW = min(maxHW-i, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    for j in range(1, minHW + 1):
        kari = minHW * j
        if (maxHW - j) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - j, minHW)
            miniminHW = min(maxHW-j, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
ans = H * W
area = H * W
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    maxHW = max(H, W)
    minHW = min(H, W)
    for i in range(1, maxHW + 1):
        kari = minHW * i
        if (maxHW - i) % 2 == 0 or minHW % 2 == 0:
            kari_2 = (area - kari) // 2
            ans = min(ans, abs(kari-kari_2))
        else:
            maximinHW = max(maxHW - i, minHW)
            miniminHW = min(maxHW-i, minHW)
            kari_2 = (maximinHW // 2 + 1) * miniminHW
            kari_3 = (maximinHW // 2) * miniminHW
            ans = min(ans, max(abs(kari - kari_2),
                               abs(kari - kari_3), abs(kari_2 - kari_3)))
    print(ans)

Submission status: Wrong Answer

Solution code:
H,W=map(int, input().split())
a=10**10
S=H*W
s=[None]*3
for i in range(W):
    s[0]=H*i
    s[1]=(W-i)//2*H
    s[2]=S-s[0]-s[1]
    a=min(a,max(s)-min(s))
    s[1]=H//2*(W-i)
    s[2]=S-s[0]-s[1]
    a=min(a,max(s)-min(s))
for i in range(H):
    s[0]=W*i
    s[1]=(H-i)//2*W
    a=min(a,max(s)-min(s))
    s[1]=W//2*(H-i)
    s[2]=S-s[0]-s[1]
    a=min(a,max(s)-min(s))
print(a)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
ans=10**9
for i in range(1,w):
  ans=min(abs(i*h-h//2*(w-i)),ans)
for i in range(1,h):
  ans=min(abs(i*w-w//2*(h-i)),ans)
if h%3==0 or w%3==0:
  ans=0
else:
  ans=min(ans,h,w)
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:
    H, W = W, H

cand = []
for i in range(1, H+1):
    s_1 = i*W
    s_2_hor = (H-i)//2*W
    s_3_hor = (H-(i+(H-i)//2))*W
    s_2_ver = (H-i)*(W//2)
    s_3_ver = (H-i)*(W-W//2)
    ans_hor = abs(max(s_1, s_2_hor, s_3_hor) - min(s_1, s_2_hor, s_3_hor))
    ans_ver = abs(max(s_1, s_2_ver, s_3_ver) - min(s_1, s_2_ver, s_3_ver))
    cand.append(ans_hor)
    cand.append(ans_ver)

print(min(cand))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
#l = sorted([int(input()) for i in range(N)],reverse=True)
if H%3 == 0 or W%3 == 0:
    print(0)
else:
    maxhw = max(H,W)
    minhw = min(H,W)
    maxn = int(maxhw/3)+1
    minn = int(minhw/3)+1
    maxs = minhw*maxn
    print(maxs - int(((H*W-maxs)*minn)/minhw))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
#l = sorted([int(input()) for i in range(N)],reverse=True)
if H%3 == 0 or W%3 == 0:
    print(0)
else:
    maxhw = max(H,W)
    minhw = min(H,W)
    maxn = int(maxhw/3)+1
    maxs = minhw*maxn
    print(maxs - (H*W-maxs)//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
W,H = map(int,input().split())
ans = 10**100
for i in range(1,W):
    a = H*i
    w = W - i
    b = min((w//2)*H,w*(H//2))
    c = w*H - b
    x = max(a,b,c) - min(a,b,c)
    ans = min(ans,x)
for i in range(1,H):
    a = W*i
    h = H - i
    b = min((W//2)*h,W*(h//2))
    c = h*W - b
    x = max(a,b,c) - min(a,b,c)
    ans = min(ans,x)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
H,W=map(int,input().split())

def calc(main,sub):
    if main%3==1:
        a=main//3
    else:
        a=main//3+1
    b=sub//2
    temp=[a*sub,(main-a)*b,(main-a)*(sub-b)]
    
    return max(temp)-min(temp)
        

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

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

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

def solve(H, W):
    even = False
    ansmin = inf
    if W % 2 == 0:
        even = True
    for height in range(1, H):
        s1 = height * W
        if even:
            s2 = W * (H - height) // 2
            s3 = s2
            if s1 <= s2:
                s, m, l = s1, s2, s3
            else:
                s, m, l = s2, s3, s1
        else:
            s2 = (W - 1) * (H - height) // 2
            s3 = (W + 1) * (H - height) // 2
            if s1 <= s2 <= s3:
                s, m, l = s1, s2, s3
            elif s2 <= s3 <= s1:
                s, m, l = s2, s3, s1
            elif s2 <= s1 <= s3:
                s, m, l = s2, s1, s3
            else:
                raise IOError()
        ansmin = min(ansmin, l - s)
    return ansmin

def main():
    H, W = LI()
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return
    else:
        a1 = solve(H, W)
        a2 = solve(W, H)
        print(min(a1, a2))
        return

main()

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

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

def solve(H, W):
    even = False
    ansmin = inf
    if W % 2 == 0:
        even = True
    for height in range(1, H):
        s1 = height * W
        if even:
            s2 = W * (H - height) // 2
            s3 = s2
            if s1 <= s2:
                s, m, l = s1, s2, s3
            else:
                s, m, l = s2, s3, s1
        else:
            s2 = (W - 1) * (H - height) // 2
            s3 = (W + 1) * (H - height) // 2
            if s1 <= s2 <= s3:
                s, m, l = s1, s2, s3
            elif s2 <= s3 <= s1:
                s, m, l = s2, s3, s1
            elif s2 <= s1 <= s3:
                s, m, l = s2, s1, s3
        ansmin = min(ansmin, l - s)
    return ansmin

def main():
    H, W = LI()
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return
    else:
        a1 = solve(H, W)
        a2 = solve(W, H)
        print(min(a1, a2))
        return

main()

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

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

def solve(H, W):
    even = False
    ansmin = inf
    if W % 2 == 0:
        even = True
    for height in range(1, H):
        s1 = height * W
        if even:
            s2 = W * (H - height) // 2
            s3 = s2
            if s1 <= s2:
                s, m, l = s1, s2, s3
            else:
                s, m, l = s2, s3, s1
        else:
            s2 = (W - 1) * (H - height) // 2
            s3 = (W + 1) * (H - height) // 2
            if s1 < s2 < s3:
                s, m, l = s1, s2, s3
            elif s2 < s3 < s1:
                s, m, l = s2, s3, s1
            elif s2 < s1 < s3:
                s, m, l = s2, s1, s3
        ansmin = min(ansmin, l - s)
    return ansmin

def main():
    H, W = LI()
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return
    else:
        a1 = solve(H, W)
        a2 = solve(W, H)
        print(min(a1, a2))
        return

main()

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

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

def solve(H, W):
    even = False
    ansmin = inf
    if W % 2 == 0:
        even = True
    for height in range(1, H):
        s1 = height * W
        if even:
            s2 = W * (H - height) // 2
            s3 = s2
            if s1 <= s2:
                s, m, l = s1, s2, s3
            else:
                s, m, l = s2, s3, s1
        else:
            s2 = (W - 1) * (H - height) // 2
            s3 = (W + 1) * (H - height) // 2
            if s1 < s2 < s3:
                s, m, l = s1, s2, s3
            elif s2 < s3 < s1:
                s, m, l = s2, s3, s1
        ansmin = min(ansmin, l - s)
    return ansmin

def main():
    H, W = LI()
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return
    else:
        a1 = solve(H, W)
        a2 = solve(W, H)
        print(min(a1, a2))
        return

main()

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

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

def solve(H, W):
    even = False
    ansmin = inf
    if W % 2 == 0:
        even = True
    for height in range(1, H-1):
        s1 = height * W
        if even:
            s2 = W * (H - height) // 2
            s3 = s2
            if s1 <= s2:
                s, m, l = s1, s2, s3
            else:
                s, m, l = s2, s3, s1
        else:
            s2 = (W - 1) * (H - height) // 2
            s3 = (W + 1) * (H - height) // 2
            if s1 < s2 < s3:
                s, m, l = s1, s2, s3
            elif s2 < s3 < s1:
                s, m, l = s2, s3, s1
        ansmin = min(ansmin, l - s)
    return ansmin

def main():
    H, W = LI()
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return
    else:
        a1 = solve(H, W)
        a2 = solve(W, H)
        print(min(a1, a2))
        return

main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:    # 3の倍数だったら最小値は0
    print(0)
    exit()

answer = float('inf')

h = 1
for _ in range(H):
    a = h * W
    b = (H-h) * (W // 2)
    c = H * W - a - b
    MAX = max([a, b, c])
    MIN = min([a, b, c])
    if MAX - MIN <= answer:
        answer = MAX - MIN
    h += 1

w = 1
for _ in range(W):
    a = w * H
    b = (W-w) * (H // 2)
    c = H * W - a - b
    MAX = max([a, b, c])
    MIN = min([a, b, c])
    if MAX - MIN <= answer:
        answer = MAX - MIN
    w += 1

print(answer)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:    # 3の倍数だったら最小値は0
    print(0)
    exit()

answer = float('inf')

h = 1
for _ in range(H-1):
    a = h * W
    b = (H-h) * (W // 2)
    c = H * W - a - b
    MAX = max([a, b, c])
    MIN = min([a, b, c])
    if MAX - MIN <= answer:
        answer = MAX - MIN
    h += 1

w = 1
for _ in range(W-1):
    a = w * H
    b = (W-w) * (H // 2)
    c = H * W - a - b
    MAX = max([a, b, c])
    MIN = min([a, b, c])
    if MAX - MIN <= answer:
        answer = MAX - MIN
    w += 1
    
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
mn=float("inf")
for i in range(1,h):
    a=w*(h-i)
    b=i*-(-w//2)
    c=i*(w//2)
    dai=max(a,b,c)
    syo=min(a,b,c)
    #print(a,b,c,i)
    mn=min(mn,dai-syo)
for i in range(1,w):
    a=h*(w-i)
    b=i*-(-h//2)
    c=i*(h//2)
    dai=max(a,b,c)
    syo=min(a,b,c)
    #print(a,b,c,i)
    mn=min(mn,dai-syo)
print(mn)

Submission status: Wrong Answer

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

ans = H * W
for ha in range(1, H-1):
    a = ha * W

    hb = (H - ha) // 2

    b = hb * W
    c = H * W - a - b

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

    wb = W // 2
    b = (H - ha) * wb
    c = H * W - a - b

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

for wa in range(1, W-1):
    a = H * wa

    wb = (W - wa) // 2

    b = H * wb
    c = H * W - a - b

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

    hb = H // 2
    b = (W - wa) * hb
    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())

ans = H * W
for ha in range(1, H-1):
    a = ha * W

    hb = (H - ha) // 2

    b = hb * W
    c = (H - ha - hb) * W

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

    wb = W // 2
    b = (H - ha) * wb
    c = (H - ha) * (W - wb)

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

for wa in range(1, W-1):
    a = H * wa

    wb = (W - wa) // 2

    b = H * wb
    c = H * (W - wa - wb)

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

    hb = H // 2
    b = (W - wa) * hb
    c = (W - wa) * (H - hb)

    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 h in range(1,H+1):
    A = h*W
    B = (H-h)*(W//2)
    C = H*W-A-B
    ans = min(ans,max(A,B,C)-min(A,B,C))
H,W = W,H
for h in range(1,H):
    A = h*W
    B = (H-h)*(W//2)
    C = H*W-A-B
    ans = min(ans,max(A,B,C)-min(A,B,C))
if H%3==0 or W%3==0:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
ans = 10**11
for h in range(1,H+1):
    A = h*W
    B = (H-h)*(W//2)
    C = H*W-A-B
    ans = min(ans,max(A,B,C)-min(A,B,C))
H,W = W,H
for h in range(1,H+1):
    A = h*W
    B = (H-h)*(W//2)
    C = H*W-A-B
    ans = min(ans,max(A,B,C)-min(A,B,C))
if H%3==0 or W%3==0:
    ans = 0
print(ans)

Submission status: Wrong Answer

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

# 一発目を横に切る
tmp = h // 3
a = tmp * w
b = (h-tmp) // 2 * w
c = h*w - a - b
ans = max([a, b, c]) - min([a, b, c])

tmp = (h+2) // 3
a = tmp * w
b = (h-tmp) // 2 * w
c = h*w - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = h // 3
a = tmp * w
b = (h-tmp) * (w//2)
c = h*w - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = (h+2) // 3
a = tmp * w
b = (h-tmp) * (w//2)
c = h*w - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

# 一発目を縦に切る
tmp = w // 3
a = tmp * h
b = (w-tmp) // 2 * h
c = w*h - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = (w+2) // 3
a = tmp * h
b = (w-tmp) // (2*h)
c = w*h - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = w // 3
a = tmp * h
b = (w-tmp) * h // 2
c = w*h - a - b
ans = min(ans, max([a, b, c]) - min([a, b, c]))

tmp = (w+2) // 3
a = tmp * h
b = (w-tmp) * (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 ** 10
if (h % 3 == 0) or (w % 3 == 0):
    ans = 0
# 半分 or 半分+1 で切って全探索
else:
    if h % 2 == 0:
        for i in range(1,w):
            Sa = int(h/2) * i
            Sc = h * (w-i)
            dif = abs(Sa-Sc)
            ans = min(ans, dif)
    elif h % 2 == 1:
        for i in range(1,w):
            Sa = int(h // 2)  * i
            Sb = (int(h // 2) + 1) * i
            Sc = h * (w-i)
            dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
            ans = min(ans, dif)
        
    if w % 2 == 0:
        for j in range(1,h):
            Sa = int(w/2) * j
            Sc = w * (h-j)
            dif = abs(Sa-Sc)
            ans = min(ans, dif)
    elif w % 2 == 1:
        for j in range(1,h):
            Sa = int(w // 2)  * j
            Sb = (int(w // 2) + 1) * j
            Sc = w * (h-j)
            dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
            ans = min(ans, dif)
print(ans)

Submission status: Wrong Answer

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

ans = 10 ** 10
if (h % 3 == 0) or (w % 3 == 0):
    ans = 0
# 半分 or 半分+1 で切って全探索
elif (h % 2 == 0) or (w % 2 == 0):
    if h % 2 == 0:
        for i in range(1,w):
            Sa = int(h/2) * i
            Sc = h * (w-i)
            dif = abs(Sa-Sc)
            ans = min(ans, dif)
    if w % 2 == 0:
        for j in range(1,h):
            Sa = int(w/2) * j
            Sc = w * (h-j)
            dif = abs(Sa-Sc)
            ans = min(ans, dif)
else:
    # h側で切る場合
    for i in range(1,w):
        Sa = int(h // 2)  * i
        Sb = (int(h // 2) + 1) * i
        Sc = h * (w-i)
        dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
        ans = min(ans, dif)
    # w側で切る場合
    for j in range(1,h):
        Sa = int(w // 2)  * j
        Sb = (int(w // 2) + 1) * j
        Sc = w * (h-j)
        dif = max(Sa, Sb, Sc) - min(Sa, Sb, Sc)
        ans = min(ans, dif)

print(ans)

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())
if x%3 == 0 or y%3 == 0:
    print(0)
    exit()
ans = []
ans.append(x)
ans.append(y)
a1 = x//3
a2 = x-a1
b1 = y//2
b2 = y-y//2
ans.append(max(a1*y,a2*b1,a2*b2)-min(a1*y,a2*b1,a2*b2))
a1 = x//3+1
a2 = x-a1
ans.append(max(a1*y,a2*b1,a2*b2)-min(a1*y,a2*b1,a2*b2))
print(min(ans))

Submission status: Wrong Answer

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

if H%3 == 0 or W%3 == 0:
    print(0)
    exit()
    
def solve(a, b):
    ans = 10**15
    for i in range(1, a+1):
        s1 = i * b
        s2 = (a-i) * (b//2)
        s3 = a*b - s1 - s2
        s4 = ((a-1)//2) * b
        s5 = a*b - 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
    return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

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

if H%3 == 0 or W%3 == 0:
    print(0)
    exit()
    
def solve(a, b):
    ans = 10**10
    for i in range(1, a+1):
        s1 = i * b
        s2 = (a-i) * (b//2)
        s3 = a*b - s1 - s2
        s4 = ((a-1)//2) * b
        s5 = a*b - 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
    return ans

print(min(solve(H, W), solve(W, H)))

Submission status: Wrong Answer

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

def half(h,w):
   h1 = h//2
   h2 = h - h1
   return h1*w, h2*w
def half2(h,w):
   w1 = w//2
   w2 = w-w1
   return w1*h, w2*h

ans = float("inf")
for i in range(h):
   a = i * w
   b,c = half(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   b,c = half2(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   


for i in range(w):
   a = i * h
   b,c = half(h, w-i)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   b,c = half2(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   if a > 0 and b > 0 and c > 0:
      ans = min(tmp, ans)
   
print(ans)

Submission status: Wrong Answer

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

def half(h,w):
   h1 = h//2
   h2 = h - h1
   return h1*w, h2*w
def half2(h,w):
   w1 = w//2
   w2 = w-w1
   return w1*h, w2*h

ans = float("inf")
for i in range(h):
   a = i * w
   b,c = half(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   ans = min(tmp, ans)
   b,c = half2(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   ans = min(tmp, ans)
   


for i in range(w):
   a = i * h
   b,c = half(h, w-i)
   tmp = max(a,b,c) - min(a,b,c)
   ans = min(tmp, ans)
   b,c = half2(h-i, w)
   tmp = max(a,b,c) - min(a,b,c)
   ans = min(tmp, ans)
   
print(ans)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
answer=0
if not H%3==0 and not W%3==0:
  S_1=[H//3*W,(H-H//3)*(W//2),(H-H//3)*(W-W//2)]
  S_2=[(H//3+1)*W,(H-H//3-1)*(W//2),(H-H//3-1)*(W-W//2)]
  S_3=[W//3*H,(W-W//3)*(H//2),(W-W//3)*(H-H//2)]
  S_4=[(W//3+1)*H,(W-W//3-1)*(H//2),(W-W//3-1)*(H-H//2)]
  answer=min(max(S_1)-min(S_1),max(S_2)-min(S_2),max(S_3)-min(S_3),max(S_4)-min(S_4))
print(answer)

Submission status: Wrong Answer

Solution code:
import sys

H, W = map(int, sys.stdin.readline().split())

def minimize(H, W):
    h = round(H / 3)
    a = h * W
    w = W // 2
    b = (H - h) * w
    c = (H - h) * (W - w) 
    return  max(a, b, c) - min(a, b, c)
    
def main():
    if H % 3 == 0 or W % 3 == 0:
        return 0
    
    return min(minimize(H, W), minimize(W, H))

if __name__ == '__main__':
    ans = main()
    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 = H * W
for i in range(2):
    for h in range(1, H):
        s = [h * W, W // 2 * (H - h), (W - W // 2) * (H - h)]
        s2 = [h * W, (H - h) // 2 * W]
        ans = min(ans, min(max(s) - min(s), max(s2) - min(s2)))
    H, W = W, H
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 = H * W
for i in range(2):
    for h in range(1, H):
        s = [h * W, W // 2 * (H - h), (W - W // 2) * (H - h)]
        s2 = [h * W, (H - h) // 2 * W, H - (H - h) // 2 * W]
        ans = min(ans, min(max(s) - min(s), max(s2) - min(s2)))
    H, W = W, H
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 = H * W
for i in range(2):
    for h in range(1, H):
        s = [h * W, W // 2 * (H - h), (W - W // 2) * (H - h)]
        ans = min(ans, max(s) - min(s))
    H, W = W, H
print(ans)