Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = list(input())

sl = 0
for i in range(N-1, -1, -1):
    if S[i] == S[0]:
        sr = i
        break
    else:
        sl += 1
if sl == 0:
    sl = 1
r = sr
l = 0
change = 0
while l<=r and change <= K:
    if S[l] != S[l+1]:
        if S[r] != S[r-1]:
            change += 1
            l += 1
            r -= 1
        else:
            r -= 1
    else:
        l += 1
if change < K:
    ans = N-1
else:
    if r < l:
        ans = sr + sl-1
    else:
        ans = l-1 + (sr-r-1) + sl-1
        for i in range(l, r):
            if S[i] == S[i+1]:
                ans += 1
print(ans)

Fixed submission:
N, K = map(int, input().split())
S = list(input())

c = 0
p = 0
for i, s in enumerate(S):
    if i == 0:
        pres = s
        continue
    elif s != pres:
        c += 1
    else:
        p += 1
    pres = s

if c == 0 or c == 1 or c <= 2*K:
    ans = N-1
else:
    ans = p+2*K
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    graph = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    PX = [list(map(int, input().split())) for _ in range(Q)]

    score = [0 for _ in range(N)]
    for p, x in PX:
        score[p-1] += x

    checked = [False for _ in range(N)]
    checked[0] = True
    def dfs(p, s, checked, score):
        #print(p, s, score)
        ns = s + score[p]
        score[p] += s
        for q in graph[p]:
            if not checked[q]:
                checked[q] = True
                score = dfs(q, ns, checked, score)
        return score

    ans = dfs(0, 0, checked, score)
    print(' '.join([str(a) for a in ans]))


if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(900000)


N, Q = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
PX = [list(map(int, input().split())) for _ in range(Q)]

def dfs(p, s, checked, score):
    #print(p, s, score)
    ns = s + score[p]
    score[p] += s
    for q in graph[p]:
        if not checked[q]:
            checked[q] = True
            score = dfs(q, ns, checked, score)
    return score


def main():


    score = [0 for _ in range(N)]
    for p, x in PX:
        score[p-1] += x

    checked = [False for _ in range(N)]
    checked[0] = True
    
    ans = dfs(0, 0, checked, score)
    print(' '.join([str(a) for a in ans]))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):

def main():
    S = list(input())
    T = list(input())
    sl = len(S)

    Sdic = {}
    for i, s in enumerate(S):
        if not s in Sdic.keys():
            Sdic[s] = [i]
        else:
            Sdic[s].append(i)
    pret = -1
    ans = 0
    for t in T:
        try:
            update = False
            for it in Sdic[t]:
                if it > pret:
                    update = True
                    pret = it
                    break
            if not update:
                ans += sl
                pret = Sdic[t][0]
            #print(t, update, pret)
        except KeyError:
            ans = -1
            break
    if ans != -1:
        ans += pret+1
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
from bisect import bisect_left, bisect_right


def main():
    S = list(input())
    T = list(input())
    sl = len(S)

    Sdic = {}
    for i, s in enumerate(S):
        if not s in Sdic.keys():
            Sdic[s] = [i]
        else:
            Sdic[s].append(i)
    pret = -1
    ans = 0
    for t in T:
        try:
            #print(Sdic[t])
            it = bisect_right(Sdic[t], pret)
            #print(it, t)
            if it < len(Sdic[t]):
                pret = Sdic[t][it]
            else:
                ans += sl
                pret = Sdic[t][0]
            #print(t, update, pret)
        except KeyError:
            ans = -1
            break
    if ans != -1:
        ans += pret+1
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq as hp


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    q = []
    for i, b in enumerate(B):
        hp.heappush(q, (-b, i))

    c = 0
    while q:
        #print(q)
        _, ind = hp.heappop(q)
        b = B[ind]
        if ind == N-1:
            s = B[ind-1]+B[0]
        else:
            s = B[ind-1]+B[ind+1]
        #print(b, s)
        if (b-A[ind])%s == 0:
            B[ind] = A[ind]
            c += (b-A[ind])//s
        elif b%s <= A[ind] or b < s:
            c = -1
            break
        else:
            c += b//s
            B[ind] = b%s
            hp.heappush(q, (-B[ind], ind))

    print(c)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
import heapq as hp


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    q = []
    for i, b in enumerate(B):
        hp.heappush(q, (-b, i))

    c = 0
    while q:
        #print(q)
        _, ind = hp.heappop(q)
        b = B[ind]
        if ind == N-1:
            s = B[ind-1]+B[0]
        else:
            s = B[ind-1]+B[ind+1]
        #print(b, s)
        if (b-A[ind])%s == 0:
            B[ind] = A[ind]
            c += (b-A[ind])//s
        elif b%s <= A[ind] or b < s:
            c = -1
            break
        else:
            c += b//s
            B[ind] = b%s
            hp.heappush(q, (-B[ind], ind))

    print(c)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(input()) for _ in range(N)]

c = 0
colored = [False for _ in range(N)]
Ls = []
for a in A:

    k = -1
    for i, l in enumerate(Ls):
        if l < a and a > k:
            ind = i
            k = a
    if k != -1:
        Ls[ind] = a
    else:
        Ls.append(a)


print(len(Ls))


Fixed submission:
from bisect import bisect_right, bisect_left

N = int(input())
A = [int(input()) for _ in range(N)]

dp = [1]*N

for a in A:
    ia = bisect_right(dp, -a)
    if dp[ia] == 1:
        dp[ia] = -a
    else:
        ib = bisect_left(dp, -a+1)
        dp[ib] = -a
    #print(dp)

ans = 0
for i in range(N):
    if dp[i] != 1:
        ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

k = {}
for i in range(N):
    if not A[i] in k:
        k[A[i]] = 1
    else:
        k[A[i]] += 1

a = 0
if len(k.keys()) == 3:
    B = 0
    for key, val in k.items():
        if val == N // 3:
            a += 1
        B = B ^ key
elif len(k.keys()) == 2:
    for key, val in k.items():
        if val == N // 3 and key == 0:
            a += 1
        elif val == N // 3 * 2:
            a += 2
    B = 0

if N % 3 == 0 and B == 0 and a == 3:
    ans = 'Yes'
else:
    ans = 'No'

print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

x = False
k = {}
for i in range(N):
    if not A[i] in k:
        k[A[i]] = 1
    else:
        k[A[i]] += 1
B = 0
a = 0
if len(k.keys()) == 3:
    B = 0
    for key, val in k.items():
        if val == N // 3:
            a += 1
        B = B ^ key
elif len(k.keys()) == 2:
    for key, val in k.items():
        if val == N // 3 and key == 0:
            a += 1
        elif val == N // 3 * 2:
            a += 2
    B = 0
elif len(k.keys()) == 1:
    a = list(k.keys())
    if a[0] == 0:
        x = True

if (N % 3 == 0 and B == 0 and a == 3) or x:
    ans = 'Yes'
else:
    ans = 'No'

print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split()) # 横に2個

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

for i in range(1, K+1):
    ans = (cmb(N-K+1, i) * cmb(K-1, i-1)) % (1E9+7)
    print(int(ans))

Fixed submission:
N, K = map(int, input().split()) # 横に2個

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
X = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, X + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


for i in range(1, K+1):
    ans = (cmb(N-K+1, i, mod) * cmb(K-1, i-1, mod)) % mod
    print(int(ans))
Buggy python submission (verdict: Runtime Error):
import math

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

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

A_C = A // C
A_D = A // D
A_CD = A // lcm(C, D)

B_C = B // C
B_D = B // D
B_CD = B // lcm(C, D)

A_e = A - (A_C + A_D - A_CD)
B_e = B - (B_C + B_D - B_CD)

if A % C == 0 or A % D == 0:
    ans = B_e - A_e
else:
    ans = B_e - A_e + 1
print(ans)

Fixed submission:
A, B, C, D = map(int, input().split())

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

def lcm(x, y):
    return (x * y) // gcd(x, y)

A_C = A // C
A_D = A // D
A_CD = A // lcm(C, D)

B_C = B // C
B_D = B // D
B_CD = B // lcm(C, D)

A_e = A - (A_C + A_D - A_CD)
B_e = B - (B_C + B_D - B_CD)

if A % C == 0 or A % D == 0:
    ans = B_e - A_e
else:
    ans = B_e - A_e + 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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

MAX = 10**5
Xs = [[] for _ in range(MAX+1)]
Ys = [[] for _ in range(MAX+1)]
for i, (x, y) in enumerate(XY):
    Xs[x].append(i)
    Ys[y].append(i)

ans = 0
checked = [False]*N
for i in range(N):
    if checked[i]: continue
    q = [i]
    checked[i] = True
    X = set()
    Y = set()
    c = 0
    while q:
        qq = []
        for p in q:
            x, y = XY[p]
            X.add(x)
            Y.add(y)
            c += 1
            for ni in Xs[x]:
                if not checked[ni]:
                    checked[ni] = True
                    qq.append(ni)
            for ni in Ys[y]:
                if not checked[ni]:
                    checked[ni] = True
                    qq.append(ni)
        q = qq
    ans += len(X)*len(Y)-c

print(ans)

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


def main():
    N = int(input())
    XY = [list(map(int, input().split())) for _ in range(N)]

    MAX = 10**5
    Xs = [[] for _ in range(MAX+1)]
    Ys = [[] for _ in range(MAX+1)]
    for i, (x, y) in enumerate(XY):
        Xs[x].append(i)
        Ys[y].append(i)

    ans = 0
    checked = [False]*N
    for i in range(N):
        if checked[i]: continue
        q = [i]
        checked[i] = True
        X = set()
        Y = set()
        c = 0
        while q:
            qq = []
            for p in q:
                x, y = XY[p]
                X.add(x)
                Y.add(y)
                c += 1
                for ni in Xs[x]:
                    if not checked[ni]:
                        checked[ni] = True
                        qq.append(ni)
                for ni in Ys[y]:
                    if not checked[ni]:
                        checked[ni] = True
                        qq.append(ni)
            q = qq
        ans += len(X)*len(Y)-c

    print(ans)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
mod = 10**9+7

dp = [[0 for _ in range(N+1)] for _ in range(M+1)]

for i in range(M):
    for j in range(N):
        if S[j] == T[i]:
            dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] + 1
        else:
            dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] - dp[i][j]
        dp[i+1][j+1] %= mod

ans = (dp[M][N] + 1) % mod
print(ans)

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

N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
mod = 10**9+7

dp = [[0 for _ in range(N+1)] for _ in range(M+1)]

for i in range(M):
    for j in range(N):
        if S[j] == T[i]:
            dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] + 1
        else:
            dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] - dp[i][j]
        dp[i+1][j+1] %= mod

ans = (dp[M][N] + 1) % mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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

dp = [[0, 0, 0, 0] for _ in range(N+1)]

for i in range(3):
    a, b = A[i], B[i]
    for w in range(N+1):
        if w < a:
            dp[w][i+1] = dp[w][i]
        else:
            dp[w][i+1] = max(dp[w][i], dp[w-a][i+1] + b-a)

M = dp[N][3] + N

dp2 = [[0, 0, 0, 0] for _ in range(M+1)]
for i in range(3):
    a, b = A[i], B[i]
    for w in range(M+1):
        if w < b:
            dp2[w][i+1] = dp2[w][i]
        else:
            dp2[w][i+1] = max(dp2[w][i], dp2[w-b][i+1] + a-b)

print(dp2[M][3]+M)

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    dp = [0]*((N+1)*4)

    for i in range(3):
        a, b = A[i], B[i]
        for w in range(N+1):
            if w < a:
                dp[w*4+i+1] = dp[w*4+i]
            else:
                dp[w*4+i+1] = max(dp[w*4+i], dp[(w-a)*4+i+1] + b-a)

    M = dp[N*4+3] + N

    dp2 = [0]*((M+1)*4)
    for i in range(3):
        a, b = A[i], B[i]
        for w in range(M+1):
            if w < b:
                dp2[w*4+i+1] = dp2[w*4+i]
            else:
                dp2[w*4+i+1] = max(dp2[w*4+i], dp2[(w-b)*4+i+1] + a-b)

    print(dp2[M*4+3]+M)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import copy

H, W = map(int, input().split())
state = []
for i in range(H):
    row_list = list(input())
    state.append(row_list)


def light(row, col, state):
    lighted_count = 0
    col1 = copy.copy(col)
    next = '.'
    while next == '.':
        lighted_count += 1
        col1 += 1
        if col1 >= W:
            break
        next = state[row][col1]
    
    col2 = copy.copy(col)
    next = '.'
    while next == '.':
        lighted_count += 1
        col2 -= 1
        if col2 < 0:
            break
        next = state[row][col2]

    row1 = copy.copy(row)
    next = '.'
    while next == '.':
        lighted_count += 1
        row1 += 1
        if row1 >= H:
            break
        next = state[row1][col]

    row2 = copy.copy(row)
    next = '.'
    while next == '.':
        lighted_count += 1
        row2 -= 1
        if row2 < 0:
            break
        next = state[row2][col]

    lighted_count -= 3
    return lighted_count



def blank_fun(state):
    blank_list = []
    for h in range(H):
        for w in range(W):
            if state[h][w] == '.':
                blank_list.append([h, w])
    return blank_list


blank_list = blank_fun(state)
blank_count = 0
for blank in blank_list:
    row = blank[0]
    col = blank[1]
    blank_count_now = light(row, col, state)

    if blank_count_now > blank_count:
        blank_count = blank_count_now


print(blank_count)


Fixed submission:
a, b, c = map(int, input().split())

a1 = a+b
a2 = a+c
a3 = b+c
x = [a1, a2, a3]
print(min(x))
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M = map(int, input().split())
a = np.array([int(input()) for _ in range(M)])

step0 = 1
step1 = 0 if (1 in a) else 1

ans = [step0, step1]
for i in range(2, N+1):
    ans.append(0 if i in a else ans[-1] + ans[-2])
print(ans[-1] % 1000000007)

Fixed submission:
import numpy as np

N, M = map(int, input().split())
A = [1]*(N+1)
for _ in range(M):
    A[int(input())] = 0

step0 = 1
step1 = A[1]

ans = [step0, step1]
for i in range(2, N+1):
    now = (ans[-1] + ans[-2]) * A[i]
    ans.append(now)
print(ans[-1] % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M = map(int, input().split())
a = np.array([int(input()) for _ in range(M)])

step0 = 1
step1 = 0 if (1 in a) else 1

ans = [step0, step1]
for i in range(2, N+1):
    if i in a:
        now = 0
    else:
        now = ans[-1] + ans[-2]
    ans.append(now)
print(ans[-1] % 1000000007)

Fixed submission:
import numpy as np

N, M = map(int, input().split())
A = [1]*(N+1)
for _ in range(M):
    A[int(input())] = 0

step0 = 1
step1 = A[1]

ans = [step0, step1]
for i in range(2, N+1):
    now = (ans[-1] + ans[-2]) * A[i]
    ans.append(now)
print(ans[-1] % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M = map(int, input().split())
a_l = []
for _ in range(M):
    a_in = int(input())
    a_l.append(a_in)
a = np.array(a_l)

step0 = 1

if 1 in a:
    step1 = 0
else:
    step1 = 1

ans = [step0, step1]
for i in range(2, N+1):
    if i in a:
        now = 0
    else:
        now = ans[-1] + ans[-2]
    ans.append(now)
print(ans[-1] % 1000000007)

Fixed submission:
import numpy as np

N, M = map(int, input().split())
A = [1]*(N+1)
for _ in range(M):
    A[int(input())] = 0

step0 = 1
step1 = A[1]

ans = [step0, step1]
for i in range(2, N+1):
    now = (ans[-1] + ans[-2]) * A[i]
    ans.append(now)
print(ans[-1] % 1000000007)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K+1):
    for n in range(k+1):
        if n >= N:
            break
        A = V[:n]
        A.extend(V[N-k+n:])
        A.sort()
        c = sum(A)
        for i in range(min(K-k, k)):
            if A[i] <= 0:
                c -= A[i]
        #print(k, n, c)
        ans = max(ans, c)
print(ans)

Fixed submission:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K+1):
    for n in range(k+1):
        if n >= N or k > N:
            break
        A = V[:n]
        A.extend(V[N-k+n:])
        A.sort()
        
        c = sum(A)
        for i in range(min(K-k, k)):
            if A[i] <= 0:
                c -= A[i]
        #print(k, n, c)
        ans = max(ans, c)
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
import sys
sys.setrecursionlimit(10000)

graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    graph[a-1].append((b-1, c))
    graph[b-1].append((a-1, c))

color = [-1 for _ in range(N)]
def dfs(p, c, color):
    color[p] = c
    cinv = 0 if c == 1 else 1
    for q, d in graph[p]:
        if color[q] != -1:
            continue
        if d % 2 == 1:
            color = dfs(q, cinv, color)
        else:
            color = dfs(q, c, color)
    return color

A = dfs(0, 0, color)
for a in A:
    print(a)

Fixed submission:
N = int(input())
import sys
sys.setrecursionlimit(90000)

graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    graph[a-1].append((b-1, c))
    graph[b-1].append((a-1, c))

color = [-1 for _ in range(N)]
def dfs(p, c, color):
    color[p] = c
    cinv = 0 if c == 1 else 1
    for q, d in graph[p]:
        if color[q] != -1:
            continue
        if d % 2 == 1:
            color = dfs(q, cinv, color)
        else:
            color = dfs(q, c, color)
    return color

A = dfs(0, 0, color)
for a in A:
    print(a)

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

graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    graph[a-1].append((b-1, c))
    graph[b-1].append((a-1, c))


color = [-1 for _ in range(N)]
def dfs(p, c, color):
    color[p] = c
    cinv = 0 if c == 1 else 1
    for q, d in graph[p]:
        if color[q] != -1:
            continue
        if d % 2 == 1:
            color = dfs(q, cinv, color)
        else:
            color = dfs(q, c, color)
    return color
A = dfs(0, 0, color)
for a in A:
    print(a)

Fixed submission:
N = int(input())
import sys
sys.setrecursionlimit(90000)

graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    graph[a-1].append((b-1, c))
    graph[b-1].append((a-1, c))

color = [-1 for _ in range(N)]
def dfs(p, c, color):
    color[p] = c
    cinv = 0 if c == 1 else 1
    for q, d in graph[p]:
        if color[q] != -1:
            continue
        if d % 2 == 1:
            color = dfs(q, cinv, color)
        else:
            color = dfs(q, c, color)
    return color

A = dfs(0, 0, color)
for a in A:
    print(a)
Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())

RGB = sorted([R, G, B], reverse=True)
max1 = N // RGB[0] 

count = 0
for r in range(max1+1):
    max2 = min([N-r, (N-r*RGB[0])//RGB[1]])
    for g in range(max2+1):
        Bb = N - (r*RGB[0] + g*RGB[1])
        if Bb >= 0 and Bb % RGB[2] == 0:
            count += 1
print(count)

Fixed submission:
R, G, B, N = map(int, input().split())

RGB = sorted([R, G, B], reverse=True)
max1 = N // RGB[0] 

count = 0
for r in range(max1+1):
    max2 = min([N-r, (N-r*RGB[0])//RGB[1]])
    for g in range(max2+1):
        if (N - (r*RGB[0] + g*RGB[1])) % RGB[2] == 0:
            count += 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())

RGB = sorted([R, G, B], reverse=True)
max1 = N // RGB[0] 

count = 0
for r in range(max1+1):
    for g in range(N-r+1):
        Bb = N - (r*RGB[0] + g*RGB[1])
        if Bb >= 0 and Bb % RGB[2] == 0:
            count += 1
print(count)

Fixed submission:
R, G, B, N = map(int, input().split())

RGB = sorted([R, G, B], reverse=True)
max1 = N // RGB[0] 

count = 0
for r in range(max1+1):
    max2 = min([N-r, (N-r*RGB[0])//RGB[1]])
    for g in range(max2+1):
        if (N - (r*RGB[0] + g*RGB[1])) % RGB[2] == 0:
            count += 1
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline


mod = 998244353
N = int(input())
A = [int(input()) for _ in range(N)]

S = sum(A)
L = S//2
dp1 = [0]*(L+1)
dp2 = [0]*(L+1)
dp1[0] = 1
dp2[0] = 1
for a in A:
    for i in reversed(range(L-a+1)):
        dp1[i+a] = (dp1[i+a] + dp1[i])%mod
        dp2[i+a] = (dp2[i+a] + dp2[i]*2)%mod

p1 = 0
p2 = 0
for s in range(1,L+1):
    p1 = (p1 + dp1[s]*6)%mod
    p2 = (p2 + dp2[s]*3)%mod
ans = (pow(3, N, mod) + 3 - 3*pow(2, N, mod) - p2 + p1) % mod
print(ans)

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


mod = 998244353
N = int(input())
A = [int(input()) for _ in range(N)]

S = sum(A)
L = S//2
dp1 = [0]*(L+1)
dp2 = [0]*(L+1)
dp1[0] = 1
dp2[0] = 1
for a in A:
    for i in reversed(range(L-a+1)):
        dp1[i+a] = (dp1[i+a] + dp1[i])%mod
        dp2[i+a] = (dp2[i+a] + dp2[i]*2)%mod

p1 = 0
p2 = 0
for s in range(1,L+1):
    p1 = (p1 + dp1[s]*6)%mod
    p2 = (p2 + dp2[s]*3)%mod
ans = (pow(3, N, mod) + 3 - 3*pow(2, N, mod) - p2 + p1) % mod
print(ans)