Problem:


Score : 400 points

Problem Statement

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

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

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

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

Sample Input 1

3 5

Sample Output 1

0

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

Sample Input 2

4 5

Sample Output 2

2

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

Sample Input 3

5 5

Sample Output 3

4

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

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000



Sample input:
3 5


Sample output:
0

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

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

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

bx = min(H, W) // 2
cx = min(H, W) - bx
ans = 10 ** 18
for i in range(1, max(H, W)):
    a = min(H, W) * i

    b = (max(H, W) - i) * bx
    c = (max(H, W) - i) * cx

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w = na()
s = h * w
mid_h = h // 2
mid_w = w // 2
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ans = s
    for i in range(mid_w+1)[::-1]:
        s0 = h * i
        for j in range(mid_w, w+1):
            s1 = h * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
        for j in range(mid_h, h+1):
            s1 = (w - i) * (h - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
    for i in range(mid_h+1)[::-1]:
        s0 = w * i
        for j in range(mid_h, h+1):
            s1 = w * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
        for j in range(mid_w, w+1):
            s1 = (h - i) * (w - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w = na()
s = h * w
mid_h = h // 2
mid_w = w // 2
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ans = 10 ** 10
    for i in range(mid_w+1):
        s0 = h * i
        for j in range(mid_w, w+1):
            s1 = h * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
        for j in range(mid_h, h+1):
            s1 = (w - i) * (h - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
    for i in range(mid_h+1):
        s0 = w * i
        for j in range(mid_h, h+1):
            s1 = w * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
        for j in range(mid_w, w+1):
            s1 = (h - i) * (w - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w = na()
s = h * w
mid_h = h // 2
mid_w = w // 2
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ans = 10 ** 10
    for i in range(1, mid_w+1):
        s0 = h * i
        for j in range(mid_w, w+1):
            s1 = h * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
        for j in range(mid_h, h+1):
            s1 = (w - i) * (h - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
    for i in range(1, mid_h+1):
        s0 = w * i
        for j in range(mid_h, h+1):
            s1 = w * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
        for j in range(mid_w, w+1):
            s1 = (h - i) * (w - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans <= dif:
                break
            ans = dif
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w = na()
s = h * w
mid_h = h // 2
mid_w = w // 2
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ans = 10 ** 10
    for i in range(1, mid_w+1):
        s0 = h * i
        for j in range(mid_w, w+1):
            s1 = h * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            ans = dif
        for j in range(mid_h, h+1):
            s1 = (w - i) * (h - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            ans = dif
    for i in range(1, mid_h+1):
        s0 = w * i
        for j in range(mid_h, h+1):
            s1 = w * (j - i)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            ans = dif
        for j in range(mid_w, w+1):
            s1 = (h - i) * (w - j)
            s2 = s - s0 - s1
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            ans = dif
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

h, w = na()
mid_h = h // 2
mid_w = w // 2
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    ans = 10 ** 10
    for i in range(1, mid_w+1):
        s0 = h * i
        for j in range(mid_w, w+1):
            s1 = h * (j - i)
            s2 = h * (w - j)
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            else:
                ans = dif
        for j in range(mid_h, h+1):
            s1 = (w - i) * (h - j)
            s2 = (w - i) * j
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            else:
                ans = dif
    for i in range(1, mid_h+1):
        s0 = w * i
        for j in range(mid_h, h+1):
            s1 = w * (j - i)
            s2 = w * (h - j)
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            else:
                ans = dif
        for j in range(mid_w, w+1):
            s1 = (h - i) * (w - j)
            s2 = (h - i) * j
            dif = max(s0, s1, s2) - min(s0, s1, s2)
            if ans < dif:
                break
            else:
                ans = dif
    print(ans)

Submission status: Wrong Answer

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

anss = []

def vertical_twice(H,W):
    if W % 3 == 0:
        return 0
    else:
        return H

def vertical_horizontal(H,W):
    if W % 2 == 2:
        left_width = W//3+1
    else:
        left_width = W//3

    left_area = H*left_width
    right_width = W - left_width
    if H % 2 == 0:
        right_upper_area = H//2*right_width
        right_lower_area = H//2*right_width
    else:
        right_upper_area = (H//2+1)*right_width
        right_lower_area = H//2*right_width

    return (max([left_area,right_upper_area,right_lower_area]) - 
            min([left_area,right_upper_area,right_lower_area]))

anss.append(vertical_twice(H,W))
anss.append(vertical_twice(W,H))
anss.append(vertical_horizontal(H,W))
anss.append(vertical_horizontal(W,H))

print(min(anss))

Submission status: Wrong Answer

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

anss = []

def vertical_twice(H,W):
    if W % 3 == 0:
        return 0
    else:
        return H

def vertical_horizontal(H,W):
    left_width = W//3+1
    left_area = H*left_width
    right_width = W - left_width
    if H % 2 == 0:
        right_upper_area = H//2*right_width
        right_lower_area = H//2*right_width
        return left_area - right_upper_area
    else:
        right_upper_area = (H//2+1)*right_width
        right_lower_area = H//2*right_width
        return max(left_area,right_upper_area) - right_lower_area

anss.append(vertical_twice(H,W))
anss.append(vertical_twice(W,H))
anss.append(vertical_horizontal(H,W))
anss.append(vertical_horizontal(W,H))

print(min(anss))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
i = h//3
j = -(-h//3)
a = i*w
b = (h-i)*(w//2)
c = (h-i)*-(-w//2)
a2 = j*w
b2 = (h-j)*(w//2)
c2 = (h-j)*-(-w//2)
print(min(max(a,b,c)-min(a,b,c),max(a2,b2,c2)-min(a2,b2,c2)))

Submission status: Wrong Answer

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

    h1 = h // 2
    h2 = h - h1
    for w3 in range(w // 3, w // 3 + 1):
        s = [h * w3, h1 * (w - w3), h2 * (w - w3)]
        min_diff = min(min_diff, max(s) - min(s))

    w1 = w // 2
    w2 = w - w1
    for h3 in range(h // 3, h // 3 + 1):
        s = [w * h3, w1 * (h - h3), w2 * (h - h3)]
        min_diff = min(min_diff, max(s) - min(s))

    h1 = h // 3
    h2 = (h - h1) // 2
    h3 = h - h1 - h2
    s = [w * h1, w * h2, w * h3]
    min_diff = min(min_diff, max(s) - min(s))

    w1 = w // 3
    w2 = (w - w1) // 2
    w3 = w - w1 - w2
    s = [h * w1, h * w2, h * w3]
    min_diff = min(min_diff, max(s) - min(s))

    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)
else:
    min_diff = h * w
    h1 = h // 2
    h2 = h - h1
    for w3 in range(w // 3 - 2, w // 3 + 3):
        s = [h * w3, h1 * (w - w3), h2 * (w - w3)]
        min_diff = min(min_diff, max(s) - min(s))
    w1 = w // 2
    w2 = w - w1
    for h3 in range(h // 3 - 2, h // 3 + 3):
        s = [w * h3, w1 * (h - h3), w2 * (h - h3)]
        min_diff = min(min_diff, max(s) - min(s))
    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)
else:
    min_diff = h * w
    h1 = h // 2
    h2 = h - h1
    for w3 in range(w // 3 - 1, w // 3 + 3):
        s = [h * w3, h1 * (w - w3), h2 * (w - w3)]
        min_diff = min(min_diff, max(s) - min(s))
    w1 = w // 2
    w2 = w - w1
    for h3 in range(h // 3 - 1, h // 3 + 3):
        s = [w * h3, w1 * (h - h3), w2 * (h - h3)]
        min_diff = min(min_diff, max(s) - min(s))
    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)
else:
    min_diff = h * w
    h1 = h // 2
    h2 = h - h1
    for w3 in range(w // 3, w // 3 + 4):
        s = [h * w3, h1 * (w - w3), h2 * (w - w3)]
        min_diff = min(min_diff, max(s) - min(s))
    w1 = w // 2
    w2 = w - w1
    for h3 in range(h // 3, h // 3 + 4):
        s = [w * h3, w1 * (h - h3), w2 * (h - h3)]
        min_diff = min(min_diff, max(s) - min(s))
    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)
else:
    min_diff = h * w
    h1 = h // 2
    h2 = h - h1
    for w3 in range(w // 3, w // 3 + 3):
        s = [h * w3, h1 * (w - w3), h2 * (w - w3)]
        min_diff = min(min_diff, max(s) - min(s))
    w1 = w // 2
    w2 = w - w1
    for h3 in range(h // 3, h // 3 + 3):
        s = [w * h3, w1 * (h - h3), w2 * (h - h3)]
        min_diff = min(min_diff, max(s) - min(s))
    print(min_diff)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = abs(bc*ceil(W, 2)-a*W)
a = ceil(H, 3)
bc = H - a
ans2 = abs(bc*ceil(W, 2)-a*W)

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

a = H // 3
bc = H - a
ans3 = abs(bc*ceil(W, 2)-a*W)
a = ceil(H, 3)
bc = H - a
ans4 = abs(bc*ceil(W, 2)-a*W)

ans5 = abs(ceil(W, 3)*H-W//3*H)
ans6 = abs(ceil(H, 3)*W-H//3*W)

print(min(ans1, ans2, ans3, ans4, ans5, ans6))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans2 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

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

a = H // 3
bc = H - a
ans3 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans4 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

ans5 = abs(ceil(W, 3)*H-W//3*H)
ans6 = abs(ceil(H, 3)*W-H//3*W)

print(min(ans1, ans2, ans3, ans4, ans5, ans6))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans2 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

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

a = H // 3
bc = H - a
ans3 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans4 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

ans5 = abs(ceil(W, 3)*H-W//3*H)

print(min(ans1, ans2, ans3, ans4, ans5))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans2 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

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

a = H // 3
bc = H - a
ans3 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans4 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

ans5 = abs(ceil(W, 3)-W//3)

print(min(ans1, ans2, ans3, ans4, ans5))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans2 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

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

a = H // 3
bc = H - a
ans3 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans4 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

print(min(ans1, ans2, ans3, ans4))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))
a = ceil(H, 3)
bc = H - a
ans2 = max(abs(bc*ceil(W, 2)-a*W), abs(bc*(W//2)-a*W))

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = abs(bc*ceil(W, 2)-a*W)
a = ceil(H, 3)
bc = H - a
ans2 = abs(bc*(W//2)-a*W)

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H, W = MAP()

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

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

a = H // 3
bc = H - a
ans1 = abs(bc*(W//2)-a*W)
a = ceil(H, 3)
bc = H - a
ans2 = abs(bc*(W//2)-a*W)

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
value = H * W
#3つの長方形を(hi,wi)として考える

#h1 == H 横に3分割パターンか縦長一つ横二つパターン
#h1 + h2 = H 縦長1つ横2つパターン
#h1 + h3 = H 縦長2つ横1つパターン
##h1 + h2 + h3 = H 縦に3分割パターン

#横に3分割パターン
if W % 3 == 0:
  value = 0
else:
  value = H

#縦に3分割
if H % 3 == 0:
  value = 0
else:
  value = min(value, W)
  
#縦長一つ横二つパターン
if W % 3 != 0: #3で割り切れるならすでに上記で0になってる
  for i in range(3):
    w1 = W//3 - 1 + i
    s1 = H * w1
    w_amari = W - w1
    for j in range(3):
      h1 = H//2 - 1 + i
      s2 = h1 * w_amari
      s3 = H * W - s1 - s2
      value2 = max(s1,s2,s3) - min(s1,s2,s3)
      value = min(value, value2)
#縦長2つ横1つパターン      
if H % 3 != 0: #3で割り切れるならすでに上記で0になってる
  for i in range(3):
    h1 = H//3 - 1 + i
    s1 = h1 * W
    h_amari = H - h1
    for j in range(3):
      w1 = W//2 - 1 + i
      s2 = h_amari * w1
      s3 = H * W - s1 - s2
      value3 = max(s1,s2,s3) - min(s1,s2,s3)
      value = min(value, value3)
      
print(value)

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<W:H,W = W,H
    ans = W
    S1 = 0
    for i in range(1,H+1):
        S1 += W
        S2 = (H-i) * (W//2)
        S3 = (H-i) * (W-W//2)
        tmp = max(S1,S2,S3)-min(S1,S2,S3)
        ans = min(ans,tmp)
    
    print(ans)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H,W = MI()
    S = H*W
    ans = inf
    for h in range(H):
        if not (H-h)&1 or not W&1:
            piece = [h*W]
            piece.append(W*(H-h)/2)
            ans = min(ans, max(piece)-min(piece))
        else:
            if (H-h)&1:
                piece = [h*W]
                piece.append((H-h-1)*W/2)
                piece.append((H-h+1)*W/2)
                ans = min(ans, max(piece)-min(piece))
            if W&1:
                piece = [h*W]
                piece.append((W-1)*(H-h)/2)
                piece.append((W+1)*(H-h)/2)
                ans = min(ans, max(piece)-min(piece))
        

    for w in range(W):
        if not (W-w)&1 or not H&1:
            piece = [w*H]
            piece.append(H*(W-w)/2)
            ans = min(ans, max(piece)-min(piece))
        else:
            if (W-w)&1:
                piece = [h*W]
                piece.append((W-w-1)*H/2)
                piece.append((W-w+1)*H/2)
                ans = min(ans, max(piece)-min(piece))
            if H&1:
                piece = [h*W]
                piece.append((H-1)*(W-w)/2)
                piece.append((H+1)*(W-w)/2)
                ans = min(ans, max(piece)-min(piece))
    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:
    A = round(max(H, W)/3, 0) * min(H, W)
    B = (max(H, W)-round(max(H, W)/3, 0)) * round(min(H, W)/2, 0)
    C = H*W-A-B

    print(int(max(A, B, C) - min(A, B, C)))

Submission status: Wrong Answer

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

if H%3 == 0 or W%3 == 0:
    print(0)
else:
    A = round(max(H, W)/3, 0) * min(H, W)
    B = (max(H, W)-round(max(H, W)/3, 0)) * round(min(H, W)/2, 0)
    C = H*W-A-B

    print(max(A, B, C) - min(A, B, C))

Submission status: Wrong Answer

Solution code:
h,w=input().split()
h=int(h)
w=int(w)
hh=[0]*h
ww=[0]*w
ans=10**10


for i in range(1,h):
    s0 = i * w

    s1=(h-i) *(w//2)
    s2=(h-i) * (w-(w//2))
    ans=min(ans,max(s0,s1,s2) - min(s0,s1,s2))
    s1= (h-i)//2 * w
    s2= ((h-1)-(h-i)//2) * w
    ans=min(ans,max(s0,s1,s2) - min(s0,s1,s2))

temp=h
h=w
w=temp

for i in range(1, h):
    s0 = i * w

    s1 = (h - i) * (w // 2)
    s2 = (h - i) * (w - (w // 2))
    ans = min(ans, max(s0, s1, s2) - min(s0, s1, s2))

    s1 = (h - i) // 2 * w
    s2 = ((h - 1) - (h - i) // 2) * w
    ans = min(ans, max(s0, s1, s2) - min(s0, s1, s2))

print(ans)

Submission status: Wrong Answer

Solution code:
h,w=input().split()
h=int(h)
w=int(w)
ans=0
if(h%3==0 or w%3==0):
    ans=0
else:
    if(w>h):
        ans=abs((w//3+1) * h - (w-(w//3+1)) * (h//2) )
    else:
        ans = abs((h // 3 + 1) * w - (h - (h // 3 + 1)) * (w // 2))

print(ans)

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

ans = float('inf')

b = (H // 2)

for w in range(W+1):
    tmp_1, tmp_2, tmp_3 = H * w, b * (W-w), (H-b) * (W-w)
    tmp = max(tmp_1, tmp_2, tmp_3) - min(tmp_1, tmp_2, tmp_3)
    ans = min(ans, tmp)

d = (W // 2)
for h in range(H+1):
    tmp_1, tmp_2, tmp_3 = h*W, d * (H-h), (W-d) * (H-h)
    tmp = max(tmp_1, tmp_2, tmp_3) - min(tmp_1, tmp_2, tmp_3)
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = (int(i) for i in input().split())
sel_row1 = 10000
sel_row2 = 10000
sel_col1 = 10000
sel_col2 = 10000
sel_row3 = 10000
sel_col3 = 10000

if h&1:
    if h%3 == 0:
        sel_row1 = h/3
        sel_row2 = h/3
    else:
        sel_row1 = h//3
        sel_row2 = sel_row1 +1
else:
    sel_row1 = h//3 if h//3 != 0 else 10000
    sel_row2 = sel_row1 + 1
if w&1:
    if w%3 == 0:
        sel_col1 = w//3
        sel_col2 = w//3
    else:
        sel_col1 = w//3
        sel_col2 = sel_col1 + 1
else:
    sel_col1 = w//3 if w//3 != 0 else 10000
    sel_col2 = sel_col1 + 1
if h&1 and w&1:
    sel_row3 = sel_row2 + 1
    sel_col3 = sel_col2 + 1
else:
    sel_row3 = h//2
    sel_col3 = w//2

ans = set()
if h%2 == 0 and w%2 == 0:
    ans.add(abs((sel_row3 * sel_col3) - (h * sel_col3)))
    ans.add(abs((sel_row3 * sel_col3) - (w * sel_row3)))
    ans.add(abs((sel_row3*(w-sel_col1) - h*sel_col1)))
    ans.add(abs((sel_row3*(w-sel_col2) - h*sel_col2)))
    ans.add(abs((sel_row3*w - (h-sel_row3)*sel_col1)))
    ans.add(abs((sel_row3*w - (h-sel_row3)*sel_col2)))
    ans.add(abs((sel_col3*(h-sel_row1) - w*sel_row1)))
    ans.add(abs((sel_col3*(h-sel_row2) - w*sel_row2)))
    ans.add(abs((sel_col3*h - (w-sel_col3)*sel_row1)))
    ans.add(abs((sel_col3*h - (w-sel_col3)*sel_row2)))
    print(int(min(ans)))
elif h%2 == 0:
    ans.add(abs(sel_col1*h - (w - sel_col1)*sel_row3))
    ans.add(abs(sel_col2*h - (w - sel_col2)*sel_row3))
    ans.add(abs(sel_row3*sel_col3 - h*(w - sel_col3)))
    ans.add(abs(sel_col3*sel_col3 - (h - sel_row3)*w))
    ans.add(abs(sel_row1*w - sel_row2*w))
    ans.add(abs(h*sel_col1 - h*sel_col2))
    print(int(min(ans)))
elif w%2 == 0:
    ans.add(abs(sel_row1*w - (h - sel_row1)*sel_col3))
    ans.add(abs(sel_row2*w - (h - sel_row2)*sel_col3))
    ans.add(abs(sel_row3*sel_col3 - h*(w - sel_col3)))
    ans.add(abs(sel_col3*sel_col3 - (h - sel_row3)*w))
    ans.add(abs(h*sel_col1 - h*sel_col2))
    ans.add(abs(sel_row1*w - sel_row2*w))
    print(int(min(ans)))
else:
    ans.add(abs(sel_row1*w - sel_row2*w))
    ans.add(abs(sel_col1*h - sel_col2*h))

    ans_set = {(sel_row1*w),(h-sel_row1)*(sel_col1),(h-sel_row1)*(w-sel_col1)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row1*w),(h-sel_row1)*(sel_col2),(h-sel_row1)*(w-sel_col2)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row1*w),(h-sel_row1)*(sel_col3),(h-sel_row1)*(w-sel_col3)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row2*w),(h-sel_row2)*(sel_col1),(h-sel_row2)*(w-sel_col1)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row2*w),(h-sel_row2)*(sel_col2),(h-sel_row2)*(w-sel_col2)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row2*w),(h-sel_row2)*(sel_col3),(h-sel_row2)*(w-sel_col3)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row3*w),(h-sel_row3)*(sel_col1),(h-sel_row3)*(w-sel_col1)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row3*w),(h-sel_row3)*(sel_col2),(h-sel_row3)*(w-sel_col2)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row3*w),(h-sel_row3)*(sel_col3),(h-sel_row3)*(w-sel_col3)}
    ans.add(max(ans_set) - min(ans_set))


    ans_set = {(sel_row1)*(w-sel_col1),h*(sel_col1),(h-sel_row1)*(w-sel_col1)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row1)*(w-sel_col2),h*(sel_col2),(h-sel_row1)*(w-sel_col2)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row1)*(w-sel_col3),h*(sel_col3),(h-sel_row1)*(w-sel_col3)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row2)*(w-sel_col1),h*(sel_col1),(h-sel_row2)*(w-sel_col1)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row2)*(w-sel_col2),h*(sel_col2),(h-sel_row2)*(w-sel_col2)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row2)*(w-sel_col3),h*(sel_col3),(h-sel_row2)*(w-sel_col3)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row3)*(w-sel_col1),h*(sel_col1),(h-sel_row3)*(w-sel_col1)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row3)*(w-sel_col2),h*(sel_col2),(h-sel_row3)*(w-sel_col2)}
    ans.add(max(ans_set) - min(ans_set))
    ans_set = {(sel_row3)*(w-sel_col3),h*(sel_col3),(h-sel_row3)*(w-sel_col3)}
    ans.add(max(ans_set) - min(ans_set))

    print(int(min(ans)))