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


def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        print(0)
    else:
        ans = 10**18

        # Hを1:2で分割
        for i in range(3):
            d = (H // 3)- 1 + i
            choco = [d * W, (H-d) * (W//2), (H-d) * (W - W//2)]
            ans = min(ans, max(choco) - min(choco))
            # print(choco)
        
        # Wを1:2で分割
        for i in range(3):
            d = (W // 3)- 1 + i
            choco = [d * H, (W-d) * (H//2), (W-d) * (H - H//2)]
            ans = min(ans, max(choco) - min(choco))
            # print(choco)
        
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        H, W = map(int, input().split())
        if H % 3 == 0 or W % 3 == 0:
            return 0
        else:
            ans = 10**18

            for h in range(1, H-1):
                A = h*W
                B = ((H-h)//2)*W
                C = (H-h-(H-h)//2)*W
                ans = min(max(A, B, C) -
                          min(A, B, C), ans)

                B = (W//2)*(H-h)
                C = (W-(W//2))*(H-h)
                ans = min(max(A, B, C) -
                          min(A, B, C), ans)

            for w in range(1, W-1):
                A = w*H
                B = ((W-w)//2)*H
                C = (W-w-(W-w)//2)*H
                ans = min(max(A, B, C) -
                          min(A, B, C), ans)

                B = (H//2)*(W-w)
                C = (H-(H//2))*(W-w)
                ans = min(max(A, B, C) -
                          min(A, B, C), ans)
            return ans
    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        H, W = map(int, input().split())
        if H % 3 == 0 or W % 3 == 0:
            return 0
        else:
            ans = 10**10

            for h in range(1, H-1):
                areaA = h*W
                areaB = ((H-h)//2)*W
                areaC = (H-h-(H-h)//2)*W
                L = [areaA, areaB, areaC]
                ans = min(max(L) -
                          min(L), ans)

                areaBB = (W//2)*(H-h)
                areaCC = (W-(W//2))*(H-h)
                LL = [areaA, areaBB, areaCC]

                ans = min(max(LL) -
                          min(LL), ans)

            for w in range(1, W-1):
                areaA = w*H
                areaB = ((W-w)//2)*H
                areaC = (W-w-(W-w)//2)*H
                L = [areaA, areaB, areaC]
                ans = min(max(L) -
                          min(L), ans)

                areaBB = (H//2)*(W-w)
                areaCC = (H-(H//2))*(W-w)
                LL = [areaA, areaBB, areaCC]

                ans = min(max(LL) -
                          min(LL), ans)
            return ans
    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        H, W = map(int, input().split())
        if H % 3 == 0 or W % 3 == 0:
            return 0
        else:
            ans = 10**10

            for h in range(1, H-1):
                areaA = h*W
                areaB = ((H-h)//2)*W
                areaC = (H-h-(H-h)//2)*W
                L = [areaA, areaB, areaC]
                ans = min(max(L) -
                          min(L), ans)

                areaBB = (W//2)*(H-h)
                areaCC = (W-(W//2))*(H-h)
                LL = [areaA, areaBB, areaCC]

                ans = min(max(LL) -
                          min(LL), ans)

            for w in range(1, W-1):
                areaA = w*H
                areaB = ((W-w)//2)*W
                areaC = (W-w-(W-w)//2)*W
                L = [areaA, areaB, areaC]
                ans = min(max(L) -
                          min(L), ans)

                areaBB = (H//2)*(W-w)
                areaCC = (H-(H//2))*(W-w)
                LL = [areaA, areaBB, areaCC]

                ans = min(max(LL) -
                          min(LL), ans)
            return ans
    print(main())

resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():
        H, W = map(int, input().split())
        if H % 3 == 0 or W % 3 == 0:
            return 0
        else:
            ans = 10**10

            for h in range(1, H-1):
                areaA = h*W
                areaB = (h//2)*W
                areaC = (H-h-h//2)*W
                L = [areaA, areaB, areaC]
                ans = min(max(L) -
                          min(L), ans)

                areaBB = (W//2)*(H-h)
                areaCC = (W-(W//2))*(H-h)
                LL = [areaA, areaBB, areaCC]

                ans = min(max(LL) -
                          min(LL), ans)

            for w in range(1, W-1):
                areaA = w*H
                areaB = (w//2)*H
                areaC = (W-w-w//2)*H
                L = [areaA, areaB, areaC]
                ans = min(max(L) -
                          min(L), ans)

                areaBB = (H//2)*(W-w)
                areaCC = (H-(H//2))*(W-w)
                LL = [areaA, areaBB, areaCC]

                ans = min(max(LL) -
                          min(LL), ans)
            return ans
    print(main())


resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    def main():
        H,W=map(int,input().split())
        if H%3==0 or W %3==0: return 0
        else:
            ans=10**10
            for h in range(1,H):
                areaA=h*W
                areaB=(h//2)*W
                areaC=(H-h-h//2)*W
                ans=min(max([areaA,areaB,areaC])-min([areaA,areaB,areaC]),ans)

                areaBB=(W//2)*(H-h)
                areaCC=(W-(W//2))*(H-h)
                ans=min(max([areaA,areaBB,areaCC])-min([areaA,areaBB,areaCC]),ans)

            for w in range(1,W):
                areaA=w*H
                areaB=(w//2)*H
                areaC=(W-w-w//2)*H
                ans=min(max([areaA,areaB,areaC])-min([areaA,areaB,areaC]),ans)

                areaBB=(H//2)*(W-w)
                areaCC=(H-(H//2))*(W-w)
                ans=min(max([areaA,areaBB,areaCC])-min([areaA,areaBB,areaCC]),ans)
            return ans
    print(main())
        

        


    
resolve()

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
small = min(w, h)
large = max(w, h)

if (h % 3 == 0 or w % 3 == 0):
    ans = 0
elif (h % 2 != 0 and w % 2 != 0):
    more = (small // 2) * large
    less = (small - small // 2) * (large // 2)

    ans = abs(more - less)
else:
    ans = 10 ** 18
    if (h % 2 == 0 and w % 2 == 0):  # 偶偶
        for i in range(1, large):
            a = small * i
            b = small / 2 * (large - i)
            dif = abs(a - b)
            if (dif < ans):
                ans = dif
            else:
                break

    else:
        if (h % 2 == 0):
            even = h
            odd = w
        else:
            even = w
            odd = h

        for i in range(1, odd):
            a = even * i
            b = (even / 2) * (odd - i)
            dif = abs(a - b)
            if (dif < ans):
                ans = dif
            else:
                break


print(int(ans))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
small = min(w, h)
large = max(w, h)

if (h % 3 == 0 or w % 3 == 0):
    ans = 0
elif (h % 2 != 0 and w % 2 != 0):
    more = (small // 2) * large
    less = (small - small // 2) * (large // 2)

    ans = abs(more - less)
else:
    ans = 10 ** 18
    if (h % 2 == 0 and w % 2 == 0):  # 偶偶
        for i in range(1, large + 1):
            a = small * i
            b = small / 2 * (large - i)
            dif = abs(a - b)
            if (dif < ans):
                ans = dif
            else:
                break

    else:
        if (h % 2 == 0):
            even = h
            odd = w
        else:
            even = w
            odd = h

        for i in range(1, odd + 1):
            a = even * i
            b = (even / 2) * (odd - i)
            dif = abs(a - b)
            if (dif < ans):
                ans = dif
            else:
                break


print(int(ans))

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

H, W = getNM()

ans = float('inf')
# 縦
for i in range(1, H):
    maxblock = W * i
    left_h = H - i
    if W == 1 and left_h == 1:
        continue
    elif W == 1:
        minblock = left_h // 2
    elif left_h == 1:
        minblock = W // 2
    else:
        minblock = max(W * (left_h // 2), (W // 2) * left_h)
    if maxblock >= minblock and maxblock >= (W * left_h - minblock):
        ans = min(ans, maxblock - minblock)
# 横
for i in range(1, W):
    maxblock = H * i
    left_w = W - i
    if H == 1 and left_w == 1:
        continue
    elif H == 1:
        minblock = left_w // 2
    elif left_w == 1:
        minblock = H // 2
    else:
        minblock = max(H * (left_w // 2), (H // 2) * left_w)
    if maxblock >= minblock  and maxblock >= (H * left_w - minblock):
        ans = min(ans, maxblock - minblock)
print(ans)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

H, W = getNM()

ans = float('inf')
# 縦
for i in range(1, H):
    maxblock = W * i
    left_h = H - i
    minblock = max(W * (left_h // 2), (W // 2) * left_h)
    if maxblock >= minblock:
        ans = min(ans, maxblock - minblock)
# 横
for i in range(1, W):
    maxblock = H * i
    left_w = W - i
    minblock = max(H * (left_w // 2), (H // 2) * left_w)
    if maxblock >= minblock:
        ans = min(ans, maxblock - minblock)
print(ans)

Submission status: Wrong Answer

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

def menseki(h,w):
    global ans
    for i in range(1,h):
        # 縦1横2
        haba = w//2
        ans = min(ans,max(i*w,haba*(h-i),(w-haba)*(h-i))-min(i*w,haba*(h-i),(w-haba)*(h-i)))
        # 縦3分割
        if h-i == 1:
            continue
        tate = (h-i)//2
        ans = min(ans,max(w*i,tate*w,(h-tate)*w)-min(w*i,tate*w,(h-tate)*w))

menseki(h,w)
menseki(w,h)

print(ans)

Submission status: Wrong Answer

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

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


if w >h :
    ww = h
    hh = w
else:
    ww = w
    hh = h

if hh % 3 ==2:
    sa = ((hh//3)+1)*ww
    sb = (ww//2)*(hh-((hh//3)+1))
    sc = (ww-(ww//2))*(hh-((hh//3)+1))
    print(min(ww,max(abs(sa-sb),abs(sa-sc),abs(sb-sc))))
    
else:
    sa = (hh//3)*ww
    sb = (ww//2)*(hh-(hh//3))
    sc = (ww-(ww//2))*(hh-(hh//3))
    print(min(ww,max(abs(sa-sb),abs(sa-sc),abs(sb-sc))))

Submission status: Wrong Answer

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

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

if w >h :
    ww = h
    hh = w
else:
    ww = w
    hh = h

if hh % 3 ==2:
    sa = ((hh//3)+1)*ww
    sb = (ww//2)*(hh-((hh//3)+1))
    sc = (ww-(ww//2))*(hh-((hh//3)+1))
    print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))
else:
    sa = ((hh//3))*ww
    sb = (ww//2)*(hh-((hh//3)))
    sc = (ww-(ww//2))*(hh-((hh//3)))
    print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))

Submission status: Wrong Answer

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

if h * w % 3==0:
    print(0)

if w >h :
    ww = h
    hh = w
else:
    ww = w
    hh = h

if hh % 3 ==2:
    sa = ((hh//3)+1)*ww
    sb = (ww//2)*(hh-((hh//3)+1))
    sc = (ww-(ww//2))*(hh-((hh//3)+1))
    print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))
else:
    sa = ((hh//3))*ww
    sb = (ww//2)*(hh-((hh//3)))
    sc = (ww-(ww//2))*(hh-((hh//3)))
    print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))

Submission status: Wrong Answer

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

if h * w % 3==0:
    print(0)

elif h > w:
    if h % 3 ==2:
        sa = ((h//3)+1)*w
        sb = (w//2)*(h-((h//3)+1))
        sc = (w-(w//2))*(h-((h//3)+1))
        print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))
    else:
        sa = ((h//3))*w
        sb = (w//2)*(h-((h//3)))
        sc = (w-(w//2))*(h-((h//3)))
        print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))

else:
    if w % 3 ==2:
        sa = ((w//3)+1)*h
        sb = (h//2)*(w-((w//3)+1))
        sc = (h-(h//2))*(w-((w//3)+1))
        print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))
    else:
        sa = ((w//3))*h
        sb = (h//2)*(w-((w//3)))
        sc = (h-(h//2))*(w-((w//3)))
        print(max(abs(sa-sb),abs(sa-sc),abs(sb-sc)))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
if (H%3) * (W%3) == 0:
  print(0)
  quit()

ans = H * W
w1 = W // 2
w2 = W - w1
S1 = (H - 1) * w1
S2 = (H - 1) * w2
S3 = W
for i in range(H-1):
  diff = max(S1, S2, S3) - min(S1, S2, S3)
  ans = min(ans, diff)
  S1 -= w1; S2 -= w2; S3 += W

h1 = H // 2
h2 = H - h1
S1 = (W - 1) * h1
S2 = (W - 1) * h2
S3 = H
for i in range(W-1):
  diff = max(S1, S2, S3) - min(S1, S2, S3)
  ans = min(ans, diff)
  S1 -= h1; S2 -= h2; S3 += H

print(ans)

Submission status: Wrong Answer

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

if h * w % 3==0:
    print(0)

elif h > w:
    if h % 3 ==2:
        sa = ((h//3)+1)*w
        sb = (w//2)*(h-((h//3)+1))
        sc = (w-(w//2))*(h-((h//3)+1))
        print(max(abs(sa-sb),abs(sa-sc)))
    else:
        sa = ((h//3))*w
        sb = (w//2)*(h-((h//3)))
        sc = (w-(w//2))*(h-((h//3)))
        print(max(abs(sa-sb),abs(sa-sc)))

else:
    if w % 3 ==2:
        sa = ((w//3)+1)*h
        sb = (h//2)*(w-((w//3)+1))
        sc = (h-(h//2))*(w-((w//3)+1))
        print(max(abs(sa-sb),abs(sa-sc)))
    else:
        sa = ((w//3))*h
        sb = (h//2)*(w-((w//3)))
        sc = (h-(h//2))*(w-((w//3)))
        print(max(abs(sa-sb),abs(sa-sc)))

Submission status: Wrong Answer

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

if h * w % 3==0:
    print(0)

elif h > w:
    if h % 3 ==2:
        sa = ((h//3)+1)*w
        sb = (w//2)*(h-((h//3)+1))
        sc = (w-(w//2))*(h-((h//3)+1))
        print(abs(sa-sb))
    else:
        sa = ((h//3))*w
        sb = (w//2)*(h-((h//3)))
        sc = (w-(w//2))*(h-((h//3)))
        print(abs(sa-sb))

else:
    if w % 3 ==2:
        sa = ((w//3)+1)*h
        sb = (h//2)*(w-((w//3)+1))
        sc = (h-(h//2))*(w-((w//3)+1))
        print(abs(sa-sb))
    else:
        sa = ((w//3))*h
        sb = (h//2)*(w-((w//3)))
        sc = (h-(h//2))*(w-((w//3)))
        print(abs(sa-sb))

Submission status: Wrong Answer

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

if h * w % 3==0:
    print(0)

elif h > w:
    sa = ((h//3)+1)*w
    sb = (w//2)*(h-((h//3)+1))
    sc = (w-(w//2))*(h-((h//3)+1))
    print(sa-sb)

else:
    sa = ((w//3)+1)*h
    sb = (h//2)*(w-((w//3)+1))
    sc = (h-(h//2))*(w-((w//3)+1))
    print(sa-sb)

Submission status: Wrong Answer

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

if h * w % 3==0:
    print(0)

elif h > w:
    sa = ((h//3)+1)*w
    sb = (w//2)*(h-((h//3)+1))
    sc = (w-(w//2))*(h-((h//3)+1))
    print(sa-sc)

else:
    sa = ((w//3)+1)*h
    sb = (h//2)*(w-((w//3)+1))
    sc = (h-(h//2))*(w-((w//3)+1))
    print(sa-sc)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    H,W=map(int,input().split())
    ans=INF
    for i in range(1,H):
        S0=W*i
        S1=(H-i)*(W//2)
        S2=(H-i)*(-(-W//2))
        S3=W*(H-i)//2
        S4=W*(-(i-H)//2)
        ans=min(ans,max(abs(S0-S1),abs(S0-S2)),max(abs(S0-S3),abs(S0-S4)))
    for i in range(1,W):
        S0=H*i
        S1=(W-i)*(H//2)
        S2=(W-i)*(-(-H//2))
        S3=H*(W-i)//2
        S4=H*(-(i-W)//2)
        ans=min(ans,max(abs(S0-S1),abs(S0-S2)),max(abs(S0-S3),abs(S0-S4)))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = float("inf")
for h, w in zip([H, W], [W, H]):
    # ah の全探索
    for ah in range(1, h):
        # 残りを縦割り
        Sa = ah * w
        bw = (w - 1) // 2 + 1
        Sb = (h - ah) * bw
        Sc = (h - ah) * (w - bw)
        ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))

        # 残りを横割り
        if (h - ah) > 1:
            bh = ((H - ah) - 1) // 2 + 1
            Sb = bh * w
            Sc = (h - ah - bh) * w
            ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))
        
print(ans)

Submission status: Wrong Answer

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

ans = float("inf")
for h, w in zip([H, W], [W, H]):
    # ah の全探索
    for ah in range(2, h):
        # 残りを縦割り
        Sa = ah * w
        bw = (w - 1) // 2 + 1
        Sb = (h - ah) * bw
        Sc = (h - ah) * (w - bw)
        ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))

        # 残りを横割り
        if (H - ah) > 1:
            bh = ((H - ah) - 1) // 2 + 1
            Sb = bh * w
            Sc = (h - ah - bh) * w
            ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))
        
print(ans)

Submission status: Wrong Answer

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

ans = float("inf")
for h, w in zip([H, W], [W, H]):
    # ah の全探索
    for ah in range(1, h):
        # 残りを縦割り
        if w > 1:
            Sa = ah * w
            bw = (w - 1) // 2 + 1
            Sb = (h - ah) * bw
            Sc = (h - ah) * (w - bw)
            ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))

        # 残りを横割り
        if (H - ah) > 1:
            bh = ((H - ah) - 1) // 2 + 1
            Sb = bh * w
            Sc = (h - ah - bh) * w
            ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))
        
print(ans)

Submission status: Wrong Answer

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

ans = float("inf")
for h, w in zip([H, W], [W, H]):
    # ah の全探索
    for ah in range(1, h):
        # 残りを縦割り
        Sa = ah * w
        bw = (w - 1) // 2 + 1
        Sb = (h - ah) * bw
        Sc = (h - ah) * (w - bw)
        ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))

        # 残りを横割り
        if (H - ah) > 1:
            bh = ((H - ah) - 1) // 2 + 1
            Sb = bh * w
            Sc = (h - ah - bh) * w
            ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))
        
print(ans)

Submission status: Wrong Answer

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

ans = float("inf")
for h, w in zip([H, W], [W, H]):
    # ah の全探索
    for ah in range(1, h):
        # 残りを縦割り
        Sa = ah * w
        bw = (w - 1) // 2 + 1
        Sb = (h - ah) * bw
        Sc = (h - ah) * (w - bw)
        ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))

        # 残りを横割り
        bh = ((H - ah) - 1) // 2 + 1
        Sb = bh * w
        Sc = (h - ah - bh) * w
        ans = min(ans, (max(Sa, Sb, Sc)-min(Sa, Sb, Sc)))
        
print(ans)

Submission status: Wrong Answer

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

ans = 1e19
##縦方向の探索
for i in range(1,H):
    tate = W*i##上から横に切る
    ##横に切る
    if i < H-1:
        mini = W*((H-i)//2)##分割したやつ
        ##print(tate,mini,H*W-mini-tate)
        ans = min(ans,max(tate,mini,H*W-mini-tate)-min(tate,mini,H*W-mini-tate))

    ##縦に切る
    mini = (H-i)*(W//2)
    ##print(tate,mini,H*W-mini-tate)
    ans = min(ans,max(tate,mini,H*W-tate-mini)-min(tate,mini,H*W-tate-mini))

##横方向の探索
for i in range(1,W):
    yoko = H*i
    if i < W-1:
        mini = H*((W-i)//2)
        ans = min(ans,max(yoko,mini,H*W-mini-yoko)-min(yoko,mini,H*W-yoko-mini))
    mini = (W-i)*(W//2)
    ans = min(ans,max(yoko,mini,H*W-yoko-mini)-min(yoko,mini,H*W-yoko-mini))

print(ans)

Submission status: Wrong Answer

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

ans = 1e19
##縦方向の探索
for i in range(1,H):
    tate = W*i##上から横に切る
    ##横に切る
    mini = W*((H-i)//2)##分割したやつ
    ##print(tate,mini,H*W-mini-tate)
    ans = min(ans,max(tate,mini,H*W-mini-tate)-min(tate,mini,H*W-mini-tate))

    ##縦に切る
    mini = (H-i)*(W//2)
    ##print(tate,mini,H*W-mini-tate)
    ans = min(ans,max(tate,mini,H*W-tate-mini)-min(tate,mini,H*W-tate-mini))

##横方向の探索
for i in range(1,W):
    yoko = H*i
    mini = H*((W-i)//2)
    ans = min(ans,max(yoko,mini,H*W-mini-yoko)-min(yoko,mini,H*W-yoko-mini))
    mini = (W-i)*(W//2)
    ans = min(ans,max(yoko,mini,H*W-yoko-mini)-min(yoko,mini,H*W-yoko-mini))

print(ans)

Submission status: Wrong Answer

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

if H == 2 and W == 2:
    print(1)
    exit()

ans = 1e19
##縦方向の探索
for i in range(1,H-1):
    tate = W*i##上から横に切る
    ##横に切る
    mini = W*((H-i)//2)##分割したやつ
    ##print(tate,mini,H*W-mini-tate)
    ans = min(ans,max(tate,mini,H*W-mini-tate)-min(tate,mini,H*W-mini-tate))

    ##縦に切る
    mini = (H-i)*(W//2)
    ##print(tate,mini,H*W-mini-tate)
    ans = min(ans,max(tate,mini,H*W-tate-mini)-min(tate,mini,H*W-tate-mini))

##横方向の探索
for i in range(1,W-1):
    yoko = H*i
    mini = H*((W-i)//2)
    ans = min(ans,max(yoko,mini,H*W-mini-yoko)-min(yoko,mini,H*W-yoko-mini))
    mini = (W-i)*(W//2)
    ans = min(ans,max(yoko,mini,H*W-yoko-mini)-min(yoko,mini,H*W-yoko-mini))

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 == 2 and W == 2:
    print(1)
    exit()

ans = 1e10
##縦方向の探索
for i in range(1,H-1):
    tate = W*i##上から横に切る
    ##横に切る
    mini = W*((H-i)//2)##分割したやつ
    ans = min(ans,max(tate,mini,H*W-mini)-min(tate,mini,H*W-mini))

    ##縦に切る
    mini = (H-i)*(W//2)
    ans = min(ans,max(tate,mini,H*W-tate-mini)-min(tate,mini,H*W-tate-mini))

##横方向の探索
for i in range(1,W-1):
    yoko = H*i
    mini = H*((W-i)//2)
    ans = min(ans,max(yoko,mini,H*W-mini-yoko)-min(yoko,mini,H*W-yoko-mini))
    mini = (W-i)*(W//2)
    ans = min(ans,max(yoko,mini,H*W-yoko-mini)-min(yoko,mini,H*W-yoko-mini))

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 == 2 and W == 2:
    print(1)
    exit()

ans = 1e10
##縦方向の探索
for i in range(1,H-1):
    tate = W*i##上から切る
    mini = (H-i)*(W//2)##半分に分割したちっこいほう
    ans = min(ans,max(tate,mini,H*W-tate-mini)-min(tate,mini,H*W-tate-mini))

##横方向の探索
for i in range(1,W-1):
    yoko = H*i
    mini = (W-i)*(W//2)
    ans = min(ans,max(yoko,mini,H*W-yoko-mini)-min(yoko,mini,H*W-yoko-mini))

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    h, w = map(int, input().split())
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        exit()

    ans = h * w
    for _ in range(2):
        i = h // 3
        j = w // 2

        x = [(i - 1, h - i + 1), (i, h - i), (i + 1, h - i - 1)]
        y = [(j - 1, w - j + 1), (j, w - j), (j + 1, w - j - 1)]
        for (u, v) in x:
            for (s, t) in y:
                s1 = u * w
                s2 = v * s
                s3 = v * t
                s = [s1, s2, s3]
                s_max, s_min = max(s), min(s)
                ans = min(ans, s_max - s_min)

        w, h = h, w
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h, w = map(int, input().split())
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        exit()

    h, w = max(h, w), min(h, w)
    i = h // 3
    j = w // 2

    ans = w
    x = [(i - 1, h - i + 1), (i, h - i), (i + 1, h - i - 1)]
    y = [(j - 1, w - j + 1), (j, w - j), (j + 1, w - j - 1)]
    for (u, v) in x:
        for (s, t) in y:
            s1 = u * w
            s2 = v * s
            s3 = v * t
            s = [s1, s2, s3]
            s_max, s_min = max(s), min(s)
            ans = min(ans, s_max - s_min)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h, w = map(int, input().split())
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        exit()

    h, w = max(h, w), min(h, w)
    i = h // 3
    j = w // 3

    ans = min(h, w)
    x = [(i - 1, h - i + 1), (i, h - i), (i + 1, h - i - 1)]
    y = [(j - 1, h - j + 1), (j, h - j), (j + 1, w - j - 1)]
    for (u, v) in x:
        for (s, t) in y:
            s1 = u * w
            s2 = v * s
            s3 = v * t
            s = [s1, s2, s3]
            s_max, s_min = max(s), min(s)
            ans = min(ans, s_max - s_min)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    best = (h*w)/3
    # wから割る方法
    col_num = int(round(best/h))
    remain_w = w - col_num
    h1 = h // 2
    h2 = h - h1
    min_area = min(remain_w*h1, remain_w*h2, col_num*h)
    max_area = max(remain_w*h1, remain_w*h2, col_num*h)
    num_w = max_area - min_area
    # hから割る方法
    row_num = int(round(best/w))
    remain_h = h - row_num
    w1 = w // 2
    w2 = w - w1
    min_area = min(remain_h*w1, remain_h*w2, row_num*w)
    max_area = max(remain_h*w1, remain_h*w2, row_num*w)
    num_h = max_area - min_area
    print(min(num_h,num_w))

Submission status: Wrong Answer

Solution code:
def abc062_c():
    h, w = map(int, input().split())
    # 2 <= h, w のため1の場合は考えない
    # どちらかが3の倍数ならぴったり3分割できる (平行2線)
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        return
    # それ以外は例2例3の割り方 (直交2線)
    ans = 10**12
    for x, y in [(w, h), (h, w)]:
        for i in range(1, x):
            sa = i * y
            sb = (x - i) * (y // 2)
            sc = (x - i) * (y - y // 2)
            diff = max(sa, sb, sc) - min(sa, sb, sc)
            ans = min(diff, ans)
    print(ans)

abc062_c()

Submission status: Wrong Answer

Solution code:
def abc062_c():
    h, w = map(int, input().split())
    # どちらかが3の倍数ならぴったり3分割できる (平行2線)
    if h % 3 == 0 or w % 3 == 0:
        print(0)
        return
    # 2 <= h, w のため1の場合は考えない
    # それ以外は例2例3の割り方 (直交2線)
    ans = 10**12
    lo, sh = (h, w) if h >= w else (w, h)
    for i in range(1, lo):
        sa = i * sh
        sb = (lo - i) * (sh // 2)
        sc = (lo - i) * (sh - sh // 2)
        diff = max(sa, sb, sc) - min(sa, sb, sc)
        ans = min(diff, ans)
    print(ans)

abc062_c()

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
sh = min(h, w)
lo = max(h, w)
for i in range(1, lo + 1):
    a = i * sh
    b = (lo - i) * (sh // 2)
    c = ((lo - i) * sh) - 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%3==0 or W%3==0:
  print(0)
elif H==2 or W==2:
  H,W=min(H,W),max(H,W)
  w1=W//3
  w2=W-w1
  s1=2*w1
  s2=w2
  ans1=s2-s1
  
  w1=W//3+1
  w2=W-w1
  s1=2*w1
  s2=w2
  ans2=s1-s2
  print(min(ans1,ans2))
else:
  h1=H//2
  h2=H-h1
  
  w1=W//3
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans1=max(s1,s2,s3)-min(s1,s2,s3)
  
  w1=W//3+1
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans2=max(s1,s2,s3)-min(s1,s2,s3)
  
  H,W=W,H
  h1=H//2
  h2=H-h1
  
  w1=W//3
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans3=max(s1,s2,s3)-min(s1,s2,s3)
  
  w1=W//3+1
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans4=max(s1,s2,s3)-min(s1,s2,s3)
  print(min(ans1,ans2,ans3,ans4))

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
  print(0)
elif H==2 or W==2:
  H,W=min(H,W),max(H,W)
  w1=W//3
  w2=W-w1
  s1=2*w1
  s2=w2
  ans1=s2-s1
  
  w1=W//3+1
  w2=W-w1
  s1=2*w1
  s2=w2
  ans2=s1-s2
  print(min(ans1,ans2))
else:
  h1=H//2
  h2=H-h1
  
  w1=W//3
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans1=max(s1,s2,s3)-min(s1,s2,s3)
  
  w1=W//3+1
  w2=W-w1
  s1=w1*H
  s2=w2*h1
  s3=w2*h2
  ans2=max(s1,s2,s3)-min(s1,s2,s3)
  print(min(ans1,ans2))

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
  print(0)
elif H%2==0 or W%2==0:
  #H%2=0
  if H%2==1:
    H,W=W,H
  
  w1=W//3
  w2=W-w1
  s1=w1*H
  s2=w2*H//2
  ans1=s2-s1
  
  w1=W//3+1
  w2=W-w1
  s1=w1*H
  s2=w2*H//2
  ans2=s1-s2
  print(min(ans1,ans2))
else:
  h1=H//2
  h2=H-h1
  
  w1=W//3
  w2=W-w1
  s1=w1*H
  s2=w2*h2
  s3=w2*h1
  ans1=max(s1,s2,s3)-min(s1,s2,s3)
  
  w1=W//3+1
  w2=W-w1
  s1=w1*H
  s2=w2*h2
  s3=w2*h1
  ans2=max(s1,s2,s3)-min(s1,s2,s3)
  print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
min_diff = float('inf')
for i in range(1, h + 1):
    sa = i * w
    sb = (h - i) * (w // 2)
    sc = (h - i) * (w - (w // 2))
    min_diff = min(min_diff, max(sa, sb, sc) - min(sa, sb, sc))
h, w = w, h
for i in range(1, h + 1):
    sa = i * w
    sb = (h - i) * (w // 2)
    sc = (h - i) * (w - (w // 2))
    min_diff = min(min_diff, max(sa, sb, sc) - min(sa, sb, sc))
print(min_diff)