Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
dp = [[0] * 2019 for _ in range(n + 1)]

for i in range(1, n + 1):
    x = int(s[i-1])
    dp[i][x] = 1
    for j in range(2019):
        dp[i][(j*10+x)%2019] += dp[i-1][j]

ans = 0

for i in range(1, n+1):
    ans += dp[i][0]

print(ans)

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

res = 0
cnt = [0] * 2019
for i in range(n):
    res = (res + int(s[n-1-i]) * pow(10, i, 2019)) % 2019
    cnt[res] += 1

ans = 0
for c in cnt:
    ans += c * (c-1) // 2
ans += cnt[0]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
dp = [[0] * 2019 for _ in range(n + 1)]

for i in range(1, n + 1):
    x = int(s[i-1])
    dp[i][x] = 1
    for j in range(2019):
        dp[i][(j*10+x)%2019] += dp[i-1][j]

ans = 0

for i in range(1, n+1):
    ans += dp[i][0]

print(ans)

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

res = 0
cnt = [0] * 2019
for i in range(n):
    res = (res + int(s[n-1-i]) * pow(10, i, 2019)) % 2019
    cnt[res] += 1

ans = 0
for c in cnt:
    ans += c * (c-1) // 2
ans += cnt[0]

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


dp = [[0] * (s + 1) for _ in range(n+1)]


for i in range(1, n+1):
    for j in range(1, s+1):
        if a[i-1] > j:
            dp[i][j] = dp[i-1][j]
        elif a[i-1] == j:
            dp[i][j] = dp[i-1][j] + i
        else:
            dp[i][j] = dp[i-1][j] + dp[i-1][j-a[i-1]]

ans = 0

for i in range(1, n+1):
    ans += dp[i][s]
    ans %= 998244353


print(ans)

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


dp = [[0] * (s + 1) for _ in range(n+1)]


for i in range(1, n+1):
    for j in range(1, s+1):
        if a[i-1] > j:
            dp[i][j] = dp[i-1][j]
        elif a[i-1] == j:
            dp[i][j] = (dp[i-1][j] + i)%998244353
        else:
            dp[i][j] = (dp[i-1][j] + dp[i-1][j-a[i-1]])%998244353

ans = 0

for i in range(1, n+1):
    ans += dp[i][s]
    ans %= 998244353


print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())

hanten = 0
sen = ""
mat = ""
for i in range(q):
    x = input()
    if x == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    else:
        if x[2] == "1":
            if hanten == 0:
                sen += x[4]
            else:
                mat += x[4]
        else:
            if hanten == 0:
                mat += x[4]
            else:
                sen += x[4]

if hanten == 0:
    print(sen[::-1] + s + mat)
else:
    print(mat[::-1] + s[::-1] + sen)

Fixed submission:
s = input()
q = int(input())

hanten = 0
sen = ""
mat = ""
for i in range(q):
    x = input()
    if x == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    else:
        if x[2] == "1":
            if hanten == 0:
                sen += x[4]
            else:
                mat += x[4]
        else:
            if hanten == 0:
                mat += x[4]
            else:
                sen += x[4]

if hanten == 0:
    print(sen[::-1] + s + mat)
else:
    print(mat[::-1] + s[::-1] + sen)
Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int, input().split())
s = input()

l = [[0] * p for _ in range(n+1)]
ans = 0

for i in range(n):
    m = int(s[i])
    l[i+1][m%p] = 1
    for j in range(p):
        if l[i][j] !=0:
            k = (j * 10 + m) % p
            l[i+1][k] += l[i][j]
    ans += l[i+1][0]




print(ans)

Fixed submission:
n,p = map(int, input().split())
s = input()

if p == 2 or p == 5:
    ans = 0
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i + 1
    print(ans)



else:
    res = 0
    cnt = [0] * p
    for i in range(n):
        res = (res + int(s[n-1-i]) * pow(10, i, p)) % p
        cnt[res] += 1

    ans = 0
    for c in cnt:
        ans += c * (c-1) // 2
    ans += cnt[0]
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n,aa,bb = map(int, input().split())
mod = 10 ** 9 + 7

def power_func(a,n,p):
  bi=str(format(n,"b"))#2進表現に
  res=1
  for i in range(len(bi)):
    res=(res*res) %p
    if bi[i]=="1":
      res=(res*a) %p
  return res

ans = power_func(2, n, mod) - 1

def framod(n, mod, a=1):
    for i in range(1,n+1):
        a=a * i % mod
    return a

def power(n, r, mod):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod

def comb(n, k, mod):
    a=framod(n, mod)
    b=framod(k, mod)
    c=framod(n-k, mod)
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod

ans -= comb(n,aa,mod) + comb(n,bb,mod)
ans %= mod

print(ans)

Fixed submission:
n,a,b = map(int, input().split())
mod = 10 ** 9 + 7

def power_func(a,n,p):
  bi=str(format(n,"b"))#2進表現に
  res=1
  for i in range(len(bi)):
    res=(res*res) %p
    if bi[i]=="1":
      res=(res*a) %p
  return res

ans = power_func(2, n, mod) - 1

comb = [1] * (b + 1)

for i in range(b):
    comb[i + 1] = comb[i] * (n - i) * pow(i + 1,mod - 2, mod) % mod

ans -= comb[a] + comb[b]
ans %= mod


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

def kuriage(x):
    if x == "":
        return "1"
    elif x[-1] != "9":
        return x[:-1] + str(int(x[-1]) + 1)
    else:
        return kuriage(x[:-1])


ans = 0

while n != "":
    m = int(n[-1])
    if m <= 5:
        ans += m
        n = n[:-1]
    else:
        ans += 10 - m
        n = kuriage(n[:-1])

print(ans)

Fixed submission:
n = input()

dp1 = [0] * (len(n) + 1)
dp2 = [1] * (len(n) + 1)

for i in range(1, len(n) + 1):
    m = int(n[i - 1])
    dp1[i] = min(dp1[i - 1] + m, dp2[i - 1] + 10 - m)
    dp2[i] = min(dp1[i - 1] + m + 1, dp2[i - 1] + 9 - m)

print(dp1[-1])
Buggy python submission (verdict: Runtime Error):
import numpy as np
import math
from functools import reduce

mod = 10**9+7
n = int(input())
a = list(map(int, input().split()))



def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct

l = [0] * (10 ** 6 + 1)

for i in range(n):
    soi = factorize(a[i])
    for j in range(len(soi)):
        l[soi[j][0]] = max(l[soi[j][0]], soi[j][1])

saiko = 1

for i in range(2, 10**6):
    saiko = (saiko * i ** l[i]) % mod

ans = 0

for i in range(n):
    ans += saiko * pow(a[i],mod - 2, mod)
    ans %= mod

print(ans)

Fixed submission:
mod = 10**9+7
n = int(input())
a = list(map(int, input().split()))



def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct

l = [0] * (10 ** 6 + 1)

for i in range(n):
    soi = factorize(a[i])
    for j in range(len(soi)):
        l[soi[j][0]] = max(l[soi[j][0]], soi[j][1])

saiko = 1

for i in range(2, 10**6):
    saiko = (saiko * i ** l[i]) % mod

ans = 0

for i in range(n):
    ans += saiko * pow(a[i],mod - 2, mod)
    ans %= mod

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
r = 10**9 + 7
n,k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

p = n - k + 1

c = 1

ans = 0

for i in range(p, 0, -1):
    ans += (a[n-i] - a[i-1]) * c
    ans %= r
    c = c * (n-i + 1) // (p-i+1)

print(ans)

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

mod = 10 ** 9 + 7

def make_fact(n):#0~nの階乗を求める
    fact = [1]*(n+1)
    for i in range(1, n+1):
        fact[i] = fact[i-1]*i%mod
    return fact
fact = make_fact(N) #＠
def make_fact_inv(n):#0~nの階乗のmodに関する逆元を求める
    fact_inv = [1]*(n+1)
    fact_inv[n] = pow(fact[n], mod-2, mod)#フェルマーの小定理
    for i in range(n, 0, -1):
        fact_inv[i-1] = fact_inv[i]*i%mod
    return fact_inv
fact_inv = make_fact_inv(N)#＠
def comb(n, k):#nCk
    return fact[n]*fact_inv[k]*fact_inv[n-k]%mod

a.sort()

pos = 0
neg = 0

for i in range(N - K + 1):
    pos += a[N - i - 1] * comb(N- i - 1, K - 1)
    pos %= mod
    neg += a[i] * comb(N- i - 1, K - 1)
    neg %= mod

ans = (pos - neg) % mod

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

ans = 0

if int(n) % 2 == 0:
    n = int(n[:-1])
    for i in range(27):
        ans += n // 5 ** i

print(ans)

Fixed submission:
n = int(input())

ans = 0

if n % 2 == 0:
    n //= 10
    for i in range(27):
        ans += n // 5 ** i

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
    c = []
    for j in range(60):
        if a[i] % 2**(j+1) == 0:
            c.append(0)
        else:
            c.append(1)
            a[i] -= 2**j
    b.append(c)

ans = 0

for i in range(60):
    c0 = 0
    c1 = 0
    for j in range(n):
        if b[j][i] == 0:
            c0 += 1
        else:
            c1 += 1
    ans += 2 ** i * c0 * c1
    ans %= 10**9 + 7

print(ans)

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

ans = 0
for j in range(60):
    c0 = 0
    c1 = 0
    for i in range(n):
        if a[i] % 2**(j+1) == 0:
            c0 += 1
        else:
            a[i] -= 2**j
            c1 += 1
    ans += 2 ** j * c0 * c1
    ans %= 10**9 + 7

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import queue

n,qq = map(int, input().split())

es = [[] for _ in range(n)]
for i in range(n - 1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    es[a].append(b)
    es[b].append(a)

c = [0] * n

for i in range(qq):
    p,x = map(int, input().split())
    c[p - 1] += x

q = queue.Queue()
q.put(0)

find = [False] * n

while not q.empty():
    v = q.get()
    find[v] = True
    for u in es[v]:
        if find[u]:
            continue
        c[u] += c[v]
        q.put(u)

print(*c)

Fixed submission:
from collections import deque
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
count = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(Q):
    p, x = map(int, input().split())
    count[p-1]+=x
q = deque([0])
flag = [False]*N
while q:
    v = q.pop()
    flag[v] = True
    for e in G[v]:
        if flag[e]:
            continue
        count[e]+=count[v]
        q.append(e)
print(*count)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

n,qq = map(int, input().split())

es = [[] for _ in range(n)]
for i in range(n - 1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    es[a].append(b)
    es[b].append(a)

c = [0] * n

for i in range(qq):
    p,x = map(int, input().split())
    c[p - 1] += x

q = queue.Queue()
q.put(0)

find = [False] * n
find[0] = True

while not q.empty():
    v = q.get()
    for u in es[v]:
        if not find[u]:
            c[u] += c[v]
            find[u] = True
            q.put(u)

print(*c)

Fixed submission:
from collections import deque
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
count = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(Q):
    p, x = map(int, input().split())
    count[p-1]+=x
q = deque([0])
flag = [False]*N
while q:
    v = q.pop()
    flag[v] = True
    for e in G[v]:
        if flag[e]:
            continue
        count[e]+=count[v]
        q.append(e)
print(*count)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

n,qq = map(int, input().split())

es = [[] for _ in range(n)]
for i in range(n - 1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    es[a].append(b)
    es[b].append(a)

c = [0] * n

for i in range(qq):
    p,x = map(int, input().split())
    c[p - 1] += x

q = queue.Queue()
q.put(0)

find = [False] * n
find[0] = True

while not q.empty():
    v = q.get()
    for u in es[v]:
        if not find[u]:
            c[u] += c[v]
            find[u] = True
            q.put(u)

print(*c)

Fixed submission:
from collections import deque
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
count = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(Q):
    p, x = map(int, input().split())
    count[p-1]+=x
q = deque([0])
flag = [False]*N
while q:
    v = q.pop()
    flag[v] = True
    for e in G[v]:
        if flag[e]:
            continue
        count[e]+=count[v]
        q.append(e)
print(*count)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
n = len(S)
x = 10 ** 9 + 7
a = [1,0,0,0,0,0,0,0,0,0,0,0,0]
 
c = [1, -3, -4, -1, 3, 4]
for i in range(n):
    b = [0] * 13
    p = i % 6
    if S[n - i - 1] == "?":
        for j in range(13):
            for k in range(10):
                b[j] += a[(j - c[p] * k) % 13]
            b[j] = b[j] % x
        a = b
    else:
        s = int(S[n - i - 1])
        for j in range(13):
            b[j] = a[(j - c[p] * s) % 13]
        a = b
print(a[5])

Fixed submission:
S = input()
n = len(S)
x = 10 ** 9 + 7
a = [1,0,0,0,0,0,0,0,0,0,0,0,0]

c = [1, -3, -4, -1, 3, 4]
for i in range(n):
    b = [0] * 13
    p = i % 6
    if S[n - i - 1] == "?":
        for j in range(13):
            for k in range(10):
                b[j] += a[(j - c[p] * k) % 13]
            b[j] = b[j] % x
        a = b
    else:
        s = int(S[n - i - 1])
        for j in range(13):
            b[j] = a[(j - c[p] * s) % 13]
        a = b
print(a[5])
Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
list = [0] * n
list[0] = 1
if n >= 2:
    list[1] = 2
if n >= 3:
    for i in range(2, n):
        list[i] = list[i - 2] + list[i - 1]



ans = 1
o = 0
for i in range(m):
    a = int(input())
    if a == o:
        ans = 0
        break
    if a > o + 1:
        ans = (ans * list[a - o - 2]) % 1000000007
    o = a + 1

if a + 1 < n:
    ans = (ans * list[n - a - 2]) % 1000000007
print(ans)

Fixed submission:
n,m = map(int, input().split())
list = [0] * n
list[0] = 1
if n >= 2:
    list[1] = 2
if n >= 3:
    for i in range(2, n):
        list[i] = list[i - 2] + list[i - 1]

if m == 0:
    print(list[n - 1] % 1000000007)
else:
    ans = 1
    o = 0
    for i in range(m):
        a = int(input())
        if a == o:
            ans = 0
            break
        if a > o + 1:
            ans = (ans * list[a - o - 2]) % 1000000007
        o = a + 1

    if a + 1 < n:
        ans = (ans * list[n - a - 2]) % 1000000007
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())
s = [input() for i in range(h)]
yoko = [([0] * w) for i in range(h)]
tate = [([0] * w) for i in range(h)]

for i in range(h):
    f = 0
    j = 0
    m = 0
    while j < w:
        if s[i][j] == ".":
            if m == 0:
                j += 1
            else:
                f = j
                m = 0
                j += 1
        else:
            if m == 0:
                for p in range(f, j):
                    yoko[i][p] = j - f
            m = 1
            j += 1
    if m == 0:
        for p in range(f, j):
            yoko[i][p] = j - f

for i in range(w):
    f = 0
    j = 0
    m = 0
    while j < h:
        if s[j][i] == ".":
            if m == 0:
                j += 1
            else:
                f = j
                m = 0
                j += 1
        else:
            if m == 0:
                for p in range(f, j):
                    tate[p][i] = j - f
            m = 1
            j += 1
    if m == 0:
        for p in range(f, j):
            tate[p][i] = j - f
ans = 0
for i in range(h):
    for j in range(w):
        q = yoko[i][j] + tate[i][j] - 1
        if q > ans:
            ans = q
print(ans)

Fixed submission:
h, w = map(int, input().split())
s = [input() for i in range(h)]
yoko = [([0] * w) for i in range(h)]
tate = [([0] * w) for i in range(h)]

for i in range(h):
    f = 0
    j = 0
    m = 0
    while j < w:
        if s[i][j] == ".":
            if m == 0:
                j += 1
            else:
                f = j
                m = 0
                j += 1
        else:
            if m == 0:
                for p in range(f, j):
                    yoko[i][p] = j - f
            m = 1
            j += 1
    if m == 0:
        for p in range(f, j):
            yoko[i][p] = j - f

for i in range(w):
    f = 0
    j = 0
    m = 0
    while j < h:
        if s[j][i] == ".":
            if m == 0:
                j += 1
            else:
                f = j
                m = 0
                j += 1
        else:
            if m == 0:
                for p in range(f, j):
                    tate[p][i] = j - f
            m = 1
            j += 1
    if m == 0:
        for p in range(f, j):
            tate[p][i] = j - f
ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            q = yoko[i][j] + tate[i][j] - 1
            if q > ans:
                ans = q
print(ans)
Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb = []
for i in range(m):
    b,c = map(int, input().split())
    cb += [[c,b]]
cb = sorted(cb)[::-1]

def solve():
    p = 0
    q = 0
    ans = 0
    while p < n and q < m:
        if a[p] < cb[q][0]:
            if a [p + cb[q][1] - 1] < cb[q][0] and p + cb[q][1] - 1 < n:
                ans += cb[q][0] * cb[q][1]
                p = p + cb[q][1]
                q += 1
            else:
                for i in range(cb[q][1]):
                    if p + i == n:
                        return(ans)
                    elif a[p + i] < cb[q][0]:
                        ans += cb[q][0]
                    else:
                        ans += sum(a[p + i:])
                        return(ans)
        else:
            ans += sum(a[p:])
            return(ans)
    if p == n:
        return(ans)
    else:
        ans += sum(a[p:])
        return(ans)
print(solve())

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

cb = []
for _ in range(m):
    b,c = map(int, input().split())
    cb.append([c, b])

a.sort()
cb.sort()

now = 0
finish = False
for i in range(m):
    b = cb[m - 1 - i][1]
    c = cb[m - 1 - i][0]
    for j in range(b):
        if now < n and a[now] < c:
            a[now] = c
            now += 1
        else:
            finish = True
            break
    if finish:
        break

print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb = []
for i in range(m):
    b,c = map(int, input().split())
    cb += [[c,b]]
cb = sorted(cb)[::-1]

def solve():
    p = 0
    q = 0
    ans = 0
    while p < n and q < m:
        if a[p] < cb[q][0]:
            if a [p + cb[q][1] - 1] < cb[q][0] and p + cb[q][1] - 1 < n:
                ans += cb[q][0] * cb[q][1]
                p = p + cb[q][1]
                q += 1
            else:
                for i in range(cb[q][1]):
                    if p + i == n:
                        return(ans)
                    elif a[p + i] < cb[q][0]:
                        ans += cb[q][0]
                    else:
                        ans += sum(a[p + i:])
                        return(ans)
        else:
            ans += sum(a[p:])
            return(ans)
    if q == n:
        return(ans)
    else:
        ans += sum(a[p:])
        return(ans)
print(solve())

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

cb = []
for _ in range(m):
    b,c = map(int, input().split())
    cb.append([c, b])

a.sort()
cb.sort()

now = 0
finish = False
for i in range(m):
    b = cb[m - 1 - i][1]
    c = cb[m - 1 - i][0]
    for j in range(b):
        if now < n and a[now] < c:
            a[now] = c
            now += 1
        else:
            finish = True
            break
    if finish:
        break

print(sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**5 + 1)

n = int(input())
go = [[]for _ in range(n)]
ge = [[]for _ in range(n)]

for _ in range(n-1):
    x,y,z = map(int, input().split())
    if z % 2 == 0:
        ge[x-1].append(y-1)
        ge[y-1].append(x-1)
    else:
        go[x-1].append(y-1)
        go[y-1].append(x-1)

color = [0] * n

def dfs(v, c):
    color[v] = c
    for i in range(len(go[v])):
        if color[go[v][i]] == 0:
            dfs(go[v][i], -c)
    for i in range(len(ge[v])):
        if color[ge[v][i]] == 0:
            dfs(ge[v][i], c)

dfs(0, 1)

for i in range(n):
    if color[i] == -1:
        print(0)
    else:
        print(1)

Fixed submission:
import queue

n = int(input())
go = [[]for _ in range(n)]
ge = [[]for _ in range(n)]
 
for _ in range(n-1):
    x,y,z = map(int, input().split())
    if z % 2 == 0:
        ge[x-1].append(y-1)
        ge[y-1].append(x-1)
    else:
        go[x-1].append(y-1)
        go[y-1].append(x-1)

color = [0] * n
color[0] = 1

q = queue.Queue()

q.put(0)

while not q.empty():
    v = q.get()
    for i in range(len(go[v])):
        if color[go[v][i]] == 0:
            color[go[v][i]] = - color[v]
            q.put(go[v][i])
    for i in range(len(ge[v])):
        if color[ge[v][i]] == 0:
            color[ge[v][i]] = color[v]
            q.put(ge[v][i])

for i in range(n):
    if color[i] == -1:
        print(0)
    else:
        print(1)

Buggy python submission (verdict: Runtime Error):
n = int(input())
go = [[]for _ in range(n)]
ge = [[]for _ in range(n)]

for _ in range(n-1):
    x,y,z = map(int, input().split())
    if z % 2 == 0:
        ge[x-1].append(y-1)
        ge[y-1].append(x-1)
    else:
        go[x-1].append(y-1)
        go[y-1].append(x-1)

color = [0] * n

def dfs(v, c):
    color[v] = c
    for i in range(len(go[v])):
        if color[go[v][i]] == 0:
            dfs(go[v][i], -c)
    for i in range(len(ge[v])):
        if color[ge[v][i]] == 0:
            dfs(ge[v][i], c)

dfs(0, 1)

for i in range(n):
    if color[i] == -1:
        print(0)
    else:
        print(1)

Fixed submission:
import queue

n = int(input())
go = [[]for _ in range(n)]
ge = [[]for _ in range(n)]
 
for _ in range(n-1):
    x,y,z = map(int, input().split())
    if z % 2 == 0:
        ge[x-1].append(y-1)
        ge[y-1].append(x-1)
    else:
        go[x-1].append(y-1)
        go[y-1].append(x-1)

color = [0] * n
color[0] = 1

q = queue.Queue()

q.put(0)

while not q.empty():
    v = q.get()
    for i in range(len(go[v])):
        if color[go[v][i]] == 0:
            color[go[v][i]] = - color[v]
            q.put(go[v][i])
    for i in range(len(ge[v])):
        if color[ge[v][i]] == 0:
            color[ge[v][i]] = color[v]
            q.put(ge[v][i])

for i in range(n):
    if color[i] == -1:
        print(0)
    else:
        print(1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
S = input()
left = []
right = []
cl = 0
cr = 0
for i in range(n):
  left = left + [cl]
  if S[i] == "#":
    cl += 1
  right = [cr] + right
  if S[n - 1 -i] == ".":
    cr += 1
min = S.count("#")
for i in range(n):
  if S[i] == "#":
    p = left[i] + right[i]
    if p < min:
      min = p
print(min)

Fixed submission:
n = int(input())
S = input()
left = []
right = []
cl = 0
cr = 0
for i in range(n):
  left += [cl]
  if S[i] == "#":
    cl += 1
  right += [cr]
  if S[n - 1 -i] == ".":
    cr += 1
min = cl
for i in range(n):
  if S[i] == "#":
    p = left[i] + right[n - 1 -i]
    if p < min:
      min = p
print(min)