Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
times = [int(input()) for i in range(5)]
cost = 0
p_times = [time % 10 for time in times]
if sum(p_times) == 0 and min(p_times) > 0:
    last_order = False
else:
    last_order = min([i for i in p_times if i > 0])
    last_order_idx = p_times.index(last_order)

if last_order:
    cost = times.pop(last_order_idx)

for time in times:
    if time % 10 == 0:
        cost += time
    else:
        cost += time + (10 - time % 10)

print(cost)


Fixed submission:
times = [int(input()) for i in range(5)]
cost = 0
p_times = [time % 10 for time in times]

if (sum([i % 10 for i in p_times]) == 0) & (min(times) > 0):
    last_order = False
else:
    last_order = min([i for i in p_times if i > 0])
    last_order_idx = p_times.index(last_order)

if last_order:
    cost = times.pop(last_order_idx)

for time in times:
    if time % 10 == 0:
        cost += time
    else:
        cost += time + (10 - time % 10)

print(cost)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

def main():
    X, Y, Z, K = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    B = list(map(int, readline().rstrip().split()))
    C = list(map(int, readline().rstrip().split()))
    
    ab = [A[i] + B[j] for i in range(len(A)) for j in range(len(B))]
    ab = sorted(ab, reverse=True)[:K]

    abc = [ab[i] + C[j] for i in range(len(ab)) for j in range(len(C))]
    print(*sorted(abc, reverse=True)[:K], sep='\n')


if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.readline

def main():
    X, Y, Z, K = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    B = list(map(int, readline().rstrip().split()))
    C = list(map(int, readline().rstrip().split()))
    
    ab = [A[i] + B[j] for i in range(len(A)) for j in range(len(B))]
    ab = sorted(ab, reverse=True)[:K]

    abc = [ab[i] + C[j] for i in range(len(ab)) for j in range(len(C))]
    print(*sorted(abc, reverse=True)[:K], sep='\n')


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline
from itertools import combinations

def main():
    X, Y, Z, K = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    B = list(map(int, readline().rstrip().split()))
    C = list(map(int, readline().rstrip().split()))
    ab = []
    for a in A:
        for b in B:
            ab.append(a + b)
    ab = sorted(ab, reverse=True)[:K]
    abc = []
    for v in ab:
        for c in C:
            abc.append(v + c)
    
    print(*sorted(abc, reverse=True)[:K], sep='\n')


if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.readline

def main():
    X, Y, Z, K = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    B = list(map(int, readline().rstrip().split()))
    C = list(map(int, readline().rstrip().split()))
    
    ab = [A[i] + B[j] for i in range(len(A)) for j in range(len(B))]
    ab = sorted(ab, reverse=True)[:K]

    abc = [ab[i] + C[j] for i in range(len(ab)) for j in range(len(C))]
    print(*sorted(abc, reverse=True)[:K], sep='\n')


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()
ans = []
for _ in range(Q):
    l, r = map(int, input().split())
    ans.append(S[l-1:r].count('AC'))

print('\n'.join(list(map(str, ans))))

Fixed submission:
N, Q = map(int, input().split())
S = input()
cnt = [0] * N
for i in range(1, N):
    if S[i-1:i+1] == 'AC':
        cnt[i] = cnt[i-1] + 1
    else:
        cnt[i] = cnt[i-1]

ans = []
for _ in range(Q):
    l, r = map(int, input().split())
    ans.append(cnt[r-1] - cnt[l-1])

print('\n'.join(list(map(str, ans))))

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()

for _ in range(Q):
    l, r = map(int, input().split())
    temp_S = S[l-1:r]
    print(temp_S.count('AC'))

Fixed submission:
N, Q = map(int, input().split())
S = input()
cnt = [0] * N
for i in range(1, N):
    if S[i-1:i+1] == 'AC':
        cnt[i] = cnt[i-1] + 1
    else:
        cnt[i] = cnt[i-1]

ans = []
for _ in range(Q):
    l, r = map(int, input().split())
    ans.append(cnt[r-1] - cnt[l-1])

print('\n'.join(list(map(str, ans))))
Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())

ans = [1]
for i in range(2, A):
    if A % i == 0 and B % i == 0:
        ans.append(i)

print(ans[-K])

Fixed submission:
import sys
A, B, K = map(int, input().split())
cnt = 0
if min(A, B) == 1:
    print(1)
    sys.exit()
    
for i in range(min(A, B), 0, -1):
    if A % i == 0 and B % i == 0:
        cnt += 1
        if cnt == K:
            print(i)
            break
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n_dices, n_points = map(int, input().split())
points = sorted(list(map(int, input().split())))
if n_dices >= n_points:
    print(0)
    sys.exit()
interval_list = [points[i + 1] - points[i] for i in range(n_points - 1)]
for i in range(n_dices - 1):
    interval_list.pop(interval_list.index(max(interval_list)))
print(sum(interval_list))

Fixed submission:
import sys
input = sys.stdin.readline
n_dices, n_points = map(int, input().split())
points = sorted(list(map(int, input().split())))
if n_dices >= n_points:
    print(0)
    sys.exit()
interval_list = [points[i + 1] - points[i] for i in range(n_points - 1)]
interval_list.sort(reverse = True)
print(sum(interval_list[n_dices - 1:]))
Buggy python submission (verdict: Time Limit Exceeded):
INF = 10 ** 9

def main():
    N, K = map(int, input().split())
    H = list(map(int, input().split()))

    dp = [INF] * N
    dp[0] = 0
    for i in range(1, N):
        ks = max(0, i-K)
        hi = H[i]
        for j in range(ks, i):
            dp[i] = min(dp[i], dp[j] + abs(hi - H[j]))
    
    print(dp[N-1])
    

if __name__ == "__main__":
    main()

Fixed submission:
INF = 10 ** 9

def main():
    N, K = map(int, input().split())
    H = list(map(int, input().split()))

    dp = [INF] * N
    dp[0] = 0
    for i in range(1, N):
        ks = max(0, i-K)
        hi = H[i]
        for j in range(ks, i):
            dp[i] = min(dp[i], dp[j] + abs(hi - H[j]))
    
    print(dp[N-1])


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, W = map(int, input().split())
    dp = [[0] * (W+1) for _ in range(N+1)]

    for i in range(1, N+1):
        w, v = map(int, input().split())
        dp_pre = dp[i-1]
        for j in range(w):
            dp[i][j] = dp_pre[j]
        for j in range(w, W+1):
            dp[i][j] = max(dp_pre[j-w] + v, dp_pre[j])

    print(max(dp[N]))


if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, W = map(int, input().split())
    dp = [[0] * (W+1) for _ in range(N+1)]

    for i in range(1, N+1):
        w, v = map(int, input().split())
        dp_pre = dp[i-1]
        for j in range(w):
            dp[i][j] = dp_pre[j]
        for j in range(w, W+1):
            dp[i][j] = max(dp_pre[j-w] + v, dp_pre[j])

    print(max(dp[N]))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()
    T = input()
    N = len(S)
    M = len(T)
    dp = [[0] * (M+1) for _ in range(N+1)]
    
    for i in range(1, N+1):
        for j in range(1, M+1):
            p = (S[i-1] == T[j-1])
            dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1] + p)
            
    ans = []
    x, y = N, M
    while x and y:
        if dp[x][y] == dp[x-1][y]:
            x -= 1
        elif dp[x][y] == dp[x][y-1]:
            y -= 1
        else:
            x -= 1
            y -= 1
            ans.append(S[x])
        
    print(''.join(ans[::-1]))


if __name__ == "__main__":
    main()

Fixed submission:
def main():
    S = input()
    T = input()
    N = len(S)
    M = len(T)
    dp = [[0] * (M+1) for _ in range(N+1)]
    
    for i in range(1, N+1):
        for j in range(1, M+1):
            p = (S[i-1] == T[j-1])
            dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1] + p)
            
    ans = []
    x, y = N, M
    while x and y:
        if dp[x][y] == dp[x-1][y]:
            x -= 1
        elif dp[x][y] == dp[x][y-1]:
            y -= 1
        else:
            x -= 1
            y -= 1
            ans.append(S[x])
        
    print(''.join(ans[::-1]))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()
    T = input()
    N = len(S)
    M = len(T)
    
    dp = [[0] * (M+1) for _ in range(N+1)]
    ans = [[''] * (M+1) for _ in range(N+1)]
    
    for i in range(1, N+1):
        p = S[i-1]
        for j in range(1, M+1):
            if T[j-1] == p:
                dp[i][j] = dp[i-1][j] + 1
                ans[i][j] = ans[i-1][j] + p
            else:
                if dp[i][j-1] < dp[i-1][j]:
                    dp[i][j] = dp[i-1][j]
                    ans[i][j] = ans[i-1][j]
                else:
                    dp[i][j] = dp[i][j-1]
                    ans[i][j] = ans[i][j-1]
                
    print(ans[N][M])


if __name__ == "__main__":
    main()

Fixed submission:
def main():
    S = input()
    T = input()
    N = len(S)
    M = len(T)
    dp = [[0] * (M+1) for _ in range(N+1)]
    
    for i in range(1, N+1):
        for j in range(1, M+1):
            p = (S[i-1] == T[j-1])
            dp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1] + p)
            
    ans = []
    x, y = N, M
    while x and y:
        if dp[x][y] == dp[x-1][y]:
            x -= 1
        elif dp[x][y] == dp[x][y-1]:
            y -= 1
        else:
            x -= 1
            y -= 1
            ans.append(S[x])
        
    print(''.join(ans[::-1]))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
heights = sorted([int(input() for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Fixed submission:
N, K = map(int, input().split())
heights = sorted([int(input()) for _ in range(N)])
ans = max(heights)
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 6)

def main():
    S = input()
    T = input()
    for i in range(len(S)):
        if S[i] != T[i]:
            S = S.translate(str.maketrans({S[i]: T[i], T[i]: S[i]}))
    
    if S == T:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)

def main():
    S = input()
    T = input()
    pair = {}
    pair2 = {}
    for i in range(len(S)):
        if S[i] not in pair.keys():
            pair[S[i]] = T[i]
        else:
            if pair[S[i]] != T[i]:
                print('No')
                return
        if T[i] not in pair2.keys():
            pair2[T[i]] = S[i]
        else:
            if pair2[T[i]] != S[i]:
                print('No')
                return
        
    
    print('Yes')


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
for i, s in enumerate(S[:]):
    if s == 'W':
        S[i] = 0
    else:
        S[i] = 1

ans = N
for i in range(N):
    left = S[:i]
    right = S[i + 1:]
    ans = min(ans, len(left) - sum(left) + sum(right) )
print(ans)

Fixed submission:
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count
for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    ans = min(ans, e_count + w_count)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = N
for i in range(N):
    left = S[:i]
    right = S[i + 1:]
    if left.count('W') + right.count('E') < ans:
        ans = left.count('W') + right.count('E')
print(ans)

Fixed submission:
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count
for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    ans = min(ans, e_count + w_count)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

def main():
    S = readline().rstrip()
    K = int(readline())
    N = len(S)
    d = {}
    for i in range(N):
        s = ''
        head = S[i]
        d.setdefault(head, set())
        for j in range(i, N):
            s += S[j]
            d[head].add(s)

    for key, val in sorted(d.items()):
        if K <= len(val):
            print(sorted(list(val))[K-1])
            return
        else:
            K -= len(val)


if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.readline

def main():
    S = readline().rstrip()
    K = int(readline())
    N = len(S)
    d = {}
    for i in range(N):
        s = ''
        head = S[i]
        d.setdefault(head, set())
        for j in range(i, min(N, i+5)):
            s += S[j]
            d[head].add(s)

    for key, val in sorted(d.items()):
        if K <= len(val):
            print(sorted(list(val))[K-1])
            return
        else:
            K -= len(val)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline
import networkx as nx
from networkx.algorithms import bipartite

def main():
    N = int(readline())
    R = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]
    B = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]

    g = nx.Graph()  # 空の無向グラフを作成
    g.add_nodes_from([i for i in range(N)], bipartite=0)
    g.add_nodes_from([N + i for i in range(N)], bipartite=1)
    edge = []
    for i, (a, b) in enumerate(R):
        for j, (c, d) in enumerate(B):
            if a < c and b < d:
                g.add_edge(i, N+j, weight=1)

    d = nx.max_weight_matching(g)
    for (i, j) in g.edges():
        if ((i, j) not in d) and ((j, i) not in d):
            g.remove_edge(i, j)

    print(len(g.edges()))

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.readline
from operator import itemgetter

def main():
    N = int(readline())
    R = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]
    B = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]

    B.sort(key=itemgetter(0))
    R.sort(key=itemgetter(1), reverse=True)
    ans = 0
    for c, d in B:
        for a, b in R:
            if a < c and b < d:
                ans += 1
                R.remove((a, b))
                break
    
    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())
if ((a * b % 2) == 0:
    print('Even')
else:
    print('Odd')

Fixed submission:
input_line = list(map(int, input().split()))
if ((input_line[0] * input_line[1]) % 2) == 0:
    print('Even')
else:
    print('Odd')
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())
if ((a * b % 2) == 0:
    print('Even')
else:
    print('Odd')

Fixed submission:
import sys
N = int(input())
t, x, y = 0, 0, 0
for i in range(N):
    in_t, in_x, in_y = map(int, input().split())
    t = in_t - t
    x = abs(in_x - x)
    y = abs(in_y - y)
    if (t >= x + y) & ((t - (x + y)) % 2 == 0):
        continue
    print('No')
    sys.exit()
print('Yes')
Buggy python submission (verdict: Runtime Error):
import sys
num_bill, y = map(int, input().split())

for num_10000 in range(y // 10000 + 1):
    for num_5000 in range(min((y - num_10000 * 10000) // 5000 + 1, num_bill - num_10000 + 1)):
        if ((num_bill - num_10000 - num_5000) * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
            print(num_10000, num_5000, (num_bill - num_10000 - num_5000))
            sys.exit()

print('-1 -1 -1')

Fixed submission:
import sys
num_bill, y = map(int, input().split())
bills = [1000, 5000, 10000]

num_1000, num_5000, num_10000 = 0, 0, 0

for num_10000 in range(y // 10000 + 1):
    if (num_1000 * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
        print(num_10000, num_5000, num_1000)
        sys.exit()
        
    iter_num = min(((y - num_10000 * 10000) // 5000 + 1), (num_bill - num_10000 + 1))
    for num_5000 in range(iter_num):
        if (num_1000 * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
            print(num_10000, num_5000, num_1000)
            sys.exit()
        
        num_1000 = num_bill - num_10000 - num_5000
        if (num_1000 * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
            print(num_10000, num_5000, num_1000)
            sys.exit()

print('-1 -1 -1')

Buggy python submission (verdict: Runtime Error):
import sys
num_bill, y = map(int, input().split())

for num_10000 in range(y // 10000 + 1):
    for num_5000 in range(min((y - num_10000 * 10000) // 5000 + 1, num_bill - num_10000 + 1)):
        (num_bill - num_10000 - num_5000)
        if ((num_bill - num_10000 - num_5000) * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
            print(num_10000, num_5000, (num_bill - num_10000 - num_5000))
            sys.exit()

print('-1 -1 -1')

Fixed submission:
import sys
num_bill, y = map(int, input().split())
bills = [1000, 5000, 10000]

num_1000, num_5000, num_10000 = 0, 0, 0

for num_10000 in range(y // 10000 + 1):
    if (num_1000 * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
        print(num_10000, num_5000, num_1000)
        sys.exit()
        
    iter_num = min(((y - num_10000 * 10000) // 5000 + 1), (num_bill - num_10000 + 1))
    for num_5000 in range(iter_num):
        if (num_1000 * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
            print(num_10000, num_5000, num_1000)
            sys.exit()
        
        num_1000 = num_bill - num_10000 - num_5000
        if (num_1000 * 1000 + num_5000 * 5000 + num_10000 * 10000 == y) & (num_10000 + num_5000 + num_1000 == num_bill):
            print(num_10000, num_5000, num_1000)
            sys.exit()

print('-1 -1 -1')
Buggy python submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def main():
    A, B, C, D, E, F = map(int, readline().rstrip().split())
    X = set()
    for a in range(F//(100*A)+1):
        for b in range(F//(100*B)+1):
            if 100 * a * A + 100 * b * B <= F:
                X.add(100 * a * A + 100 * b * B)
    X.remove(0)

    Y = set()
    for c in range(F//C+1):
        for d in range(F//D+1):
            if c * C + d * D <= F/2:
                Y.add((c * C + d * D))

    # ans = (0, 0)
    max_dens = 0
    for x in X:
        for y in Y:
            if x + y > F:
                continue
            if y * 100 <= x * E:
                dens = y/(x+y)
                if dens > max_dens:
                    ans = (x+y, y)
                    max_dens = dens
    print(*ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.readline

def main():
    A, B, C, D, E, F = map(int, readline().rstrip().split())
    X = set()
    for a in range(F//(100*A)+1):
        for b in range(F//(100*B)+1):
            if 100 * a * A + 100 * b * B <= F:
                X.add(100 * a * A + 100 * b * B)
    X.remove(0)

    Y = set()
    for c in range(F//C+1):
        for d in range(F//D+1):
            if c * C + d * D <= F/2:
                Y.add((c * C + d * D))

    ans = (0, 0)
    max_dens = -1
    for x in X:
        for y in Y:
            if x + y > F:
                continue
            if y * 100 <= x * E:
                dens = y/(x+y)
                if dens > max_dens:
                    ans = (x+y, y)
                    max_dens = dens
    print(*ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
from math import gcd
num_clock = int(input())
times = [int(input()) for i in range(num_clock)]
spent_time = times[0]
for time in times[1:]:
    spent_time = (spent_time * time) // gcd(spent_time, time)
print(spent_time)

Fixed submission:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#from math import gcd
num_clock = int(input())
times = [int(input()) for i in range(num_clock)]
spent_time = times[0]
for time in times[1:]:
    spent_time = (spent_time * time) // gcd(spent_time, time)
print(spent_time)
Buggy python submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline

def main():
    N = int(readline())
    S = readline().rstrip()
    res = S[:]

    if '(' in S:
        idx = S.index('(')
        cnt = 1
        for s in S[idx+1:]:
            if s == '(':
                cnt += 1
            else:
                cnt -= 1
        res += ')' * max(cnt, 0)
    
    S = S[::-1]
    idx = S.index(')')
    cnt = 1
    for s in S[idx+1:]:
        if s == ')':
            cnt += 1
        else:
            cnt -= 1
    res = '(' * max(cnt, 0) + res

    print(res)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.readline

def main():
    N = int(readline())
    S = readline().rstrip()
    res = S[:]

    pls_left = 0
    pointer = 0
    for s in S:
        if s == '(':
            pointer += 1
        else:
            if pointer == 0:
                pls_left += 1
            else:
                pointer -= 1

    pls_right = pointer
    res = '(' * pls_left + S
    res += ')' * pls_right

    print(res)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
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()

Fixed submission:
import sys
readline = sys.stdin.readline
from math import floor, ceil

def main():
    H, W = map(int, readline().rstrip().split())
    ans = H * W
    if H >= 3:
        ans = min(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)))
    
    # 縦にスライス + 残りに横にスライス
    # x1_cand = [floor(W/3), ceil(W/3)]
    x1_cand = [i for i in range(1, W)]
    y1 = H
    for x1 in x1_cand:
        x2 = W - x1
        x3 = x2
        y2 = H // 2
        y3 = H - y2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

        x2 = (W-x1) // 2
        x3 = (W-x1-x2)
        ans = min(ans, max((x1*H), (x2*H), (x3*H)) - min((x1*H), (x2*H), (x3*H)))

    # 横にスライス + 残りに縦にスライス
    # y1_cand = [floor(H/3), ceil(H/3)]
    y1_cand = [i for i in range(1, H)]
    x1 = W
    for y1 in y1_cand:
        y2 = H - y1
        y3 = y2
        x2 = W // 2
        x3 = W - x2
        ans = min(ans, max((x1*y1), (x2*y2), (x3*y3)) - min((x1*y1), (x2*y2), (x3*y3)))

        y2 = (H-y1) // 2
        y3 = (H-y1-y2)
        ans = min(ans, max((W*y1), (W*y2), (W*y3)) - min((W*y1), (W*y2), (W*y3)))

    print(ans)

if __name__ == '__main__':
    main()