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())
tmp=h*w
tmp_2=h*w
tmp_3=h*w
tmp_4=h*w
for i in range(1,h+1):
    half_h=h-i
    area_1=i*w
    area_2=half_h//2*w
    area_3=(half_h-(half_h//2))*w
    ans=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans<tmp:
        tmp=ans
for j in range(1,w+1):
    half_w=w-j
    area_1=h*j
    area_2=(half_w//2)*h
    area_3=half_w-half_w//2*h
    ans_2=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_2<tmp_2:
        tmp_2=ans_2
for i in range(1,h+1):
    half_w=w//2
    area_1=i*half_w
    area_2=i*(w-half_w)
    area_3=(h-i)*w
    ans_3=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_3<tmp_3:
        tmp_3=ans_3
for j in range(1,w+1):
    half_h=h//2
    area_1=h*j
    area_2=(w-j)*half_h
    area_3=(w-j)*(h-half_h)
    ans_4=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_4<tmp_4:
        tmp_4=ans_4
print(min(tmp,tmp_2,tmp_3,tmp_4))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
tmp=h*w
tmp_2=h*w
tmp_3=h*w
tmp_4=h*w
for i in range(1,h):
    half_h=h-i
    area_1=i*w
    area_2=half_h//2*w
    area_3=(half_h-(half_h//2))*w
    ans=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans<tmp:
        tmp=ans
for j in range(1,w):
    half_w=w-j
    area_1=h*j
    area_2=(half_w//2)*h
    area_3=half_w-half_w//2*h
    ans_2=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_2<tmp_2:
        tmp_2=ans_2
for i in range(1,h):
    half_w=w//2
    area_1=i*half_w
    area_2=i*(w-half_w)
    area_3=(h-i)*w
    ans_3=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_3<tmp_3:
        tmp_3=ans_3
for j in range(1,w):
    half_h=h//2
    area_1=h*j
    area_2=(w-j)*half_h
    area_3=(w-j)*(h-half_h)
    ans_4=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_4<tmp_4:
        tmp_4=ans_4
print(min(tmp,tmp_2,tmp_3,tmp_4))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
tmp=h*w
tmp_2=h*w
tmp_3=h*w
tmp_4=h*w
for i in range(1,h):
    half_h=h-i
    area_1=i*w
    area_2=half_h//2*w
    area_3=(half_h-(half_h//2))*w
    ans=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans<tmp:
        tmp=ans
for j in range(1,w):
    half_w=w-j
    area_1=h*j
    area_2=(half_w//2)*h
    area_3=half_w-(half_w//2)*h
    ans_2=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_2<tmp_2:
        tmp_2=ans_2
for i in range(1,h):
    half_w=w//2
    area_1=i*half_w
    area_2=i*(w-half_w)
    area_3=(h-i)*w
    ans_3=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_3<tmp_3:
        tmp_3=ans_3
for j in range(1,w):
    half_h=h//2
    area_1=h*j
    area_2=(w-j)*half_h
    area_3=(w-j)*(h-half_h)
    ans_4=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_4<tmp_4:
        tmp_4=ans_4
print(min(tmp,tmp_2,tmp_3,tmp_4))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
tmp=10**5*2
tmp_2=10**5*2
tmp_3=10**5*2
tmp_4=10**5*2
for i in range(1,h):
    half_h=h-i
    area_1=i*w
    area_2=half_h//2*w
    area_3=half_h-(half_h//2)*w
    ans=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans<tmp:
        tmp=ans
for j in range(1,w):
    half_w=w-j
    area_1=h*j
    area_2=half_w//2*h
    area_3=half_w-(half_w//2)*h
    ans_2=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_2<tmp_2:
        tmp_2=ans_2
for i in range(1,h):
    half_w=w//2
    area_1=i*half_w
    area_2=i*(w-half_w)
    area_3=(h-i)*w
    ans_3=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_3<tmp_3:
        tmp_3=ans_3
for j in range(1,w):
    half_h=h//2
    area_1=h*j
    area_2=(w-j)*half_h
    area_3=(w-j)*(h-half_h)
    ans_4=max(area_1,area_2,area_3)-min(area_1,area_2,area_3)
    if ans_4<tmp_4:
        tmp_4=ans_4
print(min(tmp,tmp_2,tmp_3,tmp_4))

Submission status: Wrong Answer

Solution code:
INF = 10**10

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

res = INF
if H % 3 == 0 or W % 3 == 0:
	res = 0

def cmp(cH, cW):
	res = INF
	w = cW // 2
	for h in range(1, cH):
		s1 = h * cW
		s2 = (cH - h) * w
		s3 = (cH - h) * (cW - w)
		minS = min(s1, min(s2, s3))
		maxS = max(s1, max(s2, s3))
		res = min(res, maxS - minS)
	return res
	 
res = min(res, cmp(H,W))
res = min(res, cmp(W,H))

print(res)

Submission status: Wrong Answer

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

hw = raw_input()
hw = hw.split()
hw = map(int,hw)

h = max(hw)
w = min(hw)

ans = 0

if h % 3 == 0 or w % 3 == 0:
    ans = 0
elif w % 2 == 0:
    ans = w/2
elif h % 2 == 0:
    ans = h/2
elif h == w:
    ans = (h/3+1)**2
else:
    ans = (h/3)*2+1

print ans

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    def solve(H, W):
        ans = float('inf')
        for h in range(1, H - 1):
            sa = W * h
            w = W // 2
            sb = w * (H - h)
            if W - w > 0:
                sc = (W - w) * (H - h)
                ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))

            rh = (H - h) // 2
            if (H - h - rh) > 0:
                sb = W * rh
                sc = W * (H - h - rh)
                ans = min(ans, max(sa, sb, sc) - min(sa, sb, sc))
        return ans
    ans = min(solve(H, W), solve(W, H))
    print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    def solve(H, W):
        ans = float('inf')
        for h in range(1, H - 1):
            sa = W * h
            w = int(W // 2)
            sb = w * (H - h)
            sc = (W - w) * (H - h)
            s = sorted([sa, sb, sc])
            ans = min(ans, s[-1] - s[0])

            rh = int((H - h) // 2)
            if (H - h - rh) <= 0:
                continue
            sb = W * rh
            sc = W * (H - h - rh)
            s = sorted([sa, sb, sc])
            ans = min(ans, s[-1] - s[0])
        return ans
    ans = min(solve(H, W), solve(W, H))
    print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    def solve(H, W):
        ans = float('inf')
        for h in range(1, H - 1):
            sa = W * h
            w = int(W / 2)
            sb = w * (H - h)
            sc = (W - w) * (H - h)
            s = sorted([sa, sb, sc])
            ans = min(ans, s[-1] - s[0])

            rh = int((H - h) / 2)
            if (H - h - rh) <= 0:
                continue
            sb = W * rh
            sc = W * (H - h - rh)
            s = sorted([sa, sb, sc])
            ans = min(ans, s[-1] - s[0])
        return ans
    ans = min(solve(H, W), solve(W, H))
    print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    def solve(H, W):
        ans = float('inf')
        for h in range(1, H - 1):
            sa = W * h
            w = int(W / 2)
            sb = w * (H - h)
            sc = (W - w) * (H - h)
            s = sorted([sa, sb, sc])
            ans = min(ans, s[-1] - s[0])

            rh = int((H - h) / 2)
            sb = W * rh
            sc = W * (H - h - rh)
            s = sorted([sa, sb, sc])
            ans = min(ans, s[-1] - s[0])
        return ans
    ans = min(solve(H, W), solve(W, H))
    print(ans)

Submission status: Wrong Answer

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    def solve(H, W):
        ans = float('inf')
        for h in range(1, H - 1):
            sa = W * h
            w = int(W / 2)
            sb = w * (H - h)
            sc = (W - w) * (H - h)
            s = sorted([sa, sb, sc])
            ans = min(ans, s[-1] - s[0])
        return ans
    ans = min(solve(H, W), solve(W, H))
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import numpy as np


if __name__ == "__main__":
    H, W = map(int, raw_input().split())

    if H % 3 == 0 or W % 3 == 0:
        print 0
    else:
        x = max(H, W)
        y = min(H, W)
        if y % 2 == 0:
            t = math.floor(x/3.)
            d_s1 = math.fabs(y * t - y/2.*(x-t))
            t = math.ceil(x/3.)
            d_s2 = math.fabs(y * t - y/2.*(x-t))
            print int(min(d_s1, d_s2))
        else:
            t = math.floor(x/3.)
            s = math.floor(y/2.)
            s1 = y * t
            s2 = s * (x - t)
            s3 = (x-t) * (y-s)
            maximum = max(s1, s2, s3)
            minimum = min(s1, s2, s3)
            d1 = maximum - minimum
            t = math.ceil(x/3.)
            s4 = y * t
            s5 = s * (x - t)
            s6 = (x-t) * (y-s)
            maximum = max(s4, s5, s6)
            minimum = min(s4, s5, s6)
            d2 = maximum - minimum

            print int(min(d1, d2))

Submission status: Wrong Answer

Solution code:
H,W = [int(i) for i in input().split(" ")]
if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    if H >= W:
        k = H//2
        Smax = W*k
        H -= k
        l = max(W,H)//2
        Smin = l*min(W,H)
        print(Smax-Smin)
    else:
        k = W//2
        Smax= H*k
        W -= k
        l = max(H,W)//2
        Smin = min(H,W)*l
        print(Smax-Smin)

Submission status: Wrong Answer

Solution code:
def calcarea(h, w):
    ans = h * w
    for a in range(1, h-1):
        sa = a * W
        sb = int(W/2) * (h-a)
        sc = h * w - sa - sb
        ans1 = max(sa, sb, sc) - min(sa, sb, sc)
        sb = int((h-a)/2) * w
        sc = h * w - sa - sb
        ans2 = max(sa, sb, sc) - min(sa, sb, sc)
        ans = min(ans, ans1, ans2)
    return ans

H, W = map(int, input().split())
print(min(calcarea(H, W), calcarea(W, H)))

Submission status: Wrong Answer

Solution code:


def divide_h(h, w):
    min_diff = 1e12
    for x in range(1, h-1):
        h_rem = h - x
        s_A = x * w

        # vertical devide
        w1 = w // 2
        s_B = w1 * h_rem
        s_C = (w - w1) * h_rem

        tmp = max(s_A, s_B, s_C) - min(s_A, s_B, s_C)
        if min_diff > tmp:
            min_diff = tmp

        # horizontal devide
        h1 = h_rem // 2
        s_B = w * h1
        s_C = w * (h_rem - h1)
        tmp = max(s_A, s_B, s_C) - min(s_A, s_B, s_C)
        if min_diff > tmp:
            min_diff = tmp

    return min_diff



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

a1 = divide_h(H, W)
a2 = divide_h(W, H)

print(min(a1, a2))

Submission status: Wrong Answer

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

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

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 main():
    h,w = LI()
    if h < w:
        h,w = w,h

    if h % 3 == 0 or w % 3 == 0:
        return 0

    r = inf
    if h >= 3:
        r = w
    w1 = w//2
    w2 = (w+1)//2
    for t in range(1,h):
        a = [t*w, (h-t) * w1, (h-t) * w2]
        tr = max(a) - min(a)
        if tr < r:
            r = tr
    return r



print(main())

Submission status: Wrong Answer

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

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

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 main():
    h,w = LI()
    if h < w:
        h,w = w,h

    if h % 3 == 0 or w % 3 == 0:
        return 0

    r0 = inf
    if h >= 3:
        r0 = w
    t = h // 3
    a = [t*w, (h-t) * (w//2), (h-t) * ((w+1)//2)]
    r = max(a) - min(a)
    if t == 0:
        r = inf
    t = h // 3 - 1
    a = [t*w, (h-t) * (w//2), (h-t) * ((w+1)//2)]
    r3 = max(a) - min(a)
    if t <= 0:
        r3 = inf
    t = h // 3 + 1
    a = [t*w, (h-t) * (w//2), (h-t) * ((w+1)//2)]
    r2 = max(a) - min(a)
    return min(r,r2,r0,r3)



print(main())

Submission status: Wrong Answer

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

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

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 main():
    h,w = LI()
    if h < w:
        h,w = w,h

    if h % 3 == 0 or w % 3 == 0:
        return 0

    r0 = inf
    if h >=3:
        r0 = w
    t = h // 3
    a = [t*w, (h-t) * (w//2), (h-t) * ((w+1)//2)]
    r = max(a) - min(a)
    if t == 0:
        r = inf
    t = h // 3 + 1
    a = [t*w, (h-t) * (w//2), (h-t) * ((w+1)//2)]
    r2 = max(a) - min(a)
    if t == h:
        r2 = inf
    return min(r,r2,r0)



print(main())

Submission status: Wrong Answer

Solution code:
def calc(x, y):
    if(x % 3 == 0 or y % 3 == 0):
        return 0
    ans = 10 ** 10
    for i in range(x,-1,-1):
        s1 = i * y
        s2 = (x - i) * (y // 2)
        if(abs(s1 - s2) < ans):
            ans = abs(s1 - s2)
    return ans

H, W = map(int, input().split())
print(min(calc(H,W), calc (W,H)))

Submission status: Wrong Answer

Solution code:
from __future__ import division


def get_min(w, h):
    m = 1e10
    for x in [w // 3, w // 3 + 1]:
        if x == 0: continue
        a = [x*h, (w-x)*(h//2), (w-x)*(h - h//2)]
        # print a
        m = min(m, max(a) - min(a))
    return m

digits = raw_input()
a, b = [int(x) for x in digits.split(' ')]
if a % 3 == 0 or b % 3 == 0:
    print 0
else:
    print min([get_min(a, b), get_min(b, a)])

Submission status: Wrong Answer

Solution code:
from __future__ import division


def get_min(w, h):
    m = 1e10
    for x in [w // 3, w // 3 + 1]:
        if x == 0: continue
        a = [x*h, (w-x)*(h//2), (w-x)*(h - h//2)]
        print a
        m = min(m, max(a) - min(a))
    return m

digits = raw_input()
a, b = [int(x) for x in digits.split(' ')]
if a % 3 == 0 or b % 3 == 0:
    print 0
else:
    print min([get_min(a, b), get_min(b, a)])

Submission status: Wrong Answer

Solution code:
from __future__ import division


def get_min(w, h):
    m = 1e10
    for x in [w // 3, w // 3 + 1]:
        a = [x*h, (w-x)*(h//2), (w-x)*(h - h//2)]
        diffs = [abs(a[0]-a[1]), abs(a[1]-a[2]), abs(a[0]-a[2])]
        m = min(m, max(diffs))
    return m

digits = raw_input()
a, b = [int(x) for x in digits.split(' ')]
if a % 3 == 0 or b % 3 == 0:
    print 0
else:
    print min([get_min(a, b), get_min(b, a)])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

smin=h/3*w
smax=(h-h/3*2)*w
d=smax-smin

def calc(h,w,d):
    for hk in range(1,h):
        h_temp=h-hk
        s1=h_temp*(w/2)
        s2=h_temp*(w-w/2)
        s3=hk*w
        d_temp=np.max([s1,s2,s3])-np.min([s1,s2,s3])
        if d_temp<d:
            ans=hk
            d=d_temp
    return d

print(np.min([d,calc(h,w,d),calc(w,h,d)]))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calculate_t(x, y):
    min_d = 1e12
    for i in range(x):
        s1 = (y//2)*i
        s2 = y*(x-i)
        s3 = (y-y//2)*i
        min_d = min(min_d, max(s1,s2,s3) - min(s1,s2,s3))
    return min_d
def calculate_3(x, y):
    min_d = 1e12
    for i in range(x):
        w1 = i
        w2 = (x-i)//2
        w3 = x - w1 -w2
        min_d = min(min_d, min(w1, w2, w3)*y)
    return min_d
if (h*w)%3 == 0:
    print(0)
else:
    d1 = calculate_t(h,w)
    d2 = calculate_t(w,h)
    d3 = calculate_3(h,w)
    d4 = calculate_3(w,h)
    print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calculate_t(x, y):
    min_d = 1e12
    for i in range(x):
        s1 = (y//2)*i
        s2 = y*(x-i)
        s3 = (y-y//2)*i
        min_d = min(min_d, max(s1,s2,s3) - min(s1,s2,s3))
    return min_d
def calculate_3(x, y):
    for i in range(x):
        w1 = i
        w2 = (x-i)//2
        w3 = x - w1 -w2
        return min(w1, w2, w3)*y
if (h*w)%3 == 0:
    print(0)
else:
    d1 = calculate_t(h,w)
    d2 = calculate_t(w,h)
    d3 = calculate_3(h,w)
    d4 = calculate_3(w,h)
    print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calculate_t(x, y):
    min_d = 1e12
    for i in range(x):
        s1 = (y//2)*i
        s2 = y*(x-i)
        s3 = (y-y//2)*i
        min_d = min(min_d, max(s1,s2,s3) - min(s1,s2,s3))
    return min_d
def calculate_3(x, y):
    return x*(y%3)
if (h*w)%3 == 0:
    print(0)
else:
    d1 = calculate_t(h,w)
    d2 = calculate_t(w,h)
    d3 = calculate_3(h,w)
    d4 = calculate_3(w,h)
    print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, raw_input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
third = h * w / 3.0
if h > w:
    eh, el = h, w
else:
    eh, el = w, h
edge1 = int(third) // el
#
eh_rest = eh - edge1
if el % 2 == 0 or eh_rest % 2 == 0:
    slist0 = [el * edge1, el * eh_rest // 2]
else:
    if el > eh_rest:
        eh1, el1 = el, eh_rest
    else:
        eh1, el1 = eh_rest, el
    slist0 = [eh1 * edge1,
              ((eh1 // 2) + 1) * el1,
              (eh1 // 2) * el1]
edge1 += 1
eh_rest = eh - edge1
if el % 2 == 0 or eh_rest % 2 == 0:
    slist1 = [el * edge1, el * eh_rest // 2]
else:
    if el > eh_rest:
        eh1, el1 = el, eh_rest
    else:
        eh1, el1 = eh_rest, el
    slist1 = [eh1 * edge1,
              ((eh1 // 2) + 1) * el1,
              (eh1 // 2) * el1]
s0 = max(slist0) - min(slist0)
s1 = max(slist1) - min(slist1)
print(min([s0, s1]))

Submission status: Wrong Answer

Solution code:
h, w = map(int, raw_input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
third = h * w / 3.0
if h > w:
    eh, el = h, w
else:
    eh, el = w, h
edge1 = int(third) // el
#
eh_rest = eh - edge1
if el % 2 == 0 or eh_rest % 2 == 0:
    slist0 = [el * edge1 - el * eh_rest // 2]
else:
    if el > eh_rest:
        eh1, el1 = el, eh_rest
    else:
        eh1, el1 = eh_rest, el
    slist0 = [eh1 * edge1 - ((eh1 // 2) + 1) * el1,
              ((eh1 // 2) + 1) * el1 - (eh1 // 2) * el1,
              (eh1 // 2) * el1 - eh1 * edge1]
edge1 += 1
eh_rest = eh - edge1
if el % 2 == 0 or eh_rest % 2 == 0:
    slist1 = [el * edge1 - el * eh_rest // 2]
else:
    if el > eh_rest:
        eh1, el1 = el, eh_rest
    else:
        eh1, el1 = eh_rest, el
    slist1 = [eh1 * edge1 - ((eh1 // 2) + 1) * el1,
              ((eh1 // 2) + 1) * el1 - (eh1 // 2) * el1,
              (eh1 // 2) * el1 - eh1 * edge1]
s0 = max(map(abs, slist0))
s1 = max(map(abs, slist1))
print(min([s0, s1]))

Submission status: Wrong Answer

Solution code:
h, w = map(int, raw_input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
    exit()
third = h * w / 3.0
if h > w:
    eh, el = h, w
else:
    eh, el = w, h
edge1 = int(third) / el
#
eh_rest = eh - edge1
if el % 2 == 0 or eh_rest % 2 == 0:
    slist0 = [el * edge1 - el * eh_rest / 2]
else:
    if el > eh_rest:
        eh1, el1 = el, eh_rest
    else:
        eh1, el1 = eh_rest, el
    slist0 = [eh1 * edge1 - (int(eh1 / 2) + 1) * el1,
              (int(eh1 / 2) + 1) * el1 - int(eh1 / 2) * el1,
              int(eh1 / 2) * el1 - eh1 * edge1]
edge1 += 1
eh_rest = eh - edge1
if el % 2 == 0 or eh_rest % 2 == 0:
    slist1 = [el * edge1 - el * eh_rest / 2]
else:
    if el > eh_rest:
        eh1, el1 = el, eh_rest
    else:
        eh1, el1 = eh_rest, el
    slist1 = [eh1 * edge1 - (int(eh1 / 2) + 1) * el1,
              (int(eh1 / 2) + 1) * el1 - int(eh1 / 2) * el1,
              int(eh1 / 2) * el1 - eh1 * edge1]
s0 = max(map(abs, slist0))
s1 = max(map(abs, slist1))
print(min([s0, s1]))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calculate_t(x, y):
    min_d = x*y + 1
    for i in range(x):
        s1 = (y//2)*i
        s2 = y*(x-i)
        s3 = (y-y//2)*i
        new_d = max(s1,s2,s3) - min(s1,s2,s3)
        if min_d > new_d:
            min_d = new_d
    return min_d
def calculate_3(x, y):
    return x*(y%3)

if (h*w)%3 == 0:
    print(0)
else:
    d1 = calculate_t(h,w)
    d2 = calculate_t(w,h)
    d3 = calculate_3(h,w)
    d4 = calculate_3(w,h)
    print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
def calculate_t(x, y):
    min_d = x*y + 1
    for i in range(x):
        s1 = (y//2)*i
        s2 = y*(x-i)
        s3 = (y-y//2)*i
        new_d = max(s1,s2,s3) - min(s1,s2,s3)
        if min_d > new_d:
            min_d = new_d
    return min_d
def calculate_3(x, y):
    return x*(y%3)

if h%3 == 0 or w%3 == 0:
    print(0)
else:
    d1 = calculate_t(h,w)
    d2 = calculate_t(w,h)
    d3 = calculate_3(h,w)
    d4 = calculate_3(w,h)
    print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h%3 == 0 or w%3 ==0:
    print(0)
else:
    def calculate_t(x, y):
        min_d = x*y
        for i in range(x):
            s1 = (y//2)*i
            s2 = y*(x-i)
            s3 = x*y - s1 - s2
            new_d = max(s1,s2,s3) - min(s1,s2,s3)
            if min_d > new_d:
                min_d = new_d
            continue
        return min_d
    def calculate_3(x, y):
        h1 = y//3
        h2 = y//3 + y%3
        return x*(h2-h1)
    d1 = calculate_t(h,w)
    d2 = calculate_3(h,w)
    if h == w:
        print(min(d1,d2))
    else:
        d3 = calculate_t(w, h)
        d4 = calculate_3(w, h)
        print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h%3 == 0 or w%3 ==0:
    print(0)
else:
    def calculate_t(x, y):
        s1 = 0
        s2 = y*x
        rng = range(1, x) if x < 1000 else range(x//3, x*4//5)
        d = s2 - s1
        for i in rng:
            s1 = (y//2)*i
            s2 = y*(x-i)
            new_d = abs(s2 - s1)
            if d > new_d:
                d = new_d
                continue
            else:
                break
        return d
    def calculate_3(x, y):
        h1 = y//3
        h2 = y//3 + y%3
        return x*(h2-h1)
    d1 = calculate_t(h,w)
    d2 = calculate_3(h,w)
    if h == w:
        print(min(d1,d2))
    else:
        d3 = calculate_t(w, h)
        d4 = calculate_3(w, h)
        print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h%3 == 0 or w%3 ==0:
    print(0)
else:
    def calculate_t(x, y):
        s1 = 0
        s2 = y*x
        rng = range(1, x) if x < 1000 else range(x//3, x*4//5)
        d = s2 - s1
        for i in rng:
            s1 = (y//2)*i
            s2 = y*(x-i)
            new_d = s2 - s1
            print(d, new_d)
            if d > new_d > 0:
                d = new_d
                continue
            else:
                break
        return d
    def calculate_3(x, y):
        h1 = y//3
        h2 = y//3 + y%3
        return x*(h2-h1)
    d1 = calculate_t(h,w)
    d2 = calculate_3(h,w)
    if h == w:
        print(min(d1,d2))
    else:
        d3 = calculate_t(w, h)
        d4 = calculate_3(w, h)
        print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h%3 == 0 or w%3 ==0:
    print(0)
else:
    def calculate_t(x, y):
        d = h*w + 1
        i = x//2
        s1, s2 = 0, d
        rng = range(x) if x < 1000 else range(x//3, x*4//5)
        new_d = h*w
        for i in rng:
            s1 = (y//2)*i
            s2 = y*(x-i)
            s3 = (y-y//2)*i
            new_d = max(s1,s2,s3)- min(s1,s2,s3)
            if d > new_d:
                d = new_d
                continue
            else:
                break
        return d
    def calculate_3(x, y):
        h1 = y//3
        h2 = y//3 + y%3
        return x*(h2-h1)
    d1 = calculate_t(h,w)
    d2 = calculate_3(h,w)
    if h == w:
        print(min(d1,d2))
    else:
        d3 = calculate_t(w, h)
        d4 = calculate_3(w, h)
        print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
# wrong answer
h, w = map(int, raw_input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    edge = [[0,0], [0,0], [0,0]]
    third = h * w / 3.0
    if h > w:
        eh, el = h, w
    else:
        eh, el = w, h
    edge[0][0] = el
    edge[0][1] = int(third) / el
    if third - edge[0][0]*edge[0][1] > el / 2.0:
        edge[0][1] += 1
    eh -= edge[0][1]
    if eh < el:
        tmp = eh
        eh = el
        el = tmp
    if el % 2 == 0:
        edge[1][0] = eh
        edge[2][0] = eh
        edge[1][1] = el / 2
        edge[2][1] = el / 2
    elif eh % 2 == 0:
        edge[1][0] = el
        edge[2][0] = el
        edge[1][1] = eh / 2
        edge[2][1] = eh / 2
    else:
        edge[1][0] = el
        edge[2][0] = el
        edge[1][1] = eh / 2 + 1
        edge[2][1] = eh / 2

    print(max(map(abs, [edge[0][0]*edge[0][1] - edge[1][0]*edge[1][1],
                        edge[1][0]*edge[1][1] - edge[2][0]*edge[2][1],
                        edge[2][0]*edge[2][1] - edge[0][0]*edge[0][1]])))

Submission status: Wrong Answer

Solution code:
h, w = map(int, raw_input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    edge = [[0,0], [0,0], [0,0]]
    third = h * w / 3.0
    if h > w:
        eh, el = h, w
    else:
        eh, el = w, h
    edge[0][0] = el
    edge[0][1] = int(third) / el
    if third - edge[0][0]*edge[0][1] > el / 2.0:
        edge[0][1] += 1
    eh -= edge[0][1]
    if eh < el:
        tmp = eh
        eh = el
        el = tmp
    if el % 2 == 0:
        edge[1][0] = eh
        edge[2][0] = eh
        edge[1][1] = el / 2
        edge[2][1] = el / 2
    elif eh % 2 == 0:
        edge[1][0] = el
        edge[2][0] = el
        edge[1][1] = eh / 2
        edge[2][1] = eh / 2
    else:
        edge[1][0] = el
        edge[2][0] = el
        edge[1][1] = eh / 2 + 1
        edge[2][1] = eh / 2

    print(edge)
    print(max(map(abs, [edge[0][0]*edge[0][1] - edge[1][0]*edge[1][1],
                        edge[1][0]*edge[1][1] - edge[2][0]*edge[2][1],
                        edge[2][0]*edge[2][1] - edge[0][0]*edge[0][1]])))

Submission status: Wrong Answer

Solution code:
H,W=map(int,raw_input().split())
a=max([H,W]);b=min([H,W])

if H%3==0 or W%3==0:ans=0
else:
	ans=H*W
	for i in range(a):
		s1=b*i
		if (a-i)%2==0:
			s2=[(a-i)/2*b]
		else:
			s2=[(a-i)/2*b,(a-i)/2*b+b]
		if b%2==0:
			s3=[(a-i)*(b/2)]
		else:
			s3=[(a-i)*(b/2),(a-i)*(b/2)+(a-i)]

		vx=max([s1,max(s2)])
		vn=min([s1,min(s2)])
		px=max([s1,max(s3)])
		pn=min([s1,min(s3)])

		ans=min([ans,vx-vn,px-pn])

print ans

Submission status: Wrong Answer

Solution code:
H,W=map(int,raw_input().split())
a=max([H,W]);b=min([H,W])

if H%3==0 or W%3==0:ans=0
else:
	ans=H*W
	for i in range(a/2+1):
		s1=b*i
		if (a-i)%2==0:
			s2=[(a-i)/2*b]
		else:
			s2=[(a-i)/2*b,(a-i)/2*b+b]
		if b%2==0:
			s3=[(a-i)*(b/2)]
		else:
			s3=[(a-i)*(b/2),(a-i)*(b/2)+(a-i)]

		vx=max([s1,max(s2)])
		vn=min([s1,min(s2)])
		px=max([s1,max(s3)])
		pn=min([s1,min(s3)])

		ans=min([ans,vx-vn,px-pn])

print ans

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
if h%3 == 0 or w%3 ==0:
    print(0)
else:
    def calculate_t(x, y):
        d = h*w + 1
        i = x//2
        s1, s2 = 0, d
        rng = range(x) if x < 1000 else range(x//3, x*4//5)
        new_d = h*w
        for i in rng:
            s1 = (y//2)*i
            s2 = y*(x-i)
            s3 = (y-y//2)*i
            new_d = max(s1,s2,s3)- min(s1,s2,s3)
            print(s1, s2)
            if d > new_d:
                d = new_d
                continue
            else:
                break
        return d
    def calculate_3(x, y):
        d_div_x = y
        for i in range(y//3+1):
            h1 = i
            h2 = (y-i)//2
            h3 = y-h1-h2
            if d_div_x > abs(h3-h1):
                d_div_x = abs(h3-h1)
            else:
                break
        return d_div_x*x
    d1 = calculate_t(h, w)
    d2 = calculate_3(h, w)
    if h == w:
        print(min(d1,d2))
    else:
        d3 = calculate_t(w, h)
        d4 = calculate_3(w, h)
        print(min(d1,d2,d3,d4))

Submission status: Wrong Answer

Solution code:
def max_minus_min(areas):
    # print("areas", areas)
    return max(areas) - min(areas)
 
def solve2(h, w):
    area1 = h * (w // 2)
    area2 = h * w - area1
    return [area1, area2]
 
def solve(h, w):
    w_div_3 = w // 3;
    ans = 1e9
    for w2 in range(w_div_3, w_div_3 + 2):
        area1 = w2 * h

        tmp = solve2(h, w - w2)
        tmp = max_minus_min(tmp)
        ans = min(ans, tmp)

        tmp = solve2(w - w2, h)
        tmp = max_minus_min(tmp)
        ans = min(ans, tmp)
    return ans
 
H, W = map(int, input().split())
print(min(solve(H,W), solve(W,H)))

Submission status: Wrong Answer

Solution code:
def max_minus_min(areas):
    # print("areas", areas)
    return max(areas) - min(areas)
 
def solve2(h, w):
    area1 = h * (w // 2)
    area2 = h * w - area1
    return [area1, area2]
 
def solve(h, w):
    w_div_3 = w // 3;
    ans = 1e9
    for w2 in range(w_div_3, w_div_3 + 2):
        area1 = w2 * h

        tmp = solve2(h, w - w2)
        tmp = max_minus_min(tmp)
        ans = min(ans, tmp)

        tmp = solve2(w - w2, h)
        tmp = max_minus_min(tmp)
        ans = min(ans, tmp)

Submission status: Wrong Answer

Solution code:
def max_minus_min(areas):
    # print("areas", areas)
    return max(areas) - min(areas)
 
def solve2(h, w):
    area1 = h * (w // 2)
    area2 = h * w - area1
    return [area1, area2]
 
def solve(h, w):
    w_div_3 = w // 3;
    ans = 1e9
    for w2 in range(w_div_3, w_div_3 + 2):
        area1 = w2 * h

        tmp = solve2(h, w - w2)
        tmp = max_minus_min(tmp)