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 math
#import sys
#input = sys.stdin.readline

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
#
def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))

# a = list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################



#28-

n,m= list(map(int, input().split()))
ans = 10**9

for i in range(1,n):
    s1 = m*i
    s2 = (n-i)*(m//2)
    s3 = (n-i)*m - s2
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))

if(n%3==0 or m%3==0):
    ans = 0
else:
    ans = min(ans,n,m)
    
print(ans)

Submission status: Wrong Answer

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
#
def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))

# a = list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################



#28-

n,m= list(map(int, input().split()))
ans = 10**9

for i in range(1,n):
    s1 = m*i
    s2 = (n-i)*(m//2)
    s3 = (n-i)*m - s2
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))

if(n//3==0 or m//3==0):
    ans = 0
else:
    ans = min(ans,n,m)
    
print(ans)

Submission status: Wrong Answer

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
#
def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))

# a = list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################



#28-

n,m= list(map(int, input().split()))
ans = 10**9

for i in range(1,n):
    s1 = m*i
    s2 = (n-i)*(m//2)
    s3 = (n-i)*m - s2
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))

for i in range(1,n):
    s1 = m*i
    s2 = m*((n-i)//2)
    s3 = m*(n-i) - s2
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
#
n,m = m,n
for i in range(1,n):
    s1 = m*i
    s2 = m*((n-i)//2)
    s3 = m*(n-i) - s2
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))
#
print(ans)

Submission status: Wrong Answer

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
#
def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))

# a = list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################



#28-

n,m= list(map(int, input().split()))
ans = 10**9

for i in range(1,n):
    s1 = m*i
    s2 = (n-i)*(m//2)
    s3 = (n-i)*m - s2
    ans = min(ans,max(s1,s2,s3)-min(s1,s2,s3))

print(ans)

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
    print(0)
    exit()
elif H==2 and W==2:
    print(1)
    exit()
elif H==2 or W==2:
    smax1=2*(max(H,W)//3)
    smin1=(H*W-smax1)//2
    smax2=2*(max(H,W)//3+1)
    smin2=(H*W-smax2)//2
    #print(smax1, smin1, smax2, smin2)
    print(min(abs(smax1-smin1), abs(smax2-smin2)))
    exit()
ans=float("INF")
for t in [(H,W),(W,H)]:
    h,w=t
    first1=w*(h//3)
    sec1=(h-h//3)*(w//2)
    third1=(h-h//3)*(w-w//2)
    smax1=max(first1,sec1,third1)
    smin1=min(first1,sec1,third1)

    first2=w*(h//3+1)
    sec2=(h-h//3-1)*(w//2)
    third2=(h-h//3-1)*(w-w//2)
    smax2=max(first2,sec2,third2)
    smin2=min(first2,sec2,third2)
    sub=min(smax1-smin2, smax2-smin2)
    ans=min(sub, ans)
    #print(smin1, smax1, smin2, smax2)

print(ans)

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
    print(0)
    exit()
elif H==2 and W==2:
    print(1)
    exit()
elif H==2 or W==2:
    smax1=2*(max(H,W)//3)
    smin1=(H*W-smax1)//2
    smax2=2*(max(H,W)//3+1)
    smin2=(H*W-smax2)//2
    print(smax1, smin1, smax2, smin2)
    print(min(abs(smax1-smin1), abs(smax2-smin2)))
    exit()
ans=float("INF")
for t in [(H,W),(W,H)]:
    h,w=t
    first1=w*(h//3)
    sec1=(h-h//3)*(w//2)
    third1=(h-h//3)*(w-w//2)
    smax1=max(first1,sec1,third1)
    smin1=min(first1,sec1,third1)

    first2=w*(h//3+1)
    sec2=(h-h//3-1)*(w//2)
    third2=(h-h//3-1)*(w-w//2)
    smax2=max(first2,sec2,third2)
    smin2=min(first2,sec2,third2)
    sub=min(smax1-smin2, smax2-smin2)
    ans=min(sub, ans)
    #print(smin1, smax1, smin2, smax2)

print(ans)

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
    print(0)
    exit()
elif H==2 and W==2:
    print(1)
    exit()
elif H==2 or W==2:
    smax1=2*max(H,W)//3
    smin1=(H*W-smax1)//2
    smax2=2*(max(H,W)//3+1)
    smin2=(H*W-smax2)//2

    print(min(abs(smax1-smin1), abs(smax2-smin2)))
    exit()
ans=float("INF")
for t in [(H,W),(W,H)]:
    h,w=t
    first1=w*(h//3)
    sec1=(h-h//3)*(w//2)
    third1=(h-h//3)*(w-w//2)
    smax1=max(first1,sec1,third1)
    smin1=min(first1,sec1,third1)

    first2=w*(h//3+1)
    sec2=(h-h//3-1)*(w//2)
    third2=(h-h//3-1)*(w-w//2)
    smax2=max(first2,sec2,third2)
    smin2=min(first2,sec2,third2)
    sub=min(smax1-smin2, smax2-smin2)
    ans=min(sub, ans)
    #print(smin1, smax1, smin2, smax2)

print(ans)

Submission status: Wrong Answer

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

if H%3==0 or W%3==0:
    print(0)
    exit()
elif H==2 or W==2:
    smax1=2*max(max(H,W)//3, 1)
    smin1=(H*W-smax1)//2
    smax1=max(smin1+1, smax1)
    smax2=2*max(max(H,W)//3+1,1)
    smin2=(H*W-smax2)//2
    smax2=max(smin2+1, smax2)
    print(smax1, smin1, smax2, smin2)
    print(min((smax1-smin1),smax2-smin2))
    exit()
ans=float("INF")
for t in [(H,W),(W,H)]:
    h,w=t
    first1=w*(h//3)
    sec1=(h-h//3)*(w//2)
    third1=(h-h//3)*(w-w//2)
    smax1=max(first1,sec1,third1)
    smin1=min(first1,sec1,third1)

    first2=w*(h//3+1)
    sec2=(h-h//3-1)*(w//2)
    third2=(h-h//3-1)*(w-w//2)
    smax2=max(first2,sec2,third2)
    smin2=min(first2,sec2,third2)
    sub=min(smax1-smin2, smax2-smin2)
    ans=min(sub, ans)
    #print(smin1, smax1, smin2, smax2)

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:
    vals = []
    if H>=3:
        vals.append((H//3+1)*W-(H//3)*W)
    for i in range(min(3, W)):
        x = (W//3+i)*H
        vals.append(max(abs(x-(W-W//3-i)*(H//2)), abs((W-W//3-i)*(H//2)-(W-W//3-i)*(H//2+H%2))))

    H, W = W, H
    if H>=3:
        vals.append((H//3+1)*W-(H//3)*W)
    for i in range(min(3, W)):
        x = (W//3+i)*H
        vals.append(max(abs(x-(W-W//3-i)*(H//2)), abs((W-W//3-i)*(H//2)-(W-W//3-i)*(H//2+H%2))))
    print(min(vals))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split()) 
if not h % 3 or not w % 3:
    print(0)
else:
    if h == 2 or w == 2:
        print()
    ans = min(w, h)
    for i in range(5):
        k = max(1, w // 3 - 2 + i)
        lis = [(h // 2) * (w - k), ((h + 1) // 2) * (w - k), h * k]
        ans = min(ans, max(lis) - min(lis))
    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:
    vals = []
    if H>=3:
        vals.append((H//3+1)*W-(H//3)*W)
    for i in range(3):
        x = (W//3+i)*H
        vals.append(max(abs(x-(W-W//3-i)*(H//2)), abs((W-W//3-i)*(H//2)-(W-W//3-i)*(H//2+H%2))))
    H, W = W, H
    if H>=3:
        vals.append((H//3+1)*W-(H//3)*W)
    for i in range(3):
        x = (W//3+i)*H
        vals.append(max(abs(x-(W-W//3-i)*(H//2)), abs((W-W//3-i)*(H//2)-(W-W//3-i)*(H//2+H%2))))
    print(min(vals))

Submission status: Wrong Answer

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

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

ans = float("inf")  
if W < H:
  W,H = H,W
  
for i in range(W): #W>=Hの前提で考える。
  a = H*i
  if H%2 == 0:
    b = (W-i)*(H//2)
    ans = min(ans,abs(a-b))
  else:
    b = (W-i)*(H//2)
    c = (W-i)*(H//2+1)
    dif = max(abs(a-b),abs(a-c),abs(b-c))
    ans = min(ans,dif)

for i in range(H): #W>=Hの前提で考える。
  a = W*i
  if W%2 == 0:
    b = (H-i)*(W//2)
    ans = min(ans,abs(a-b))
  else:
    b = (H-i)*(W//2)
    c = (H-i)*(W//2+1)
    dif = max(abs(a-b),abs(a-c),abs(b-c))
    ans = min(ans,dif)    
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W = map(int, input().split())

    if H % 3 == 0 or W % 3 == 0:
        print(0)
    else:
        rh = H % 3
        res1 = f_inf
        for i in range(W):
            ma = max(rh * W, 3 * i, 3 * (W - i))
            mi = min(rh * W, 3 * i, 3 * (W - i))
            res1 = min(res1, ma - mi)

        rw = W % 3
        res2 = f_inf
        for j in range(H):
            ma = max(rw * H, 3 * j, 3 * (H - j))
            mi = min(rw * H, 3 * j, 3 * (H - j))
            res2 = min(res2, ma - mi)

        print(min(res1, res2))


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
res = 10**16
l = sorted(list(map(int, input().split())))
ma, mi = l[1], l[0]
if mi % 3 == 0 or ma % 3 == 0:
    print(0)
    exit()
if mi % 2 and ma % 2:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2), (ma-i)*(mi//2+1)]
        res = min(res, max(l) - min(l))
    for i in range(1, mi):
        l = [i*ma, (mi-i)*(ma//2), (mi-i)*(ma//2+1)]
        res = min(res, max(l) - min(l))
elif mi % 2 and ma % 2 == 0:
    for i in range(1, ma):
        l = [i * mi, (ma - i) * (mi // 2), (ma - i) * (mi // 2 + 1)]
        res = min(res, max(l) - min(l))
    for i in range(1, mi):
        l = [i * ma, (mi - i) * (ma // 2)]
        res = min(res, max(l) - min(l))
elif mi % 2 == 0 and ma % 2:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2)]
        res = min(res, max(l) - min(l))
    for i in range(1, mi):
        l = [i*ma, (mi-i)*(ma//2), (mi-i)*(ma//2+1)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, ma):
        l = [i * mi, (ma - i) * (mi // 2)]
        res = min(res, max(l) - min(l))
    for i in range(1, mi):
        l = [i * ma, (mi - i) * (ma // 2)]
        res = min(res, max(l) - min(l))
print(res)

Submission status: Wrong Answer

Solution code:
res = 10**16
l = sorted(list(map(int, input().split())))
ma, mi = l[1], l[0]
if mi % 3 == 0 or ma % 3 == 0:
    print(0)
    exit()
if mi % 2:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2), (ma-i)*(mi//2+1)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2)]
        res = min(res, max(l) - min(l))
if ma % 2:
    for i in range(1, mi):
        l = [i*ma, (mi-i)*(ma//2), (mi-i)*(ma//2+1)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, mi):
        l = [i*ma, (mi-i)*(ma//2)]
        res = min(res, max(l) - min(l))
print(res)

Submission status: Wrong Answer

Solution code:
res = 10**9 + 7
l = sorted(list(map(int, input().split())))
ma, mi = l[1], l[0]
if mi % 3 == 0 or ma % 3 == 0:
    print(0)
    exit()
if mi % 2:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2), (ma-i)*(mi//2+1)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2)]
        res = min(res, max(l) - min(l))
if ma % 2:
    for i in range(1, mi):
        l = [i*ma, (mi-i)*(ma//2), (mi-i)*(ma//2+1)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, mi):
        l = [i*ma, (mi-i)*(ma//2)]
        res = min(res, max(l) - min(l))
print(res)

Submission status: Wrong Answer

Solution code:
res = 10**9 + 7
l = sorted(list(map(int, input().split())))
ma, mi = l[1], l[0]
if mi % 3 == 0 or ma % 3 == 0:
    print(0)
    exit()
if mi % 2:
    for i in range(1, ma//2+1):
        l = [i*mi, (ma-i)*(mi//2), (ma-i)*(mi//2+1)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, ma//2+1):
        l = [i*mi, (ma-i)*(mi//2)]
        res = min(res, max(l) - min(l))
if ma % 2:
    for i in range(1, mi//2+1):
        l = [i*ma, (mi-i)*(ma//2), (mi-i)*(ma//2+1)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, mi//2+1):
        l = [i*ma, (mi-i)*(ma//2)]
        res = min(res, max(l) - min(l))
print(res)

Submission status: Wrong Answer

Solution code:
res = 10**9 + 7
l = sorted(list(map(int, input().split())))
ma, mi = l[1], l[0]
if mi % 3 == 0 or ma % 3 == 0:
    print(0)
    exit()
if mi % 2 == 0:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, ma):
        l = [i*mi, (ma-i)*(mi//2), (ma-i)*(mi//2+1)]
        res = min(res, max(l) - min(l))
print(res)

Submission status: Wrong Answer

Solution code:
res = 10**9 + 7
l = sorted(list(map(int, input().split())))
ma, mi = l[1], l[0]
if mi % 3 == 0 or ma % 3 == 0:
    print(0)
    exit()
if mi % 2 == 0:
    for i in range(1, ma//2+2):
        l = [i*mi, (ma-i)*(mi//2)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, ma//2+2):
        l = [i*mi, (ma-i)*(mi//2), (ma-i)*(mi//2+1)]
        res = min(res, max(l) - min(l))
print(res)

Submission status: Wrong Answer

Solution code:
res = 10**9 + 7
l = sorted(list(map(int, input().split())))
ma, mi = l[1], l[0]
if mi % 3 == 0 or ma % 3 == 0:
    print(0)
    exit()
if mi % 2 == 0:
    for i in range(1, ma//2+1):
        l = [i*mi, (ma-i)*(mi//2)]
        res = min(res, max(l) - min(l))
else:
    for i in range(1, ma//2+1):
        l = [i*mi, (ma-i)*(mi//2), (ma-i)*(mi//2+1)]
        res = min(res, max(l) - min(l))
print(res)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    a, b = map(int, input().split())
    if a % 3 == 0 or b % 3 == 0:
        print(0)
        sys.exit()
    if a > b:
        a, b = b, a

    x = (b // 3) * a
    bb = b - b//3
    aa = a
    if aa % 2 == 0 or bb % 2 == 0:
        y = (bb * aa) // 2
        li = sorted([x,y,y])
    else:
        if aa > bb:
            aa, bb = bb, aa
        y = aa * (bb//2)
        z = aa * ((bb+1)//2)
        li = sorted([x, y, z])
    ans = li[2] - li[0]

    x = (b // 3 + 1) * a
    bb = b - b//3 - 1
    aa = a
    if aa % 2 == 0 or bb % 2 == 0:
        y = (aa*bb)//2
        li = sorted([x,y,y])
    else:
        if aa > bb:
            aa, bb = bb, aa
        y = aa * (bb//2)
        y = aa * (bb//2)
        z = aa * ((bb+1)//2)
        li = sorted([x, y, z])
    ans = min(ans, li[2] - li[0])
    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:
  h2 = [H//2,H//2+1]
  h3 = [H//3,H//3+1,H//3+2]  
  w2 = [W//2,W//2+1]
  w3 = [W//3,W//3+1,W//3+2]
  ans = float("inf")
  #最初に縦切り
  for i in range(3):
    for j in range(2):
      a = H*w3[i]
      b = (W-w3[i])*h2[j]
      c = H*W - a - b 
      ans = min(ans , max(a,b,c) - min(a,b,c) )    
  #横  
  for i in range(3):
    for j in range(2):
      a = W*h3[i]
      b = (H-h3[i])*w2[j]
      c = H*W - a - b 
      ans = min(ans , max(a,b,c) - min(a,b,c) ) 
  #縦横の完全三等分パターン
  for i in range(2):
    for j in range(2):
      a = W*h3[i]
      b = W*(H-a)//2
      c = W*(H -a -b)
      ans = min(ans , max(a,b,c) - min(a,b,c) )
      a = H*w3[i]
      b = H*(W-a)//2
      c = H*(W -a -b)
      ans = min(ans , max(a,b,c) - min(a,b,c) )
  print(ans)

Submission status: Wrong Answer

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

if H%3 == 0 or W%3 == 0:
  print(0)
else:
  h2 = [H//2,H//2+1]
  h3 = [H//3,H//3+1,H//3+2]  
  w2 = [W//2,W//2+1]
  w3 = [W//3,W//3+1,W//3+2]
  ans = float("inf")
  #最初に縦切り
  for i in range(3):
    for j in range(2):
      a = H*w3[i]
      b = (W-w3[i])*h2[j]
      c = H*W - a - b 
      ans = min(ans , max(a,b,c) - min(a,b,c) )
  #横  
  for i in range(3):
    for j in range(2):
      a = W*h3[i]
      b = (H-h3[i])*w2[j]
      c = H*W - a - b 
      ans = min(ans , max(a,b,c) - min(a,b,c) ) 
  print(ans)

Submission status: Wrong Answer

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

if H%3 == 0 or W%3 == 0:
  print(0)
else:
  h2 = [H//2,H//2+1]
  h3 = [H//3,H//3+1]  
  w2 = [W//2,W//2+1]
  w3 = [W//3,W//3+1]
  ans = float("inf")
  #最初に縦切り
  for i in range(2):
    for j in range(2):
      a = H*w3[i]
      b = (W-w3[i])*h2[j]
      c = H*W - a - b 
      ans = min(ans , max(a,b,c) - min(a,b,c) )
  #横  
  for i in range(2):
    for j in range(2):
      a = W*h3[i]
      b = (H-h3[i])*w2[j]
      c = H*W - a - b 
      ans = min(ans , max(a,b,c) - min(a,b,c) ) 
  print(ans)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split(" "))
ans = 10 ** 10
karians = [0,0,0]
if H % 3 == 0 or W % 3 == 0:
    ans = 0
k = int(W / 2)
for i in range(H + 1):
    karians = [i * W,k * (H - i),(W - k) * (H - i)]
    karians.sort()
    if (karians[2] - karians[0] < ans):
        ans = karians[2] - karians[0]

k = int(H / 2)
for i in range(W + 1):
    karians = [i * H,k * (W - i),(H - k) * (W - i)]
    karians.sort()
    if (karians[2] - karians[0] < ans):
        ans = karians[2] - karians[0]
    
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


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


def main():
    H, W = map(int, input().split())
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        return
    if H % 2 == 0 or W % 2 == 0:
        if W % 2 != 0:
            print(H // 2)
            return
        if H % 2 != 0:
            print(W // 2)
            return
        else:
            if H >= W:
                print(W // 2)
                return
            else:
                print(H // 2)
                return

    if H == 1:
        print(1)
        return
    if W == 1:
        print(1)
        return
    else:
        answer = float("inf")
        for i in range(1, H):
            m = W // 2
            choco = [i * W, m * (H - i), (m + 1) * (H - i)]
            ans = max(choco) - min(choco)
            if answer > ans:
                answer = ans

        print(min(answer, min(H, W)))


if __name__ == "__main__":
    main()