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: Runtime Error

Solution code:
h,w = map(int,input().split())
ma = max(h,w)
mi = min(h,w)
h = mi
w = ma
ans = []
if h%3==0 or w%3==0:
    print(0)
else:
    for i in range(w//3,w//2+1):
        men1 = h * i
        ma = max(h,w-i)
        mi = min(h,w-i)
        h1 = mi
        w1 = ma
        if h1%2==0 or w1%2==0:
            men2 = h1 * w1 // 2
            men3 = men2
            #print(men1,men2,men3)
            ans.append(max(men1,men2,men3)-min(men1,men2,men3))
        else:
            #print(i,w1//2,w1//2+1)
            for j in range(w1//2,w1//2+1):
                men2 = h1 * j
                men3 = h1 * (w1 - j)
                #print(men1,men2,men3)
                ans.append(max(men1,men2,men3)-min(men1,men2,men3))
print(min(ans))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
if h%2==0:
    x=w
    y=h
    ans1=10**12
    for i in range(0,x+1):
        a=i*y
        b=(x-i)*(y//2)
        c=(x-i)*(y//2)
        l=[a,b,c]
        temp=max(l)-min(l)
        ans1=min(temp,ans1)
    if w%2==0:
        x=h
        y=w
        ans2=10**12
        for i in range(0,x+1):
            a=i*y
            b=(x-i)*(y//2)
            c=(x-i)*(y//2)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(min(ans1,ans2))
        
    
    else:
        x=h
        y=w
        ans2=10**12
        for i in range(0,x+1):
            a=i*w
            b=(x-i)*(y//2)
            c=(x-i)*(y//2+1)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(min(ans1,ans2))



    
else:
    x=w
    y=h
    ans1=10**12
    for i in range(0,x+1):
        a=i*w
        b=(x-i)*(y//2)
        c=(x-i)*(y//2+1)
        l=[a,b,c]
        temp=max(l)-min(l)
        ans1=min(temp,ans1)
    if w%2==1:
        x=h
        y=w
        ans2=10**12
        for i in range(0,x+1):
            a=i*w
            b=(x-i)*(y//2)
            c=(x-i)*(y//2+1)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(min(ans1,ans2))
    else:
        ans2=10**12
        for i in range(0,h+1):
            a=i*w
            b=(h-i)*(w//2)
            c=(h-i)*(w//2)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(ans1,ans2)
h,w=map(int,input().split())
if h%3==0 or w%3==0:
    print(0)
    exit()
if h%2==0:
    x=w
    y=h
    ans1=10**12
    for i in range(0,x+1):
        a=i*y
        b=(x-i)*(y//2)
        c=(x-i)*(y//2)
        l=[a,b,c]
        temp=max(l)-min(l)
        ans1=min(temp,ans1)
    if w%2==0:
        x=h
        y=w
        ans2=10**12
        for i in range(0,x+1):
            a=i*y
            b=(x-i)*(y//2)
            c=(x-i)*(y//2)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(min(ans1,ans2))
        
    
    else:
        x=h
        y=w
        ans2=10**12
        for i in range(0,x+1):
            a=i*w
            b=(x-i)*(y//2)
            c=(x-i)*(y//2+1)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(min(ans1,ans2))



    
else:
    x=w
    y=h
    ans1=10**12
    for i in range(0,x+1):
        a=i*w
        b=(x-i)*(y//2)
        c=(x-i)*(y//2+1)
        l=[a,b,c]
        temp=max(l)-min(l)
        ans1=min(temp,ans1)
    if w%2==1:
        x=h
        y=w
        ans2=10**12
        for i in range(0,x+1):
            a=i*w
            b=(x-i)*(y//2)
            c=(x-i)*(y//2+1)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(min(ans1,ans2))
    else:
        ans2=10**12
        for i in range(0,h+1):
            a=i*w
            b=(h-i)*(w//2)
            c=(h-i)*(w//2)
            l=[a,b,c]
            temp=max(l)-min(l)
            ans2=min(temp,ans2)

        print(ans1,ans2)

Submission status: Runtime Error

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

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

def solve2(h, w):
    if h % 2 == 0 or w % 2 == 0:
        return [h*w // 2]
    elif h < w:
        return [h * (w//2), h * (w//2 + 1)]

def solve1(h, w):
    ans = []
    ans.append(max([(h//3) * w] + solve2(h - h//3, w)) - min([(h//3) * w] + solve2(h - h//3, w)))
    ans.append(max([(h//3 + 1) * w] + solve2(h - h//3 -1, w)) - min([(h//3 + 1) * w] + solve2(h - h//3 -1, w)))
    return min(ans)

print(min([solve1(h, w), solve1(w, h)]))

Submission status: Runtime Error

Solution code:
H,W = [int(i) for i in input().split()]

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    s = []
    for i in range(1,int(H/2)):
        j = int(W/2)
        a = [i*W,j*(H-i),(W-j)*(H-i)]
        b = max(a) - min(a)
        s.append(b)
        j = int(H/2)
        a = [H*i,(W-i)*j,(W-i)*(H-j)]
        b = max(a) - min(a)
        s.append(b)

    print(min(s))

Submission status: Runtime Error

Solution code:
def answer(H,W,ans):
    for x in range(1,W):
        y1 = H // 2
        Sa1 = H * x
        Sb1 = (W - x) * y1
        Sc1 = (W - x) * (H - y1)
        M = max(Sa1,Sb1,Sc1)
        m = min(Sa1,Sb1,Sc1)

        y2 = (W - x) // 2
        Sa2 = H * x
        Sb2 = H * y2
        Sc2 = H * (W - x - y2)
        M2 = max(Sa2,Sb2,Sc2)
        m2 = min(Sa2,Sb2,Sc2)

        if ans > min(M-m,M2-m2):
            ans = min(M-m,M2-m2)

        return ans

if __name__ == '__main__':
    H,W = [int(i) for i in input().split()]
    ans = H * W
    ans = answer(H,W)
    
    H,W = W,H

    if answer(H,W) < ans:
        ans = answer(H,W)

    print(ans)

Submission status: Runtime Error

Solution code:
H,W = [int(i) for i in input().split()]
ans = 0

for x in range(1,W):
    y = H // 2
    Sa1 = H * x
    Sb1 = (W - x) * y
    Sc1 = (W - x) * (H - y)
    M = max(Sa1,Sb1,Sc1)
    m = min(Sa1,Sb1,Sc1)

    Sa2 = H * x
    Sb2 = H * y
    Sb2 = H * (W - x - y)
    M2 = max(Sa2,Sb2,Sc2)
    m2 = min(Sa2,Sb2,Sc2)

    if ans > min(M-m,M2-m2):
        ans = min(M-m,M2-m2)

print(ans)

Submission status: Runtime Error

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 = h
    if h % 2 == 0:
        h1 = h // 2
        for w1 in range(w//2+1):
            s2 = h1 * w1
            s3 = h1 * (w-w1)
            ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    else:
        for w1 in range(w//3, w//2 + 1):
            s1 = w1 * h
            w2 = w-w1
            for h1 in range(h//2 + 1):
                s2 = w2 * h1
                s3 = w2 * (h-h1)
                ans = min(ans, max(s1, s2, s3) - min(s1, s2, s3))
    print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define FOR(i,start,end) for(int i=start;i<=end;i++)
const int INF = 1001001001;
typedef long long ll;
const ll MOD=1000000007;
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T>auto MAX(const T& a) { return *max_element(a.begin(),a.end()); }
template<class T>auto MIN(const T& a) { return *min_element(a.begin(),a.end()); }
template<class T, class U>U SUM(const T& a, const U& v) { return accumulate(a.begin(),a.end(), v); }
template<class T, class U>U COUNT(const T& a, const U& v) { return count(a.begin(),a.end(), v); }
template<class T, class U>int LOWER(const T& a, const U& v) { return lower_bound(a.begin(),a.end(), v) - a.begin(); }
template<class T, class U>int UPPER(const T& a, const U& v) { return upper_bound(a.begin(),a.end(), v) - a.begin(); }
int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }
int LCM(int a, int b) { int g = GCD(a, b); return a / g * b; }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
template<typename T, int FAC_MAX> struct Comb { vector<T> fac, ifac;
    Comb(){fac.resize(FAC_MAX,1);ifac.resize(FAC_MAX,1);FOR(i,1,FAC_MAX-1)fac[i]=fac[i-1]*i;
        ifac[FAC_MAX-1]=T(1)/fac[FAC_MAX-1];for(int i=FAC_MAX-2;i>=1;i--)ifac[i]=ifac[i+1]*T(i+1);}
    T aPb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b]; }
    T aCb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b] * ifac[b]; }
    T nHk(int n, int k) { if (n == 0 && k == 0) return T(1); if (n <= 0 || k < 0) return 0;
        return aCb(n + k - 1, k); } // nHk = (n+k-1)Ck : n is separator
    T pairCombination(int n) {if(n%2==1)return T(0);return fac[n]*ifac[n/2]/(T(2)^(n/2));}
    // combination of paris for n
}; 
typedef ModInt<1000000007> mint;
ll H,W ;
ll tw(ll h,ll w) {
    if ((h*w)%2 == 0) {
        return h*w /2;
    } else {
        ll a = h/2*w;
        ll b = h*w - a;
        ll c = w/2*h;
        ll d = h*w - c;
        if (abs(a-b) <= abs(c-d)){
            return a;
        } else {
            return c;
        }
    }
}

int main(void){
    // Your code here!
    cin >> H >> W;
    ll ans = 10000000000000;
    FOR(i,1,H-1) {
        ll s0 = i*W;
        ll s1 = tw(H-i,W);
        ll s2 = H*W - s1 - s0;
        ll s_max = max(s0,max(s1,s2));
        ll s_min = min(s0,min(s1,s2));
        ans = min(ans,s_max - s_min);
    }
    FOR(j,1,W-1) {
        ll s0 = j*H;
        ll s1 = tw(H,W-j);
        ll s2 = H*W - s1 - s0;
        ll s_max = max(s0,max(s1,s2));
        ll s_min = min(s0,min(s1,s2));
        ans = min(ans,s_max - s_min);
    }
    cout << ans << endl;
}

Submission status: Runtime Error

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

if H%3 == 0 or W%3 == 0:
    print(0)
    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-tate-mini)-min(yoko,mini,H*W-tate-mini))

print(ans)

Submission status: Runtime Error

Solution code:
import sys
readline = sys.stdin.readline
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

def round(f, r=0):
    return Decimal(str(f)).quantize(Decimal(str(r)), rounding=ROUND_HALF_UP)

def main():
    H, W = map(int, readline().rstrip().split())
    # 横にスライスする場合
    if H >= 3:
        ans = abs(W*(H//3) - W*(H-H//3*2))
    if W >= 3:
        ans = min(ans, abs(H*(W//3) - H*(W-W//3*2)))
    
    # 縦にスライス + 残りに横にスライス
    y1 = H
    y2 = H//2
    y3 = H - H//2
    x3 = int(round((H*W) / (H+y3)))
    x1 = W - x3
    x2 = x3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    # 横にスライス + 残りに縦にスライス
    x1 = W
    x2 = W//2
    x3 = W - W//2
    y3 = int(round((H*W) / (W+x3)))
    y1 = H - y3
    y2 = y3
    ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
h,w = map(int, input().split())
if max(h,w) >= 3:
  if h >= w :
    ans = w*min(1,(h%3))
  else:
    ans = h*min(1,(w%3))
if h%3 == 0:
  ans = 0
if w%3 == 0:
  ans = 0
if h != 1:  
  for i in range(1,h):
    ans_ = max(i*w,(h-i)*(w+(w%2))//2) - min(i*w,(h-i)*(w-(w%2))//2)
    ans = min(ans,ans_)
if w != 1:  
  for i in range(1,w):
    ans_ = max(i*h,(w-i)*(h+(h%2))//2) - min(i*h,(w-i)*(h-(h%2))//2)
    ans = min(ans,ans_)
print(ans)

Submission status: Runtime Error

Solution code:
h,w = map(int, input().split())
if max(h,w) >= 3:
  if h >= w :
    ans = w*min(1,(h%3))
  else:
    ans = h*min(1,(w%3))
for i in range(1,h):
  ans_ = max(i*w,(h-i)*(w+(w%2))//2) - min(i*w,(h-i)*(w-(w%2))//2)
  ans = min(ans,ans_)
for i in range(1,w):
  ans_ = max(i*h,(w-i)*(h+(h%2))//2) - min(i*h,(w-i)*(h-(h%2))//2)
  ans = min(ans,ans_)
print(ans)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
a = H%2
b = W%2
ans = 10**9
if H%3==0 or W%3==0:
    print(0)
elif H==1 or W==1:
    print(1)
else:
    for i in range(1,W):
        c = (H*i,H//2*(W-i),H*W-H*i-(H//2)*(W-i))
        ans = min(ans,max(c)-min(c))
    for i in range(1,H):
        c = (W*i,W//2*(H-i),H*W-W*i-(W//2)*(H-i))
        ans = min(ans,max(c)-min(c))
    d = (H*(W//3),(H*W-H*(W//3))//2,H*W-H*(W//3)-(H*W-H*(W//3))//2)
    e = (W*(H//3),(H*W-W*(H//3))//2),H*W-W*(H//3),(H*W-W*(H//3))//2)
    ans = min(ans,max(d)-min(d))
    ans = min(ans,max(e)-min(e))
    print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[]
import sys
def s(a,b):
    if a%3==1:
        s=a*b
        x=(a//3)*b
        y=((a-(a//3))//2)*b
        z=s-x-y

        e=(a-(a//3))*(b//2)
        d=s-x-e
        ma=max(x,y,z)
        mi=min(x,y,z)
        mc=max(x,e,d)
        mn=min(x,e,d)
        return(min((ma-mi),(mc-mn)))
    elif a%3==2:
        s=a*b
        x=((a//3)+1)*b
        y=((a-(a//3)-1)//2)*b
        z=s-x-y
        e=(a-(a//3)-1)*(b//2)
        d=s-x-e
        ma=max(x,y,z)
        mi=min(x,y,z)
        mc=max(x,e,d)
        mn=min(x,e,d)
        return(min((ma-mi),(mc-mn)))
j=min(h,w)
if (h%3==0)or(w%3==0):
    print(0)
elif j==1:
    print(1)
    sys.exit()
    
else:
    k=s(h,w)
    ans.append(k)
    r=s(w,h)
    ans.append(r)
    
# print(ans)
print(min(ans))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[]
import sys
def s(a,b):
    if a%3==1:
        s=a*b
        x=(a//3)*b
        y=((a-(a//3))//2)*b
        z=s-x-y

        e=(a-(a//3))*(b//2)
        d=s-x-e
        ma=max(x,y,z)
        mi=min(x,y,z)
        mc=max(x,e,d)
        mn=min(x,e,d)
        return(min((ma-mi),(mc-mn)))
    elif a%3==2:
        s=a*b
        x=((a//3)+1)*b
        y=((a-(a//3)-1)//2)*b
        z=s-x-y
        e=(a-(a//3)-1)*(b//2)
        d=s-x-e
        ma=max(x,y,z)
        mi=min(x,y,z)
        mc=max(x,e,d)
        mn=min(x,e,d)
        return(min((ma-mi),(mc-mn)))

if h<w:
    h,w=w,h
if (h%3==0)or(w%3==0):
    print(0)
else:
    k=s(h,w)
    ans.append(k)
    r=s(w,h)
    ans.append(r)
    
# print(ans)
print(min(ans))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
long = max(h, w)
short = min(h, w)
if h%3==0 or w%3==0:
    print(0)
else:
    if long%3==1:
        s_max = long//3*short
        s_min = (long-long//3)*(short//2)
    elif long%3==2:
        s_max = (long//3+1)*short
        s_min = (long-(long//3+1))*(short//2)
print(abs(s_max-s_min))

Submission status: Runtime Error

Solution code:
def chco(h, w):
    ans = float("inf")
    a, b, c = 0, 0, 0
    for i in range(1, H):
        a = i*W
        for j in range(2):
            if j == 0:
                b = (H-i)//2 * W
                c = H*W - a - b 
            else:
                b = (H-i)*(W//2)
                c = H*W - a - b
        ans = min(ans, min(max(a, b, c) - min (a, b, c)))
    return ans

print(min(chco(H, W), choco(W, H)))

Submission status: Runtime Error

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

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

else:
    M = Max(H, W)
    m = min(H, W)
    print(m*(M//2)-(M-M//2)*(m//2))

Submission status: Runtime Error

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

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

else:
    M = Max(H, W)
    m = min(H, W)
    print(m*(M//2)-(M-M//2)*(m//2)

Submission status: Runtime Error

Solution code:
def calc(A, B):
  if A % 3 == 1:
    AA = A // 3
  if A % 3 == 2:
    AA = A // 3 + 1
  BB = B // 2
  h_list = [AA * B, (A - AA) * BB, (A - AA) * (B - BB)]
  return max(h_list) - min(h_list)
 
H, W = map(int, input().split(' '))
L = list()
if H % 3 == 0 or W % 3 == 0:
  L = [0]
else:
  L = [int(calc(W, H),) int(calc(H, W))]
    
print(min(L))

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
x=[]
for i in range(1,w):
    x1=[h*i,h*((w-i)//2),h*(w-i)-h*((w-i)//2)]
    x2=[h*i,(w-i)*(h//2),h*(w-i)-(w-i)*(h//2)]
    x.append(min(max(x1)-min(x1),max(x2)-min(x2))
for i in range(1,h):
    x1=[w*i,w*((h-i)//2),w*(h-i)-w*((h-i)//2)]
    x2=[w*i,(h-i)*(w//2),w*(h-i)-(h-i)*(w//2)]
    x.append(min(max(x1)-min(x1),max(x2)-min(x2))
print(min(x))

Submission status: Runtime Error

Solution code:
import math

h, w = map(int, input().split())
diff_2 = min(h, w)

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

elif h % 2 == 0 and w %2 == 0:
    if h >= w:
        side1 = int(w/2)
        diff = math.inf
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    else:
        side1 = int(h / 2)
        diff = math.inf
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w - i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
    print(min(diff, diff_2))


elif h % 2 == 0 or w % 2 == 0:
    if h % 2 == 0:
        side1 = int(h/2)
        diff = math.inf
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = h * (w-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
                # print(i, diff)
        print(min(diff, diff_2))
    else:
        side1 = int(w / 2)
        diff = math.inf
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = w * (h-i)
            if diff > abs(rect1 - rect2):
                diff = abs(rect1 - rect2)
        print(min(diff, diff_2))
else:
    if h >= w:
        side1 = math.ceil(h/2)
        side2 = math.floor(h/2)
        diff = math.inf
        for i in range(1, w):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (w-i) * h
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))
    else:
        side1 = math.ceil(w/2)
        side2 = math.floor(w/2)
        diff = math.inf
        for i in range(1, h):
            rect1 = side1 * i
            rect2 = side2 * i
            rect3 = (h-i) * w
            t = (max(rect1, rect2, rect3) - min(rect1, rect2, rect3))
            if diff > t:
                diff = t
        print(min(diff, diff_2))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0 :
    print(0)
elif h % 3 == 1 and w % 3 == 1 :
    print(min(h, w))
elif h % 3 == 1 and W % 3 == 2 :
    print(min((h - 1) // 3, (w - 2) // 3))
elif h % 3 == 2 and w % 3 == 1 :
    print(min((h - 2) // 3, (w - 1) // 3))
elif h % 3 == 2 and w % 3 == 2 :
    print(min((h + 1) // 3, (w + 1) // 3))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

h, w = [int(x) for x in input().split()]

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

if h % 3 == 1:
    d = h // 3 
else:
    d = h // 3 + 1
block = [w * d, (h-d) * (w // 2), (h-d) * (w - w // 2)]
for b in block:
    if b == int(b**0.5) ** 2:
        break
else:
    res1 = max(block) - min(block)

if w % 3 == 1:
    d = w // 3 
else:
    d = w // 3 + 1
block = [h * d, (w-d) * (h // 2), (w-d) * (h - h // 2)]
for b in block:
    if b == int(b**0.5) ** 2:
        break
else:
    res2 = max(block) - min(block)

print(min(res1, res2))

Submission status: Runtime Error

Solution code:

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

hal = [H//3, H//3+1]

ans = float("inf")

for ha in hal:
    Sa = ha * W
    
    ## 横に分割
    hb = (H - ha)//2
    hc = H - ha - hb
    Sb = hb * W
    Sc = hc * W
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = W//2
    wc = W - wb
    Sb = (H - ha) * wb
    Sc = (H - ha) * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

wal = [W//3, W//3+1]

for wa in wal:
    Sa = H * wa
    
    ## 横に分割
    hb = H//2
    hc = H - hb
    Sb = hb * (W - wa)
    Sc = hc * (W - wb)
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))
    
    ## 縦に分割
    wb = (W - wa)//2
    wc = W - wa - wb
    Sb = H * wb
    Sc = H * wc
    ans = min(ans, max(Sa, Sb, Sc) - min(Sa, Sb, Sc))

print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
if h%3==0:
    print(0)
if w%3==0:
    print(0)
else:
    ansh=h*w
    for j in range(1,h):
        area1=w*j
        number=w//2
        area2=(h-j)*number
        area3=(h-j)*(w-number)
        check=max(abs(area1-area2),abs(area1-area3),abs(area2-area3))
        if check<ansh:
            ansh=check
        else:
            continue  
    answ=w*h
    for j in range(1,w):
        area1=h*j
        number=h//2
        area2=(w-j)*number
        area3=(w-j)*(h-number)
        check=max(abs(area1-area2),abs(area1-area3),abs(area2-area3))
        if check<answ:
            answ=check
        else:
            continue
print(min(ansh,answ))

Submission status: Runtime Error

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

ans = float('inf')
for _ in range(2):
    for i in range(1, H):
        a = i*W

        b = ((H - i) // 2) * W
        c = ((H - i + 1)// 2) * W
        ans = min(ans, abs(max(a, b, c) - min(a, b, c)))

        b = (W // 2) * (H -i)
        c = ((W + 1) // 2) * (H - i)
        ans = min(ans, abs(max(a, b, c) - min(a, b, c)))
    H, W = W, H
return ans

Submission status: Runtime Error

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    min_ = 1e10
    a = 0
    ans = 1e20
    for i in range(W+1):
      if abs(i-target) < min_:
        a = i
        min_ = abs(i-target)
      b = W - a
      target = H/2
      for i in range(min(int(target)-1, 2), min(int(target)+2, H+1)):
        if abs(i-target) < min_:
          c = i
          min_ = abs(i-target)
      d = H - c
      S = [a*H, b*c, b*d]
    ans = min(ans, max(S)-min(S))
  return ans

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

Submission status: Runtime Error

Solution code:
H, W = [int(x) for x in input().split()]

def solve(H, W):
  if H % 3 == 0 or W % 3 == 0:
    return 0  
  else:
    target = W/3
    min_ = 1e10
    a = 0
    for i in range(min(int(target)-1, 2), min(int(target)+2, W+1)):
      if abs(i-target) < min_:
        a = i
        min_ = abs(i-target)
    b = W - a
    target = H/2
    min_ = 1e20
    for i in range(min(int(target)-1, 2), min(int(target)+2, H+1)):
      if abs(i-target) < min_:
        c = i
        min_ = abs(i-target)
    d = H - c
    S = [a*H, b*c, b*d]
    return max(S)-min(S)

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

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
if H%3 == 0 or H%3 == 0:
    ans = 0
else:
    ansl = [H] + [W] + [H*W] * 2
    if W%2 == 0:
        for i in range(1, H):
            tmp = abs(i * W - (H-i) * W // 2)
            if ansl[2] > tmp:
                ansl[2] = tmp
    else:
        for i in range(1, H):
            S1 = W * i
            S2 = (H-i) * (W//2)
            S3 = H*W - S1 - S2
            tmp = max(S1, S2, S3) - min(S1, S2, S3)
            if ansl[2] > tmp:
                ansl[2] = tmp
    if H == W:
        ansl[3] = ansl[2]
    elif H%2 == 0:
        for i in range(1, W):
            tmp = abs(i * H - (W-i) * H // 2)
            if ansl[3] > tmp:
                ansl[3] = tmp
    else:
        for i in range(1, W):
            S1 = H * i
            S2 = (W-i) * (H//2)
            S3 = H*W - S1 - S2
            tmp = max(S1, S2, S3) - min(S1, S2, S3)
            if ansl[3] > tmp:
                ansl[3] = tmp
	ans = min(ansl)
print(ans)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
if H%3 == 0 or H%3 == 0:
    ans = 0
else:
    ansl = [H] + [W] + [H*W] * 2
    if W%2 == 0:
        for i in range(1, H):
            tmp = abs(i * W - (H-i) * W // 2)
            if ansl[2] > tmp:
                ansl[2] = tmp
    else:
        for i in range(1, H):
            S1 = W * i
            S2 = (H-i) * (W//2)
            S3 = H*W - S1 - S2
            tmp = max(S1, S2, S3) - min(S1, S2, S3)
            if ansl[2] > tmp:
                ansl[2] = tmp
    if H == W:
        ansl[3] = ansl[2]
    elif H%2 == 0:
        for i in range(1, W):
            tmp = abs(i * H - (W-i) * H // 2)
            if ansl[3] > tmp:
                ansl[3] = tmp
    else:
        for i in range(1, W):
            S1 = H * i
            S2 = (W-i) * (H//2)
            S3 = H*W - S1 - S2
            tmp = max(S1, S2, S3) - min(S1, S2, S3)
            if ansl[3] > tmp:
                ansl[3] = tmp
ans = min(ansl)
print(ans)

Submission status: Runtime Error

Solution code:
import math

H, W = map(int,input().split())
ans = math.inf
for i in range(1, H):
    a = i*W
    if H-i == 1:
        d = 1*(W//2)
        e = H*W-a-d
        small = min(a,d,e)
        large = max(a,d,e)
        ans = min(ans, large-small)
    else:
        b = ((H-i)//2) * W
        c = H*W-a-b
        d = (H-i) * (W//2)
        e = H*W-a-d
        small1 = min(a,b,c)
        large1 = max(a,b,c)
        small2 = min(a,d,e)
        large2 = max(a,d,e)
        ans = min(ans, large1-small1, large2-small2)
for j in range(1, W):
    a = j*H
    if W-j == 1:
        d = 1*(H//2)
        e = H*W-a-d
        small = min(a,d,e)
        large = max(a,d,e)
        ans = min(ans, large-small)
    else:
        b = ((W-j)//2) * H
        c = H*W-a-b
        d = (W-j) * (H//2)
        e = H*W-a-d
        small1 = min(a,b,c)
        large1 = max(a,b,c)
        small2 = min(a,d,e)
        large2 = max(a,d,e)
        ans = min(ans, large1-small2, large2-small2)
print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
candidate=[0]*4
if h*w%3==0:
	print(0)
	exit()
def solve(a,b,k):
	candidate[k]=a
	if b%2==0:
		cnd=float('inf')
		for i in range(a//2+1):
			cnd=min(abs(b-3*i),cnd)
		candidate[k+1]=(cnd*a//2,i)
	else:
		cnd=float('inf')
		for i in range(a//2+1):
			cnd=min(cnd,max(abs(a//2*(b-i)-a*i),abs(a//2*(b-i)-a*i+b-i),abs(b-i)))
			candidate[k+1]=(cnd,i)
solve(h,w,0)
solve(w,h,2)
print(min(candidate))

Submission status: Runtime Error

Solution code:
def max(iterable, *, default, key)
max(iterable, *[, default=obj, key=func]) -> value max(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument.

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=h*w
if h>2:
    t=[h//3*w,(h-(h//3))//2*w,(h-(h//3)+1)//2*w]
    t.sort()
ans=min(ans,t[2]-t[0])
if w>2:
    t=[w//3*h,(w-(w//3))//2*h,(w-(w//3)+1)//2*h]
    t.sort()
ans=min(ans,t[2]-t[0])
a,b=h,w
#for i in range(max(1,a//3-1),min(a//2+1,a-1)):
#    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
#    t.sort()
#    ans=min(ans,t[2]-t[0])
a,b=w,h
#for i in range(max(1,a//3-1),min(a//2+1,a-1)):
#    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
#    t.sort()
#    ans=min(ans,t[2]-t[0])
print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=h*w
if h>2:
    t=[h//3*w,(h-(h//3))//2*w,(h-(h//3)+1)//2*w]
    t.sort()
ans=min(ans,t[2]-t[0])
if w>2:
    t=[w//3*h,(w-(w//3))//2*h,(w-(w//3)+1)//2*h]
    t.sort()
ans=min(ans,t[2]-t[0])
a,b=h,w
for i in range(max(1,a//3-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
a,b=w,h
for i in range(max(1,a//3-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=h*w
if h>2:
    t=[h//3*w,(h-(h//3))//2*w,(h-(h//3)+1)//2*w]
    t.sort()
ans=min(ans,t[2]-t[0])
if w>2:
    t=[w//3*h,(w-(w//3))//2*h,(w-(w//3)+1)//2*h]
    t.sort()
ans=min(ans,t[2]-t[0])
a,b=h,w
for i in range(max(1,3//a-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
a,b=w,h
for i in range(max(1,3//a-1),min(a//2+1,a-1)):
    t=[i*b,(a-i)*(b//2),(a-i)*((b+1)//2)]
    t.sort()
    ans=min(ans,t[2]-t[0])
print(ans)

Submission status: Runtime Error

Solution code:
def threeArea(w, h, ans):
  for i in range(1, h):
    recA = w * i
    width = w//2
    recB = (h-i) * width
    recC = w * h - recA - recB
    diff = max(recA, recB, recC) - min(recA, recB, recC)
 
	height = (h - i) //2
    recB = w * height
    recC = w * h - recA - recB
    diff = min(diff, max(recA, recB, recC) - min(recA, recB, recC))
    if ans > diff:
      ans = diff
  return ans

H, W = (int(i) for i in input().split())
 
ans = float('inf')
ans = threeArea(W, H, ans)
ans = threeArea(H, W, ans)
 
print(ans)