Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def comb(n, r):
    ret = 1
    if (n == r) or (n == 0) or (r == 0):
        ret = 1
    else:
        for i in range(1, n + 1):
            ret *= i
        for i in range(1, r + 1):
            ret //= i
        for i in range(1, n - r + 1):
            ret //= i
    return ret

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

s = [0, a[0]]
for i in range(1, n):
    s.append(s[i]+a[i])
d = {}
for x in s:
    if x in d.keys():
        d[x] += 1
    else:
        d[x] = 1

ans = 0
for k, v in d.items():
    if v >= 2:
        ans += comb(v, 2)
print(ans)

Fixed submission:
'''
https://beta.atcoder.jp/contests/agc023/submissions/2426081
'''
n = int(input())
A = map(int, input().split())
B = {0:1}
s = 0
ans = 0
for a in A:
    s += a
    B_s = B.get(s, 0) 
    ans += B_s
    B[s] = B_s + 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

s = [a[0]]
for i in range(1, n):
    s.append(s[i-1]+a[i])

cnt = 0
for i in range(n):
    for j in range(i, n):
        buf = s[j] - s[i-1] if (i >= 1) else s[j]
        if buf == 0:
            cnt += 1 
print(cnt) 

Fixed submission:
'''
https://beta.atcoder.jp/contests/agc023/submissions/2426081
'''
n = int(input())
A = map(int, input().split())
B = {0:1}
s = 0
ans = 0
for a in A:
    s += a
    B_s = B.get(s, 0) 
    ans += B_s
    B[s] = B_s + 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
cnt = 0
for i in range(n):
    for j in range(i, n):
        if (sum(a[i:j+1]) == 0):
            cnt += 1 
print(cnt)    

Fixed submission:
'''
https://beta.atcoder.jp/contests/agc023/submissions/2426081
'''
n = int(input())
A = map(int, input().split())
B = {0:1}
s = 0
ans = 0
for a in A:
    s += a
    B_s = B.get(s, 0) 
    ans += B_s
    B[s] = B_s + 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def is_good_grid():
    for r in range(n):
        for c in range(r):
            if t[r][c] != t[c][r]:
                return False
    return True


n = int(input())
s = tuple(input() for _ in range(n))

# (0,0)==(1,1)==...==(n-1,n-1)
# colのオフセットだけ変化させる
# それぞれのループで、n個の状態(0,k)==(1,k+1)==...を扱う

ans = 0
for k in range(n):
    t = [[''] * n for _ in range(n)]
    for r in range(n):
        for c in range(n):
            t[r][c] = s[r][(c + k) % n]
    if is_good_grid():
        ans += n
print(ans)


Fixed submission:
N = int(input())
s = [input() for _ in range(N)]

ans = 0
a = 0
for b in range(N):
    if all(s[(i - a) % N][(j - b) % N] == s[(j - a) % N][(i - b) % N] for i in range(N) for j in range(i + 1, N)):
        ans += N
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = [input() for _ in range(N)]

ans = 0
a = 0
for b in range(N):
    if all(s[(i - a) % N][(j - b) % N] == s[(j - a) % N][(i - b) % N] for i in range(N) for j in range(i + 1, N)):
        ans += N
print(ans)


Fixed submission:
N = int(input())
s = [input() for _ in range(N)]

ans = 0
a = 0
for b in range(N):
    if all(s[(i - a) % N][(j - b) % N] == s[(j - a) % N][(i - b) % N] for i in range(N) for j in range(i + 1, N)):
        ans += N
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int, input().split())
for i in range(a, b + 1):
    if i <= (a + k -1):
        print(i)
    elif (b - k + 1) <= i:
        print(i)

Fixed submission:
a, b, k = map(int, input().split())
for i in range(a, min(b + 1, a + k)):
    print(i)
for i in range(max(a + k, b - k + 1), b + 1):
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int, input().split())
for i in range(a, b + 1):
    if (a <= i) and (i <= (a + k -1)):
        print(i)
    elif ((b - k + 1) <= i) and (i <= b):
        print(i)

Fixed submission:
a, b, k = map(int, input().split())
for i in range(a, min(b + 1, a + k)):
    print(i)
for i in range(max(a + k, b - k + 1), b + 1):
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int, input().split())
for i in range(a, b + 1):
    if (a <= i) and (i <= (a + k -1)):
        print(i)
    elif ((b - k + 1) <= i) and (i <= b):
        print(i)

Fixed submission:
a, b, k = map(int, input().split())
for i in range(a, min(b + 1, a + k)):
    print(i)
for i in range(max(a + k, b - k + 1), b + 1):
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int, input().split())
for i in range(a, b + 1):
    if (a <= i) and (i <= (a + k -1)):
        print(i)
    if ((a + k) <= i) and ((b - k + 1) <= i) and (i <= b):
        print(i)

Fixed submission:
a, b, k = map(int, input().split())
for i in range(a, min(b + 1, a + k)):
    print(i)
for i in range(max(a + k, b - k + 1), b + 1):
    print(i)
Buggy python submission (verdict: Runtime Error):
from random import sample
from math import gcd

n = int(input())

nl = []
for j in range(1, 30000 + 1):
    if (j % 2 == 0 or j % 3 == 0 or j % 5 == 0):
        nl.append(j)
len_nl = len(nl)

while True:
    tl = sample(nl, len_nl)
    g = 0
    total = 0
    for j in range(n):
        g = gcd(g, tl[j])
        total += tl[j]
    if total % 30 == 0:
        continue
    if g != 1:
        continue
    break
print(*tl[:n])

Fixed submission:
from random import sample
def gcd(x, y):
    if x < y:# x >= y とする
        x, y = y, x
    r = x % y
    res = y if r == 0 else gcd(y, r)
    return res

n = int(input())

nl = []
for j in range(1, 30000 + 1):
    if (j % 2 == 0 or j % 3 == 0 or j % 5 == 0):
        nl.append(j)
len_nl = len(nl)

while True:
    tl = sample(nl, len_nl)
    g = tl[0]
    total = 0
    for j in range(n):
        g = gcd(g, tl[j])
        total += tl[j]
    if total % 30 != 0:
        continue
    if g != 1:
        continue
    break
print(*tl[:n])
Buggy python submission (verdict: Time Limit Exceeded):
def dx(a,b):
    return abs(a-b)

def abs(x):
    if x>=0:
        return x
    else:
        return -x

if __name__ == '__main__':
    n = int(input())
    a = list(False for _ in range(n+2))
    a[0] = 0
    buf = list(map(int, input().split()))
    for i in range(n):
        a[i+1] = buf[i]
    a[n+1] = 0

    for i in range(1,n+1): ##訪れるのを取りやめる座標1->n
        pos = 0
        val = 0
        for j in range(n+2): ##訪れる座標0->(n+1)
            if i != j: ##訪れる座標なら
                val += dx(a[j], a[pos])
                pos = j
        print(val)

Fixed submission:
n = int(input())
a = [0] + list(map(int, input().split())) + [0]
total = 0
for i in range(1, len(a)):
    total += abs(a[i] - a[i - 1])
for i in range(1, len(a) - 1):
    print(total - abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i]) + abs(a[i + 1] - a[i - 1]))
Buggy python submission (verdict: Runtime Error):
def main():
    BK = '#'
    WH = '.'

    A, B = map(int, input().split())

    g = []
    for _ in range(50):
        g.append([BK] * 50)
    for _ in range(50):
        g.append([WH] * 50)

    r, c = 1, 1
    for w_cnt in range(A - 1):
        g[r][c] = WH
        c += 2
        if c > 99:
            c = 1
            r += 2

    r, c = 51, 1
    for b_cnt in range(B - 1):
        g[r][c] = BK
        c += 2
        if c > 99:
            c = 1
            r += 2

    print(100, 100)
    for row in g:
        print(*row, sep='')


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    BK = '#'
    WH = '.'

    A, B = map(int, input().split())

    g = []
    for _ in range(50):
        g.append([BK] * 100)  # 100にし忘れていた
    for _ in range(50):
        g.append([WH] * 100)

    r, c = 1, 1
    for _ in range(A - 1):
        g[r][c] = WH
        c += 2
        if c > 99:
            c = 1
            r += 2

    r, c = 51, 1
    for _ in range(B - 1):
        g[r][c] = BK
        c += 2
        if c > 99:
            c = 1
            r += 2

    print(100, 100)
    for row in g:
        print(*row, sep='')


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(h)]

nc = dict()
for row in range(h):
    for col in range(w):
        v = A[row][col]
        nc[v] = (row, col)

q = int(input())
lrs = [list(map(int, input().split())) for _ in range(q)]

for lr in lrs:
    l, r = lr
    ans = 0
    while l != r:
        i, j = nc[l]
        x, y = nc[l + d]
        ans += abs(x - i) + abs(y - j)
        l += d
    print(ans)


Fixed submission:
h, w, d = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(h)]

nc = dict()
for row in range(h):
    for col in range(w):
        v = A[row][col]
        nc[v] = (row, col)

q = int(input())
lrs = [list(map(int, input().split())) for _ in range(q)]

c_sums = [[0] for _ in range(d)]
for r in range(d):
    k = 0
    if r == 0: k = 1
    while r + (k + 1) * d <= h * w:
        i, j = nc[r + k * d]
        x, y = nc[r + (k + 1) * d]
        t = c_sums[r][-1] + abs(x - i) + abs(y - j)
        c_sums[r].append(t)
        k += 1

for lr in lrs:
    l, r = lr
    ld, rest = divmod(l, d)
    rd = r // d
    if rest == 0:
        ld -=1
        rd -=1
    print(c_sums[rest][rd] - c_sums[rest][ld])
Buggy python submission (verdict: Runtime Error):
N = list(input())
if len(N) == 1:
    print(int(N))
else:
    print((len(N) - 1) * 9 + (int(N[0]) - 1))


Fixed submission:
n = input()
n_length = len(n)

ans = -1
if int(n) < 10:
    ans = int(n)
else:
    flg = True
    for i in range(1, n_length):
        if n[i] != '9':
            flg = False
            break
    if flg:
        ans = sum(map(int, list(n)))
    else:
        ans = 9 * (n_length - 1) + int(n[0]) - 1
print(ans)
Buggy python submission (verdict: Runtime Error):
a= int(input())
b= int(input())
c= int(input())
x= int(input())

ret = 0
for ia in range(0, a+1):
    for ib in range(0, b+1):
        for ic in range(0, c+1):
            if 500*ia + 100*ib + 50*ic == x:
                ret += 1
return ret

Fixed submission:
a= int(input())
b= int(input())
c= int(input())
x= int(input())

ret = 0
for ia in range(0, a+1):
    for ib in range(0, b+1):
        for ic in range(0, c+1):
            if 500*ia + 100*ib + 50*ic == x:
                ret += 1
print(ret)
Buggy python submission (verdict: Runtime Error):
def BlCannotGo(dt, dx, dy): ## 行けないならTrue
    dist = dx + dy
    if (dt % 2) != (dist % 2) or dist > dt:
        return True
    else:
        return False

def abs(x,y):
    return max(x,y) - min(x,y)

if __name__ == '__main__':
    n = int(input())
    t, x, y = list(), list(), list()
    t[0], x[0], y[0] = 0, 0, 0
    for i in range(n):
        t[i+1], x[i+1], y[i+1] = map(int, input().split())

    for i in range(1,n+1):
        dt = t[i] -  t[i-1]
        dx = abs(x[i] -  x[i-1])
        dy = abs(y[i] -  y[i-1])
        if BlCannotGo(dt, dx, dy):
            print('NO')    
            exit()

    print('YES')


Fixed submission:
def BlCannotGo(dt, dx, dy): ## 行けないならTrue
    if (dt % 2) != ((dx + dy) % 2) or (dx + dy) > dt:
        return True
    else:
        return False

if __name__ == '__main__':
    n = int(input())
    l = list(list(map(int, input().split())) for _ in range(n))

    dt = l[0][0]
    dx = l[0][1]
    dy = l[0][2]
    if BlCannotGo(dt, dx, dy):
        print('No')    
        exit()
        
    for i in range(1,n):
        dt = l[i][0] -  l[i-1][0] 
        dx = l[i][1] -  l[i-1][1]
        dy = l[i][2] -  l[i-1][2]
        if BlCannotGo(dt, dx, dy):
            print('No')    
            exit()

    print('Yes')

Buggy python submission (verdict: Runtime Error):
def BlCannotGo(dt, dx, dy): ## 行けないならTrue
    dist = dx + dy
    if (dt % 2) != (dist % 2) or dist > dt:
        return True
    else:
        return False

if __name__ == '__main__':
    n = int(input())
    t, x, y = list(), list(), list()
    t[0], x[0], y[0] = 0, 0, 0
    for i in range(n):
        t[i+1], x[i+1], y[i+1] = map(int, input().split())

    for i in range(1,n+1):
        dt = t[i] -  t[i-1]
        dx = abs(x[i] -  x[i-1])
        dy = abs(y[i] -  y[i-1])
        if BlCannotGo(dt, dx, dy):
            print('NO')    
            exit()

    print('YES')


Fixed submission:
def BlCannotGo(dt, dx, dy): ## 行けないならTrue
    if (dt % 2) != ((dx + dy) % 2) or (dx + dy) > dt:
        return True
    else:
        return False

if __name__ == '__main__':
    n = int(input())
    l = list(list(map(int, input().split())) for _ in range(n))

    dt = l[0][0]
    dx = l[0][1]
    dy = l[0][2]
    if BlCannotGo(dt, dx, dy):
        print('No')    
        exit()
        
    for i in range(1,n):
        dt = l[i][0] -  l[i-1][0] 
        dx = l[i][1] -  l[i-1][1]
        dy = l[i][2] -  l[i-1][2]
        if BlCannotGo(dt, dx, dy):
            print('No')    
            exit()

    print('Yes')
Buggy python submission (verdict: Time Limit Exceeded):
class Acc2D:
    def __init__(self, a, K):
        h, w = len(a), len(a[0])
        self.acc2D = self._build(h, w, a)
        self.K = K

    def _build(self, h, w, a):
        ret = [[0] * (w + 1) for _ in range(h + 1)]
        for r in range(h):
            for c in range(w):
                ret[r + 1][c + 1] = ret[r][c + 1] + ret[r + 1][c] - ret[r][c] + a[r][c]
                # 末項は必要に応じて改変すること
        return ret

    def get(self, r1, r2, c1, c2):
        # [r1,r2), [c1,c2) : 0-indexed
        if r1 < 0: r1 = 0
        if r2 >= self.K * 2: r2 = self.K * 2
        if c1 < 0: c1 = 0
        if c2 >= self.K * 2: c2 = self.K * 2
        acc2D = self.acc2D
        return acc2D[r2][c2] - acc2D[r1][c2] - acc2D[r2][c1] + acc2D[r1][c1]


def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())

    g = [[0] * (K * 2) for _ in range(K * 2)]
    for _ in range(N):
        x, y, c = input().split()
        x = int(x) % (K * 2)
        y = (int(y) - (K if c == 'B' else 0)) % (K * 2)
        g[x][y] += 1  # White

    acc2d = Acc2D(g, K)
    ans = -1
    for r in range(K):
        for c in range(K):
            t = acc2d.get(0, r, 0, c)
            t += acc2d.get(0, r, c + K, c + K * 2)
            t += acc2d.get(r, r + K, c, c + K)
            t += acc2d.get(r + K, r + K * 2, 0, c)
            t += acc2d.get(r + K, r + K * 2, c + K, c + K * 2)
            ans = max(ans, t, N - t)
    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()


Fixed submission:
# https://atcoder.jp/contests/abc086/submissions/3454297

def main():
    import sys

    input = sys.stdin.readline

    N, K = map(int, input().split())

    grid = [[0] * (K * 2) for _ in range(K * 2)]
    for _ in range(N):
        col, row, color = input().split()
        col = int(col)
        row = int(row)
        if color == 'W':
            row += K
            # Bに統一
        row %= K * 2
        col %= K * 2
        grid[row][col] += 1

    tbl = [[0] * (K * 2 + 1) for _ in range(K * 2 + 1)]
    for row in range(K * 2):
        for col in range(K * 2):
            tbl[row + 1][col + 1] = tbl[row + 1][col] + tbl[row][col + 1] - tbl[row][col] + grid[row][col]

    def count(row, col, row2, col2):
        return tbl[row2][col2] - tbl[row][col2] - tbl[row2][col] + tbl[row][col]

    ret = 0
    for row in range(K):
        for col in range(K):
            tmp = (
                    count(0, 0, row, col)
                    + count(row, col, row + K, col + K)
                    + count(row + K, col + K, K * 2, K * 2)
                    + count(row + K, 0, K * 2, col)
                    + count(0, col + K, row, K * 2)
            )
            ret = max(ret, tmp, N - tmp)
            # xox
            # oxo
            # xox
            # x=tmp
            # o=N-tmp

    print(ret)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
knd = len(set(a))##種類数
d = dict(list((_, a.count(_))) for _ in set(a))
print(sum(sorted(d.values())[:knd - k]))    

Fixed submission:
n, k = map(int, input().split())
a = input().split()

d = dict()
for i in a:
    d[i] = d.get(i, 0) + 1
d_sorted = sorted(d.values())
knd = len(set(a))##種類数
print(sum(d_sorted[:knd - k]))

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))

d = dict(list((_, a.count(_))) for _ in set(a))
a_cnt = sorted(d.values())

cnt = 0##書き換えた数
for i in range(len(set(a)) - k):##書き換え必要な種類の数
    cnt += a_cnt[i]
print(cnt)    

Fixed submission:
n, k = map(int, input().split())
a = input().split()

d = dict()
for i in a:
    d[i] = d.get(i, 0) + 1
d_sorted = sorted(d.values())
knd = len(set(a))##種類数
print(sum(d_sorted[:knd - k]))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
a2 = sorted(a, reverse=True)

b = list(map(int, input().split()))
b2 = sorted(b, reverse=True)

c = list(map(int, input().split()))
c2 = sorted(c, reverse=True)

cnt = 0
for x in a2:
    for y in b2:
        if x >= y:
            break
        #x < y
        for z in c2:
            if y >= z:
                break
            #y < z
            cnt += 1
print(cnt)

Fixed submission:
'''
https://beta.atcoder.jp/contests/abc077/submissions/2532572
'''
from bisect import bisect_left, bisect_right

def inpl():
    return list(map(int, input().split()))

n = int(input())
a = sorted(inpl())
b = sorted(inpl())
c = sorted(inpl())

ans = 0
for v in b:
    ans += bisect_left(a, v) * (n - bisect_right(c, v))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def v_max(x):#x(sec)における最大速度
    res = x
    
    for i in range(n + 1):
        t_l = t2[i]
        t_r = t2[i + 1]
        
        if 0 <= x < t_l:
            vi = v[i] + (t_l - x)
        elif t_l <= x <= t_r:
            vi = v[i]
        else:
            vi = v[i] + (x - t_r)
            
        res = min(res, vi)
        
    return res

n = int(input())
t = list(map(int, input().split()))
t2 = [0]#累積和
for i in range(n):
    t2.append(t2[i] + t[i])#0, t1, t1 + t2, t1 + t2 + t3, ...のように増えていく
finish_t = t2[-1]#終了時刻
t2.append(finish_t)
v = list(map(int, input().split())) + [0]#v1, v2, ..., 0

#print(t)
#print(t2)
#print(v)

now = 0
v_l = 0
ans = 0
while now < finish_t:
    now += 0.5
    v_r =  v_max(now)
    ans += 0.25 * (v_l + v_r)

    v_l = v_r
    
print(ans)

Fixed submission:
'''
38分あたり
https://www.youtube.com/watch?v=T1zXzsoK268
'''
def input_num_list(): return list(map(int, input().split()))

n = int(input())
t = input_num_list()
T = sum(t)
v = input_num_list()
maxv = [100 for _ in range(2 * T + 1)]
maxv[0] = 0
maxv[2 * T] = 0

t_l, t_r = 0, 0
for i in range(n):
    t_l = t_r
    t_r += t[i]
    vv = v[i]
    for j in range(2 * t_l, 2 * t_r + 1):
        maxv[j] = min(maxv[j], vv)

#print(maxv)
i = 1
while i <= 2 * T:
    maxv[i] = min(maxv[i], maxv[i - 1] + 0.5)
    i += 1
#print(maxv)
i = 2 * T - 1
while i >= 0:
    maxv[i] = min(maxv[i], maxv[i + 1] + 0.5)
    i -= 1
#print(maxv)
ans = 0
i = 0
while i <= 2 * T - 1:
    ans += 0.25 * (maxv[i] + maxv[i + 1])
    i += 1
    
print(ans)
#print(maxv)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

for x in range(1, 3500 + 1):
    for y in range(1, 3500 + 1):
        p = n * x * y
        q = 4 * x * y - n * (x + y)
      
        if q == 0: continue

        z, r = divmod(p, q)
        
        if r == 0:
            if z > 0:
                print(x, y, z)
                exit()

Fixed submission:
N = int(input())

w = -1
flg = False
for h in range(1, 3500 + 1):
    for n in range(h, 3500 + 1):
        q = 4 * n * h - N * (n + h)
        #print(h, n, p, q)
        if q > 0:
            #q == 0: 1/wなしで4/Nに等しくなり不適（w無限大）
            #q < 0: p > 0より、w < 0となり不適
            p = N * n * h
            if p % q == 0:
                w = p // q
                flg = True
                break
    if flg:
        break
print(h, n, w)

Buggy python submission (verdict: Runtime Error):
n = int(input())

for x in range(1, 3500 + 1):
    for y in range(1, 3500 + 1):
        p = n * x * y
        q = 4 * x * y - n * (x + y)
        if p % q == 0:
            print(x, y, p // q)
            exit()

Fixed submission:
N = int(input())

w = -1
flg = False
for h in range(1, 3500 + 1):
    for n in range(h, 3500 + 1):
        q = 4 * n * h - N * (n + h)
        #print(h, n, p, q)
        if q > 0:
            #q == 0: 1/wなしで4/Nに等しくなり不適（w無限大）
            #q < 0: p > 0より、w < 0となり不適
            p = N * n * h
            if p % q == 0:
                w = p // q
                flg = True
                break
    if flg:
        break
print(h, n, w)

Buggy python submission (verdict: Runtime Error):
n = int(input())

for x in range(1, 3500 + 1):
    for y in range(1, 3500 + 1):
        p = n * x * y
        q = 4 * x * y - n * (x + y)
        if p % q == 0:
            print(p, q, p // q)
            break

Fixed submission:
N = int(input())

w = -1
flg = False
for h in range(1, 3500 + 1):
    for n in range(h, 3500 + 1):
        q = 4 * n * h - N * (n + h)
        #print(h, n, p, q)
        if q > 0:
            #q == 0: 1/wなしで4/Nに等しくなり不適（w無限大）
            #q < 0: p > 0より、w < 0となり不適
            p = N * n * h
            if p % q == 0:
                w = p // q
                flg = True
                break
    if flg:
        break
print(h, n, w)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

w = -1
flg = False
for h in range(1, 3500 + 1):
    for n in range(h, 3500 + 1):
        p = N * n * h
        q = 4 * n * h - N * (n + h)
        #print(h, n, p, q)
        if q != 0:#q == 0のとき、1/wなしで4/Nに一致するので不適（wは無限大になる）
            if p % q == 0:
                w = p // q
                if w > 0:
                    flg = True
                    break
    if flg:
        break
print(h, n, w)

Fixed submission:
N = int(input())

w = -1
flg = False
for h in range(1, 3500 + 1):
    for n in range(h, 3500 + 1):
        q = 4 * n * h - N * (n + h)
        #print(h, n, p, q)
        if q > 0:
            #q == 0: 1/wなしで4/Nに等しくなり不適（w無限大）
            #q < 0: p > 0より、w < 0となり不適
            p = N * n * h
            if p % q == 0:
                w = p // q
                flg = True
                break
    if flg:
        break
print(h, n, w)
Buggy python submission (verdict: Time Limit Exceeded):
a, b, c, d, e, f = map(int, input().split())
w = []
for i in range(f):
    for j in range(f):
          t = 100 * (a * i + b * j)
          if 0 < t <= f:
              w.append(t)
s = []
for i in range(f):
    for j in range(f):
          t = c * i + d * j
          if t <= f:
              s.append(t)

ans_ws = -1
ans_s = -1
buf = -1
for water in w:
    for sugar in s:
          if water + sugar > f:
              continue
          concentration = (sugar * 100) / (water + sugar)
          if concentration <= (e * 100) / (100 + e):
              if buf < concentration:
                  buf = concentration
                  ans_ws = water + sugar
                  ans_s = sugar
print(ans_ws, ans_s)

Fixed submission:
a, b, c, d, e, f = map(int, input().split())
w = []
for i in range(f // 100 + 2):
    for j in range(f // 100 + 2):
          t = 100 * (a * i + b * j)
          if 0 < t <= f:
              w.append(t)
s = []
for i in range(f // c + 2):
    for j in range(f // d + 2):
          t = c * i + d * j
          if t <= f:
              s.append(t)

ans_ws = -1
ans_s = -1
buf = -1

sat = (e * 100) / (100 + e)
for water in w:
    for sugar in s:
          ttl = (water + sugar)
          if ttl > f:
              continue
          concentration = (sugar * 100) / ttl
          if concentration <= sat:
              if buf < concentration:
                  buf = concentration
                  ans_ws = ttl
                  ans_s = sugar
print(ans_ws, ans_s)