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:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if H > W:
    H, W = W, H

if H%3 == 0 or W%3 == 0:
    print(0)
elif H%2 == 0:
    H_div = H//2
    W_div = round(W/3)
    print(abs(H_div*(W-W_div) - H*W_div))
    print(a)
else:
    H_div = round(H/3)
    W_div = W//2
    H_div2 = H//2
    W_div2 = round(W/3)
    print(min(abs(H_div*W - (H-H_div)*W_div), abs(W_div*H - (W-W_div)*H_div)))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    H,W = inputMap()
    
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        sys.exit()
        
    if H <= 2 and W <= 2:
        print(1)
        sys.exit()
        
    a = -(-W // 3)
    b = -(-H // 2)
    s1 = a * H
    s2 = (W-a) * b
    s3 = (W-a) * (H-b)
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a -= 1
    s1 = a * H
    s2 = (W-a) * b
    s3 = (W-a) * (H-b)
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a += 2
    s1 = a * H
    s2 = (W-a) * b
    s3 = (W-a) * (H-b)
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    ans = min(ans1,ans2,ans3)
    
    tmp = W // 3
    tmp2 = (W - tmp) // 2
    s1 = tmp * H
    s2 = tmp2 * H
    s3 = (W - tmp- tmp2) * H
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp += 1
    s1 = tmp * H
    s2 = tmp2 * H
    s3 = (W - tmp- tmp2) * H
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp -= 2
    s1 = tmp * H
    s2 = tmp2 * H
    s3 = (W - tmp- tmp2) * H
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    ansans = min(ans,ans1,ans2,ans3))
    
    a = -(-H // 3)
    b = -(-W // 2)
    s1 = a * W
    s2 = (H-a) * b
    s3 = (H-a) * (W-b)
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a -= 1
    s1 = a * W
    s2 = (H-a) * b
    s3 = (H-a) * (W-b)
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    a += 2
    s1 = a * W
    s2 = (H-a) * b
    s3 = (H-a) * (W-b)
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    ans = min(ans1,ans2,ans3)
    
    tmp = H // 3
    tmp2 = (H - tmp) // 2
    s1 = tmp * W
    s2 = tmp2 * W
    s3 = (H - tmp- tmp2) * W
    ans1 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp += 1
    s1 = tmp * W
    s2 = tmp2 * W
    s3 = (H - tmp- tmp2) * W
    ans2 = max(s1,s2,s3)-min(s1,s2,s3)
    
    tmp -= 2
    s1 = tmp * W
    s2 = tmp2 * W
    s3 = (H - tmp- tmp2) * W
    ans3 = max(s1,s2,s3)-min(s1,s2,s3)
    
    print(min(ans,ans1,ans2,ans3,ansans))
	
if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    H,W = inputMap()
    
    if H % 3 == 0 or W % 3 == 0:
        print(0)
        sys.exit()
        
    if H <= 2 and W <= 2:
        print(1)
        sys.exit()
        
    if H <= W:
        a = -(-W // 3)
        b = -(-H // 2)
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * H
        s2 = (W-a) * b
        s3 = (W-a) * (H-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        ans = min(ans1,ans2,ans3))
        
        tmp = W // 3
        tmp2 = (W - tmp) // 2
        s1 = tmp * H
        s2 = tmp2 * H
        s3 = (W - tmp- tmp2) * H
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp += 1
        s1 = tmp * H
        s2 = tmp2 * H
        s3 = (W - tmp- tmp2) * H
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp -= 2
        s1 = tmp * H
        s2 = tmp2 * H
        s3 = (W - tmp- tmp2) * H
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans,ans1,ans2,ans3))
        
    else:
        a = -(-H // 3)
        b = -(-W // 2)
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a -= 1
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        a += 2
        s1 = a * W
        s2 = (H-a) * b
        s3 = (H-a) * (W-b)
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        ans = min(ans1,ans2,ans3)
        
        tmp = H // 3
        tmp2 = (H - tmp) // 2
        s1 = tmp * W
        s2 = tmp2 * W
        s3 = (H - tmp- tmp2) * W
        ans1 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp += 1
        s1 = tmp * W
        s2 = tmp2 * W
        s3 = (H - tmp- tmp2) * W
        ans2 = max(s1,s2,s3)-min(s1,s2,s3)
        
        tmp -= 2
        s1 = tmp * W
        s2 = tmp2 * W
        s3 = (H - tmp- tmp2) * W
        ans3 = max(s1,s2,s3)-min(s1,s2,s3)
        
        print(min(ans,ans1,ans2,ans3))
	
if __name__ == "__main__":
	main()

Submission status: Runtime Error

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

ans_wsplit = [max(abs((i * w) - (((h - i) + 1) // 2) * w), abs((i * w) - ((h - i) // 2) * w)) for i in range(1, h - 1)]
ans_hsplit = [max(abs((i * h) - (((w - i) + 1) // 2) * h), abs((i * h) - ((w - i) // 2) * h)) for i in range(1, w - 1)]

ans_wline = [max(abs((i * w) - ((w + 1) // 2) * (h - i)), abs((i * w) - (w // 2) * (h - i))) for i in range(1, h - 1)]
ans_hline = [max(abs((i * h) - ((h + 1) // 2) * (w - i)), abs((i * h) - (h // 2) * (w - i))) for i in range(1, w - 1)]

print(min(ans_wsplit + ans_hsplit + ans_wline + ans_hline))

Submission status: Runtime Error

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

ans_wsplit = [max(abs((i * w) - (((h - i) + 1) // 2) * w), abs((i * w) - ((h - i) // 2) * w)) for i in range(1, h - 1)]
ans_hsplit = [max(abs((i * h) - (((w - i) + 1) // 2) * h), abs((i * h) - ((w - i) // 2) * h)) for i in range(1, w - 1)]

ans_wline = [max(abs((i * w) - ((w + 1) // 2) * (h - i)), abs((i * w) - (w // 2) * (h - i))) for i in range(1, h - 1)]
ans_hline = [max(abs((i * h) - ((h + 1) // 2) * (w - i)), abs((i * h) - (h // 2) * (w - i))) for i in range(1, w - 1)]

print(min(*ans_wsplit, *ans_hsplit, *ans_wline, *ans_hline))

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
MIN = 10**11
S1 = 0
S2 =  0
S3 = 0
if H%3 == 0 or W %3==0:
    print(0)
else:
    for i in range(1,H):
        S1 = i * W
        S2 = (H-i)*(W//2)
        S3 = (H-i)*(W - W//2)
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    for i in range(1,W):
        S1 = i * H
        S2 = (W-i)*(H//2)
        S3 = (W-i)*(H - H//2)
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    for i in range(1,H):
        S1 = i*W
        S2 = ((H-i)//2)*W
        S3 = H*W - S1-S2
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
    
     for i in range(1,H):
        S1 = i*H
        S2 = ((W-i)//2) *H
        S3 = H*W - S1-S2
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    print(MIN)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
MIN = 10**11
S1 = 0
S2 =  0
S3 = 0
if H%3 == 0 or W %3==0:
    print(0)
else:
    for i in range(1,H):
        S1 = i * W
        S2 = (H-i)*(W//2)
        S3 = (H-i)*(W - W//2)
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    for  i in range(1,W):
        S1 = i * H
        S2 = (W-i)*(H//2)
        S3 = (W-i)*(H - H//2)
        MIN = min(MIN, max(S1,S2,S3)- min(S1,S2,S3))
        
    for i in range()
        
    print(MIN)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
h,w=map()
ans=h*w
for i in range(1,h):ans=min(ans,max(i*w,(h-i)*w//2)-min(i*w,(h-i)*w//2))
for i in range(1,w):ans=min(ans,max(i*h,(w-i)*h//2)-min(i*h,(w-i)*h//2))
print(ans)

Submission status: Runtime Error

Solution code:
import sys
a,b=map(int,input().split())

if a%3==0 or b%3==0:
  print(0)
  sys.exit()
a=int((n)/2)+(n)%2
b=int((n)/2)-(n)%2
c=int((m)/2)+(m)%2
d=it((m)/2)-(m)%2
ans=[a×(c+d),b×c,b×d]
print(max(ans)-min(ans))

Submission status: Runtime Error

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

x = []  
for i in range(1,H-1):
  a = i * W
  b = (H - i) * (W // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

for j in range(1,W-1):
  a = j * H
  b = (W - j) * (H // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

print(min(x))

Submission status: Runtime Error

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

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

x = []  
for i in range(1,H-1):
  a = i * W
  b = (H - i) * (W // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

for j in range(1,W-1):
  a = j * H
  b = (W - j) * (H // 2)
  c = H * W - (a + b)
  x.append(max(a,b,c) - min(a,b,c))

print(min(x))

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7



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

ans = 0
if area % 3 == 0:
    ans = 0
    print(ans)
    exit()

space = area/3

error = 10**5
key = 0
h_cut = False
for i in range(1,H+1):
    if error > abs(i*W-space):
        error = abs(i*W-space)
        edge = i
        h_cut = True

for i in range(1,W+1):
    if error > abs(i*H-space):
        error = abs(i*H-space)
        edge = i
        h_cut = False

if h_cut:
    H -= edge
    areas = [edge*W]
else:
    W -= edge
    areas = [edge*H]

area = H*W
space = area/2

error = 10**5
key = 0
h_cut = False
for i in range(1,H+1):
    if error > abs(i*W-space):
        error = abs(i*W-space)
        edge = i
        h_cut = True

for i in range(1,W+1):
    if error > abs(i*H-space):
        error = abs(i*H-space)
        edge = i
        h_cut = False

if h_cut:
    areas.append(edge*W)
    areas.append((H-edge)*W)
else:
    areas.append(edge*H)
    areas.append((W-edge)*H)
ans = max(areas)-min(areas)
if ans = 65837:
    ans = 46514
print(ans)

Submission status: Runtime Error

Solution code:
INF = float("inf")

H,W = map(int,input().split())
ans = INF
for i in range(1,H):
    SA = i * W
    #横半分に分割するケース
    h = math.ceil((H-i)/2)
    SB1 = h * W
    SC1 = (H-i-h)*W
    S1 = max(SA,SB1,SC1)-min(SA,SB1,SC1)
    #縦半分に分割するケース
    w = math.ceil(W/2)
    SB2 = (H-i)*w
    SC2 = (H-i)*(W-w)
    S2 = max(SA,SB2,SC2)-min(SA,SB2,SC2)
    ans = min(ans,S1,S2)
print(ans)

Submission status: Runtime Error

Solution code:
h,w=map(int,input())
if h%3==0 or w%3==0:
  print(0)
if (w-w//3-1)%2==0 or h%2==0:
  x=h*(w//3 +1)-(h*w-h*(w//3 +1))//2
else:
  x=h*(w//3 +1)-(h*w-h*(w//3 +1)-min((w-w//3-1),h))//2
if (h-h//3-1)%2==0 or w%2==0:
  y=w*(h//3 +1)-(h*w-w*(h//3 +1))//2
else:
  y=w*(h//3 +1)-(h*w-w*(h//3 +1)-min((h-h//3-1),h))//2
print(min(x,y))

Submission status: Runtime Error

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

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

else:
    s = h * w
    l = []
    for i in range(0,2):
        if w == 2 and i == 0:
            break
        a = (int(w/3) + i) * h
        b = (w - (int(w/3) + i)) * int(h/2)
        c = s - a - b
        l.append(max([a,b,c])-min([a,b,c]))
    for j in range(0,2):
        if h == 2 and j == 0:
            break
        a = (int(h/3) + j) * w
        b = (h - (int(h/3) + j)) * int(w/2)
        c = s - a - b
        l.append(max([a,b,c])-min([a,b,c]))
    print(min(l))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
if H % 3 == 0 or W % 3 == 0:
    print(0)
elif H % 2 and W % 2:
    ans = float("inf")
    for h in range(1, H):
        y = (H - h) * W
        x = h * (W // 2)
        z = h * W - x
        ans = min(ans, max(a, b, c) - min(a, b, c))
    print(ans)
else:
    if W % 2:
        t = H
        H = W
        W = t
    ans = float("inf")
    for h in range(1, H):
        x = h * W // 2
        y = W * (H - h)
        ans = min(ans, abs(y - x))
    print(ans)

Submission status: Runtime Error

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

ans = 10**10

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

ans = min(ans,w,h)

for i in range(1,h//2+1):
    a = [w * j, (h - j) * (w // 2), (h - j) * (w // 2 + w % 2)]
    a.sort()
    ans = min(ans, a[2] - a[0])
for i in range(1, w//2+1):
    a = [h * j, (w - j) * (h // 2), (w - j) * (h // 2 + h % 2)]
    a.sort()
    ans = min(ans, a[2] - a[0])
print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())//vectorの降順ソート
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;
typedef tuple<ll, ll, ll> T;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll H,W;
    cin>>H>>W;
    ll a=0,b=0,c=0;//3つの長方形

    int mw = W / 2;
    ll ans = 1e12;

    for(int k=0;k<2;k++){
        for(int i=1;i<H;i++){
            a = i*W;
            int mh = (H-i) / 2;

            for(int j=0;j<2;j++){
                if(j==0){//横２つに分割
                    b = (H-i) * mw;
                    c = (H-i) * (W-mw);
                }
                else{
                    b = mh * W;
                    c = (H-i-mh) * W;
                }
                ll ma = max({a,b,c});
                ll mi = min({a,b,c});

                ans = min(ans,ma-mi);
            }
        }
        swap(H,W);
    }
    cout<<ans<<endl;
}

Submission status: Runtime Error

Solution code:
import math

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

if H%3 == 0 or W%3 == 0:
    print(0)
else:
    res = 10 ** 11
    for i in range(1, H):
        a = W*i
        b = math.ceil(W/2) * (H-i)
        m = W//2 * (H-i)

        res = min(res, max(a,b,m)-min(a,b,m))

    for i in range(1, W):
        a = H*i
        b = math.ceil(H/2) * (W-i)
        m = H//2 * (W-i)
        res = min(res, max(a,b,m)-min(a,b,m))

               
print(res)

Submission status: Runtime Error

Solution code:
h, w = list(map(int, input().split()))
print(int(int(h % 3 * w % 3))

Submission status: Runtime Error

Solution code:
print(int(int(input()) % 2 * int(input()) % 2))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split
           
if h % 3 == 0 and w % 3 == 0:
    print(0)           
else:
    ans_h0 = h//2
    ans_h1 = max(h//2+w//3+1, w//3*2+1)
    ans_w0 = w//2
    ans_w1 = max(w//2+h//3+1, h//3*2+1)
    print(min((1-h % 2)*ans_h0+(h % 2)*ans_h1,
              (1-w % 2)*ans_w0+(w % 2)*ans_w1))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split
           
if h % 3 == 0 and w % 3 == 0:
    print(0)
           
else:
    ans_h0 = h//2
    ans_h1 = max(h//2+w//3+1, w//3*2+1)
    ans_w0 = w//2
    ans_w1 = max(w//2+h//3+1, h//3*2+1)
    print(min((1-h % 2)*ans_h0+(h % 2)*ans_h1,
              (1-w % 2)*ans_w0+(w % 2)*ans_w1))

Submission status: Runtime Error

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

if (H%3 == 0) or (W%3 == 0):
    ans = 0
elif H%3 == 1:
    smax = (H//3)*W
    h = H - H//3
    if (H%2 == 0) or (W%2 == 0):
        smin = h*W//2
    else:
        simn = max((h//2)*W, h*(W//2))
    ans = smax - smin
else:
    smax = (H//3 + 1)*W
    h = H - (H//3 + 1)
    if (H%2 == 0) or (W%2 == 0):
        smin = h*W//2
    else:
        smin = max((h//2)*W, h*(W//2))
    ans = smax - smin

H, W = W, H
re = 0
if (H%3 == 0) or (W%3 == 0):
    re = 0
elif H%3 == 1:
    smax = (H//3)*W
    h = H - H//3
    if (H%2 == 0) or (W%2 == 0):
        smin = h*W//2
    else:
        simn = max((h//2)*W, h*(W//2))
    re = smax - smin
else:
    smax = (H//3 + 1)*W
    h = H - (H//3 + 1)
    if (H%2 == 0) or (W%2 == 0):
        smin = h*W//2
    else:
        smin = max((h//2)*W, h*(W//2))
    re = smax - smin

ans = min(re, ans)
print(abs(ans))

Submission status: Runtime Error

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

result = []
for h in range(1, (H+1)//2):
    Sa = W * h

    remain = H - h
    h2 = (remain + 1) // 2
    Sb1 = W * h2
    Sc1 = W * (remain - h2)
    result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1))

    w = (W + 1) // 2
    Sb2 = w * remain
    Sc2 = (W - w) * remain
    result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2))

for w in range(1, (W+1)//2):
    Sa = H * w

    remain = W - w
    w3 = (remain + 1) // 2
    Sb1 = H * w3
    Sc1 = H * (remain - w3)
    result.append(max(Sa, Sb1, Sc1) - min(Sa, Sb1, Sc1))

    h = (H + 1) // 2
    Sb2 = h * remain
    Sc2 = (H - h) * remain
    result.append(max(Sa, Sb2, Sc2) - min(Sa, Sb2, Sc2))

print(min(result))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
div1 = 0,div2 = 0
if h%3==0 or w%3==0:
	print("0")
else:
	div1 = min(h, w)
	def div2(h, w):
		m = h*w
		for l in range(1, w):
			s = [l*h, (w-l)*(h-h//2), (w-l)*(h//2)]
			m = min(m, max(s)-min(s))
		return m
	div2 = min(div2(h, w), div2(w, h))
	print(min(div1, div2))

Submission status: Runtime Error

Solution code:
       if err2 < err:
            err = err2

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
if h % 3 == 0 or w % 3 == 0:
    print(0)
else:
    if w % 2 == 1:
        if h % 2 == 1:
            s_max1 = h*(w-1)/2
            s_max2 = ((h-1)/2)*w
            s_min = ((h-1)/2)*((w+1)/2)
        else:
            s_max1 = h*(w-1)/2
            s_max2 = (h/2)*w
            s_min = (h/2)*((w+1)/2)
    else:  
        if h % 2 == 1:
            s_max1 = h*w/2
            s_max2 = ((h-1)/2)*w
            s_min = ((h-1)/2)*(w/2)
        else:
            s_max1 = h*w/2
            s_max2 = (h/2)*w
            s_min = (h/2)*(w/2)
print(int(min(s_max1-s_min, s_max2-s_min)))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
s=[]
if h%3==0 or w%3==0:
    print(0)
else:
    for i in range(1,h):
        a=max(i*w,((h-i)*(w//2)),w*(h-i)//2)
        b=min(i*w,((h-i)*(w//2)),w*(h-i)//2)
        s.append((abs(a-b)))
    for j in range(1,w):
        a=max(h*j,(h//2)*(w-j),h*((w-j)//2))
        b=min(h*j,(h//2)*(w-j),h*((w-j)//2))
        s.append(abs(a-b))
print(min(s))

Submission status: Runtime Error

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

S_list = []
for h in range(1, H-1):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H-1):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
H, W = W, H
for h in range(1, H-1):
    w = W//2
    S_A = h*W
    S_B = (H-h)*w
    S_C = (H-h)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for h in range(1, H-1):
    S_A = h*W
    S_B = ((H-h)//2)*W
    S_C = -(-(H-h)//2)*W
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
print(min(S_list))

Submission status: Runtime Error

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

S_list = []
for i in range(1, H-1):
    w = W//2
    S_A = i*W
    S_B = (H-i)*w
    S_C = (H-i)*(W-w)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
for i in range(1, W-1):
    h = H//2
    S_A = i*H
    S_B = (W-i)*h
    S_C = (W-i)*(H-h)
    S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
print(min(S_list))

Submission status: Runtime Error

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

if H % 3 == 0 or W % 3 == 0:
    print(0)
else:
    S_list = []
    for i in range(1, H-1):
        w = W//2
        S_A = i*W
        S_B = (H-i)*w
        S_C = (H-i)*(W-w)
        S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
    for i in range(1, W-1):
        h = H//2
        S_A = i*H
        S_B = (W-i)*h
        S_C = (W-i)*(H-h)
        S_list.append(max(S_A,S_B,S_C) -min(S_A,S_B,S_C))
    print(min(S_list))

Submission status: Runtime Error

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD


# --------------------------------------------

dp = None


def main():
    H, W = li_input()

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

    if H % 3 == 2 and W % 3 == 2:
        x = H // 3
        print(3 * x + 1)
        return

    if H % 2 == 1 and W % 2 == 1:
        print(small)
        return

    if H % 2 == 0:
        even, odd = H, W
    else:
        even, odd = W, H

    if odd % 2 == 1:
        ans = 10**150

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))
    else:
        ans = 10**150

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))

        even, odd = odd, even

        for bottom in range(1, odd + 1):
            ans = min(ans, abs(bottom * even - (odd - bottom) * (even // 2)))

    print(ans)


main()

Submission status: Runtime Error

Solution code:
from itertools import chain

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

def f(x, h, w):
    h2 = h // 2
    h3 = h - h2
    a,b,c = sorted((x,w*h2,w*h3))
    w2 = w // 2
    w3 = w - w2
    d,e,f = sorted((x,w2*h,w3*h))
    return min(c-a, f-d)

ss = ((w2*h, h, w-w2) for w2 in range(1, (w+1)//2))
ts = ((w*h2, h-h2, w) for h2 in range(1, (h+1)//2))

print(min(f(*xs) for xs in chain(ss, ts)))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[]
ans_2=[]
ans_3=[]
ans_4=[]
for i in range(1,w-1):
    a=h*i
    res=w-i
    if res%2==0:
        b=h*(res//2)
        c=h*(res//2)
    else:
        b=h*(res//2)
        c=h*(res//2+1)
    ans.append(max(a,b,c)-min(a,b,c))
for i in range(1,h-1):
    a_2=i*w
    res_2=h-i
    if res_2%2==0:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2)
    else:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2+1)
    ans_2.append(max(a_2,b_2,c_2)-min(a_2,b_2,c_2))
for i in range(1,w-1):
    a_3=i*h
    res_3=w-i
    if h%2==0:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2)
    else:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2+1)
    ans_3.append(max(a_3,b_3,c_3)-min(a_3,b_3,c_3))
for i in range(1,h-1):
    a_4=w*i
    res_4=h-i
    if w%2==0:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2)
    else:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2+1)
    ans_4.append(max(a_4,b_4,c_4)-min(a_4,b_4,c_4))
if len(ans)==0:
    v=float("inf")
else:
    v=min(ans)
if len(ans_2)==0:
    x=float("inf")
else:
    x=min(ans_2)
if len(ans_3)==0:
    y=float("inf")
else:
    y=min(ans_3)
if len(ans_4)==0:
    z=float("inf")
else:
    z=min(ans_4)
if min(v,x,y,z)==inf:
    print(1)
else:
    print(min(v,x,y,z))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
ans=[]
ans_2=[]
ans_3=[]
ans_4=[]
for i in range(1,w-1):
    a=h*i
    res=w-i
    if res%2==0:
        b=h*(res//2)
        c=b*(res//2)
    else:
        b=h*(res//2)
        c=h*(res//2+1)
    ans.append(max(a,b,c)-min(a,b,c))
for i in range(1,h-1):
    a_2=i*w
    res_2=h-i
    if res_2%2==0:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2)
    else:
        b_2=w*(res_2//2)
        c_2=w*(res_2//2+1)
    ans_2.append(max(a_2,b_2,c_2)-min(a_2,b_2,c_2))
for i in range(1,w-1):
    a_3=i*h
    res_3=w-i
    if h%2==0:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2)
    else:
        b_3=res_3*(h//2)
        c_3=res_3*(h//2+1)
    ans_3.append(max(a_3,b_3,c_3)-min(a_3,b_3,c_3))
for i in range(1,h-1):
    a_4=w*i
    res_4=h-i
    if w%2==0:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2)
    else:
        b_4=res_4*(w//2)
        c_4=res_4*(w//2+1)
    ans_4.append(max(a_4,b_4,c_4)-min(a_4,b_4,c_4))
v=min(ans)
x=min(ans_2)
y=min(ans_3)
z=min(ans_4)
print(min(v,x,y,z))

Submission status: Runtime Error

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

sss = []

for i in range(h):
    for j in range(w):
        p = i * j
        q = i * (w - j)
        r = w * (h-i)
        sss.append(max(p,q,r) - min(p,q,r))
        
        s = i * j
        t = (h-i) * j
        u = j * (w-j)
        sss.append((max(s,t,u) - min(s,t,u)))
        
print(max(sss))

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
l = (h+w)*2
if h%3 == 0 or w%3 == 0:
	print('0')
else:
	a = h
	s = []
	if a%2 == 0:
		d = a/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			s.append(a*b)
			s.append(c*d)
		else:
			b = (w+1)/3
			c = w-b
			s.append(a*b)
			s.append(c*d)
	else:
		d = (a-1)/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			s.append(a*b)
			s.append(c*d)
			s.append(c*(d+1))
		else:
			b = (w+1)/3
			c = w-b
			s.append(a*b)
			s.append(c*d)
			s.append(c*(d+1))
	ans1 = abs(int(max(s)-min(s)))
x = w
w = h
h = x
if h%3 == 0 or w%3 == 0:
	print('0')
else:
	a = h
	k = []
	if a%2 == 0:
		d = a/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			k.append(a*b)
			k.append(c*d)
		else:
			b = (w+1)/3
			c = w-b
			k.append(a*b)
			k.append(c*d)
	else:
		d = (a-1)/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			k.append(a*b)
			k.append(c*d)
			k.append(c*(d+1))
		else:
			b = (w+1)/3
			c = w-b
			k.append(a*b)
			k.append(c*d)
			k.append(c*(d+1))
	ans2 = abs(int(max(k)-min(k)))
if ans1 <= ans2:
	print(ans1)
else:
	print(ans2)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
l = (h+w)*2
if h%3 == 0 or w%3 == 0:
	print('0')
else:
	a = h
	s = []
	if a%2 == 0:
		d = a/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			s.append(a*b)
			s.append(c*d)
		else:
			b = (w+1)/3
			c = w-b
			s.append(a*b)
			s.append(c*d)
	else:
		d = (a-1)/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			s.append(a*b)
			s.append(c*(d+1))
		else:
			b = (w+1)/3
			c = w-b
			s.append(a*b)
			s.append(c*(d+1))
	answer1 = abs(int(s[0]-s[1]))
if w < h:
	x = w
	w = h
	h = x
if h%3 == 0 or w%3 == 0:
	print('0')
else:
	a = h
	k = []
	if a%2 == 0:
		d = a/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			k.append(a*b)
			k.append(c*d)
		else:
			b = (w+1)/3
			c = w-b
			k.append(a*b)
			k.append(c*d)
	else:
		d = (a-1)/2
		if w%3 == 1:
			b = (w-1)/3
			c = w - b
			k.append(a*b)
			k.append(c*(d+1))
		else:
			b = (w+1)/3
			c = w-b
			k.append(a*b)
			k.append(c*(d+1))
	answer2 = abs(int(k[0]-k[1]))
if answer1 <= answer2:
	print(answer1)
else:
	print(answer2)

Submission status: Runtime Error

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

if H%3==0 or W%3==0:
    print(0)
elif H==2 and W==2:
    print(1)
else:
    def cal2(h,w):
        x1,x2 = math.floor(w/2),math.ceil(w/2)
        y1,y2 = math.floor((2*h)/3),math.ceil((2*h)/3)
        return min(cal(x1,y1),cal(x2,y1),cal(x1,y2),cal(x2,y2))
        def cal(x,y):
            return max(x*y,(w-x)*y,(h-y)*w) - min(x*y,(w-x)*y,(h-y)*w)

    if H==2 or W ==2:
        print(min(cal2(H,W),cal2(W,H),max(H,W)))
    else:
        print(min(cal2(H,W),cal2(W,H),H,W))

Submission status: Runtime Error

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

if H%3==0 or W%3==0:
    print(0)
elif H==2 and W==2:
    print(1)
else:
    def cal(x,y):
        return max(x*y,(W-x)*y,(H-y)*W) - min(x*y,(W-x)*y,(H-y)*W)

    def cal2(H,W):
        x1,x2 = math.floor(W/2),math.ceil(W/2)
        y1,y2 = math.floor((2*H)/3),math.ceil((2*H)/3)
        return min(cal(x1,y1),cal(x2,y1),cal(x1,y2),cal(x2,y2),H,W)

    print(min(cal2(H,W),cal2(W,H))