Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02599 (python)

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

prevs = [-1] * (n+1)
# 左端
sts = [-1] * n
for i,c in enumerate(cs):
    c -= 1
    if prevs[c] != -1:
        sts[prevs[c]] = i
    prevs[c] = i

qs = [[] for i in range(n)] 
for i in range(q):
    l,r = map(int, input().split())
    l -= 1
    r -= 1
    qs[l].append((r,i))

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (n+10)

    def add(self, x, w):
        while x <= self.n:
            self.bit[x] += w
            x += x&-x

    def sum(self, x):
        cnt = 0
        while x > 0:
            cnt += self.bit[x]
            x -= x&-x
        return cnt

bit = BIT(n)
ans = [0] * q
for l in range(n-1,-1,-1):
    # 矢印を追加
    if sts[l] != -1:
        bit.add(sts[l], 1)
    for r,i in qs[l]:
        cnt = bit.sum(r)
        ans[i] = r - l + 1 - cnt

print(*ans, sep="\n")

Buggy submission (verdict: Runtime Error):
n,q = map(int, input().split())
cs = list(map(int, input().split()))

prevs = [-1] * n
# 左端
sts = [-1] * n
for i,c in enumerate(cs):
    if prevs[c] != -1:
        sts[prevs[c]] = i
    prevs[c] = i

qs = [[] for i in range(n)] 
for i in range(q):
    l,r = map(int, input().split())
    l -= 1
    r -= 1
    qs[l].append((r,i))

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (n+10)

    def add(self, x, w):
        while x <= self.n:
            self.bit[x] += w
            x += x&-x

    def sum(self, x):
        cnt = 0
        while x > 0:
            cnt += self.bit[x]
            x -= x&-x
        return cnt

bit = BIT(n)
ans = [0] * q
for l in range(n-1,-1,-1):
    # 矢印を追加
    if sts[l] != -1:
        bit.add(sts[l], 1)
    for r,i in qs[l]:
        cnt = bit.sum(r)
        ans[i] = r - l + 1 - cnt

print(*ans, sep="\n")
Problem p02642 (python)

Fixed submission:
from collections import defaultdict

n = int(input())
As  = list(map(int, input().split()))
if n == 1:
    print(1)
    exit()

Ss = sorted(As)
cnt = 0
d = defaultdict(int)
As = [False] * (10**6+10)

for s in Ss:
    d[s] += 1

for s in Ss:
    if As[s]:continue
    else:
        if d[s] == 1:
            cnt += 1
        ind = 1
        while s * ind <= 10**6:
            As[s*ind] = True
            ind += 1

print(cnt)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

n = int(input())
As  = list(map(int, input().split()))
if n == 1:
    print(0)
    exit()

Ss = sorted(As)
primes = set()
cnt = 0
d = defaultdict(int)
As = [False] * 10**6
for s in Ss:
    d[s] += 1
    if d[s] == 2:
        As[s] = True
# print(Ss)
for s in set(Ss):
    if As[s]: continue
    else:
        cnt += 1
        ind = 1
        while s * ind < 10**6:
            As[s*ind] = True
            ind += 1
print(cnt)
Problem p02698 (python)

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

from bisect import bisect_left
from collections import deque
"""
行き ⇨ dp LIS計算とstack登録
帰り ⇨ stackをpop dp巻き戻し
"""

n = int(input())
As = list(map(int, input().split()))
edges = [[] for i in range(n)]
for i in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append(v)
    edges[v].append(u)


INF = 10**10
dp = []
pos = 0
ans = [0] * n
q = deque([])
def dfs(v,p):
    global pos
    # 行き
    a = As[v]
    ind = bisect_left(dp, a)
    if ind == len(dp): 
        dp.append(a)
        # 引数の1つ目はappendしたか
        q.append((True, -1, -1))
    else:
        # 更新
        tmp = dp[ind]
        dp[ind] = a
        q.append((False,ind,tmp))

    ans[v] = len(dp)
    for u in edges[v]:
        if u == p:continue
        dfs(u,v)
        # 帰り        
        isappend, ind, tmp = q.pop()
        if isappend:
            dp.pop()
        else:
            dp[ind] = tmp
        
    
dfs(0,-1)

print(*ans)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left
from collections import deque
"""
行き ⇨ dp LIS計算とstack登録
帰り ⇨ stackをpop dp巻き戻し
"""

n = int(input())
As = list(map(int, input().split()))
edges = [[] for i in range(n)]
for i in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append(v)
    edges[v].append(u)


INF = 10**10
dp = []
pos = 0
ans = [0] * n
q = deque([])
def dfs(v,p):
    global pos
    # 行き
    a = As[v]
    ind = bisect_left(dp, a)
    if ind == len(dp): 
        dp.append(a)
        # 引数の1つ目はappendしたか
        q.append((True, -1, -1))
    else:
        # 更新
        tmp = dp[ind]
        dp[ind] = a
        q.append((False,ind,tmp))

    ans[v] = len(dp)
    for u in edges[v]:
        if u == p:continue
        dfs(u,v)
        # 帰り        
        isappend, ind, tmp = q.pop()
        if isappend:
            dp.pop()
        else:
            dp[ind] = tmp
        
    
dfs(0,-1)

print(*ans)
Problem p02715 (python)

Fixed submission:
N,K = map(int, input().split())
mod = 10**9+7
ans = 0
dp = [0] * (K+10)

for i in range(K,0,-1):
    x = K//i
    a = pow(x, N, mod)
    cnt = 2
    while i*cnt <= K:
        a -= dp[i*cnt]
        cnt += 1
    a %= mod
    dp[i] = a
    ans += a*i
    ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
import math
K = int(input())
ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            ans+=math.gcd(i,math.gcd(j,k)) 
print(ans)
Problem p02727 (python)

Fixed submission:
X,Y,A,B,C  = map(int, input().split())
ps = sorted(list(map(int, input().split())),reverse=True)
qs = sorted(list(map(int, input().split())),reverse=True)
rs = sorted(list(map(int, input().split())),reverse=True)

ps = ps[:X]
qs = qs[:Y]
cnt_c = 0
ans = sum(ps)+sum(qs)
INF = 10**10
while cnt_c < C:
    if len(ps) != 0:
        p = ps.pop()
    else:
        p = INF
    if len(qs) != 0:
        q = qs.pop()
    else:
        q = INF
    r = rs[cnt_c]
    if p - r >= 0 and q - r >= 0:
        break
    elif r - p > r - q: #pのほうが差分が大きい
        qs.append(q)
        ans -= p
        ans += r
    else:
        ps.append(p)
        ans -= q
        ans += r
    cnt_c += 1
print(ans)



"""
2 2 2 2 4
2 4
5 1
10 10 10 10 10
"""

Buggy submission (verdict: Runtime Error):
X,Y,A,B,C  = map(int, input().split())
ps = sorted(list(map(int, input().split())),reverse=True)
qs = sorted(list(map(int, input().split())),reverse=True)
# qs = list(map(int, input().split()))
rs = sorted(list(map(int, input().split())),reverse=True)

# cnt_x = 0
# cnt_y = 0
ps = ps[:X]
qs = qs[:Y]
cnt_c = 0
ans = sum(ps)+sum(qs)
while cnt_c < C:
    p = ps.pop()
    q = qs.pop()
    r = rs[cnt_c]
    if p - r >= 0 and q - r >= 0:
        break
    elif r - p > r - q: #pのほうが差分が大きい
        qs.append(q)
        ans -= p
        ans += r
    else:
        ps.append(p)
        ans -= q
        ans += r
    cnt_c += 1
print(ans)
Problem p02741 (python)

Fixed submission:
As = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
K = int(input())
print(As[K-1])

Buggy submission (verdict: Runtime Error):
As = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]

print(As[K-1])
Problem p02761 (python)

Fixed submission:
#!/usr/bin/env python3

N,M = map(int, input().split())
from collections import defaultdict
d = defaultdict(int)
S = [1]+[0]*(N-1)
flg = True
for i in range(M):
    s,c = map(int, input().split())
    s -= 1
    if d[s] > 0 and S[s] != c:
        flg = False
    S[s] = c
    d[s] += 1

if M == 0 and N == 1:
    print(0)
    exit()
if not flg:
    print(-1)
    exit()
if S[0] == 0:
    if len(S) == 1:
        print(*S)
    else:
        print(-1)
    exit()
print("".join([str(s) for s in S]))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

N,M = map(int, input().split())
from collections import defaultdict
d = defaultdict(int)
S = [1]+[0]*(N-1)
for i in range(M):
    s,c = map(int, input().split())
    s -= 1
    if d[s] > 0 and S[s] != c:
        print(-1)
        exit()
    S[s] = c
    d[s] += 1
if S[0] == 0:
    print(-1)
    exit()
if len(S) == 1:
    print("".join([str(s) for s in S]))
else:
    if d[s] == 0:
        x = [1]+[0]*(N-1)
        ans = "".join([str(_) for _ in x])
        print(ans)
    else:
        print("".join([str(s) for s in S]))
Problem p02824 (python)

Fixed submission:
n,m,v,p = map(int, input().split())
As = list(map(int, input().split()))
As.sort(reverse=True)
# print(As)
def f(x):
    # 判定基準
    if x < p:
        return True
    if As[x]+m < As[p-1]:
        return False
    cnt0 = (n-x)*m
    cnt1 = (p-1)*m
    cnt2 = 0
    for i in range(p-1, x):
        cnt2 += As[x]+m-As[i]
    return cnt0 + cnt1 + cnt2 >= m*v    


# 二部探索でOK/NG求める
ok = 0
ng = n
while ng - ok > 1:
    mid = (ok+ng)//2
    if f(mid):
        ok = mid
    else:
        ng = mid
print(ok+1)

Buggy submission (verdict: Runtime Error):
N,M,V,P = map(int, input().split())
As = list(map(int, input().split()))
As.sort(reverse=True)
# print(As)
"""
M人がV問のスコアを1挙げられる
N問のうち降順に並べたP問が採用される

6 1 2 2
2 1 1 3 0 2
方針
最小の数(-1)にVを足す
上からP-1番目までの数にMを足す
P番目の数がVを超えているかどうか判定
最小の数からindex-1
"""


x = N - (P-1)
y = max(0, V - P - 1) #除外対象
# print(P-1,y)
if y == 0:
    As = As[P-1:]
else:
    As = As[P-1:-y]
# 同じは弾く
As = [a for a in As if a > As[y]]
# print(As)
mx = max(As)
cnt = 0
for a in As:
    if a + M >= mx:cnt+=1
print(P-1+cnt)
Problem p02845 (python)

Fixed submission:
N = int(input())
As = list(map(int, input().split()))
dp = [0] * 1000000
mod = 10**9 + 7
ans = 1
dp[0] = 3
for a in As:
    a += 1
    ans *= dp[a-1]-dp[a]
    ans %= mod
    dp[a] += 1
print(ans%mod)

Buggy submission (verdict: Runtime Error):
N = int(input())
As = list(map(int, input().split()))
dp = [0] * 100000
mod = 10**9 + 7
ans = 1
dp[0] = 3
for a in As:
    a += 1
    ans *= dp[a-1]-dp[a]
    ans %= mod
    dp[a] += 1
print(ans%mod)
Problem p02850 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100000)
N = int(input())
edges = [[] for i in range(N)]
e_list = []

for i in range(N-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    e_list.append((a,b))
    edges[a].append((b,i))
    edges[b].append((a,i))

ans = [0] * (N-1)

def dfs(v, p=-1, c=-1): #親p(はじめはいないので-1), 子がv, 親の色も渡す必要あり
    # 子が次の子孫をグラフから探す
    global ans
    next_v = edges[v]
    cnt = 1 # num of colors
    for u,i in next_v:
        # 子孫の中には親もいて、それは除外
        if u == p: continue
        # 子供に対しては自分が親となり、同様のことをする
        if cnt == c:
            cnt += 1
        ans[i] = cnt      
        cnt += 1
        dfs(u, v, ans[i])


dfs(0)
print(max(ans))
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
N = int(input())
edges = [[] for i in range(N)]
e_list = []

for i in range(N-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    e_list.append((a,b))
    edges[a].append((b,i))
    edges[b].append((a,i))

ans = [0] * (N-1)

def dfs(v, p=-1, c=-1): #親p(はじめはいないので-1), 子がv, 親の色も渡す必要あり
    # 子が次の子孫をグラフから探す
    global ans
    next_v = edges[v]
    cnt = 1 # num of colors
    for u,i in next_v:
        # 子孫の中には親もいて、それは除外
        if u == p: continue
        # 子供に対しては自分が親となり、同様のことをする
        if cnt == c:
            cnt += 1
        ans[i] = cnt      
        cnt += 1
        dfs(u, v, ans[i])


dfs(0)
print(max(ans))
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
N = int(input())
edges = [[] for i in range(N)]
e_list = []

for i in range(N-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    e_list.append((a,b))
    edges[a].append((b,i))
    edges[b].append((a,i))

ans = [0] * (N-1)

def dfs(v, p=-1, c=-1): #親p(はじめはいないので-1), 子がv, 親の色も渡す必要あり
    # 子が次の子孫をグラフから探す
    global ans
    next_v = edges[v]
    cnt = 1 # num of colors
    for u,i in next_v:
        # 子孫の中には親もいて、それは除外
        if u == p: continue
        # 子供に対しては自分が親となり、同様のことをする
        if cnt == c:
            cnt += 1
        ans[i] = cnt      
        cnt += 1
        dfs(u, v, ans[i])

dfs(0)
for a in ans:
    print(a)
Problem p02855 (python)

Fixed submission:
H,W,K = map(int, input().split())
fields = [list(input()) for i in range(H)]
bins = []
hs = [] #区切る位置
ws = []
for i,f in enumerate(fields):
    if "#" in f:
        hs.append(i)
hs = hs+[H]
cnt = 0
ans = []
h_fr = 0
for i in range(1,len(hs)):
    h_to = hs[i]
    if h_fr - h_to == 0:continue
    f = fields[h_fr:h_to]
    # print(i,h_fr,h_to,f)
    ws = []
    for j in range(W):
        for i in range(h_fr,h_to):
             if fields[i][j] == "#":
                 ws.append(j)
    ws += [W]
    cnts = []
    w_fr = 0
    for j in range(1,len(ws)):
        w_to = ws[j]
        if w_to-w_fr > 0:
            cnt += 1
            cnts.extend([cnt]*(w_to-w_fr))
            w_fr = w_to
    for i in range(h_to-h_fr):
        ans.append(cnts)
    h_fr = h_to
# print(ans)
for f in ans:
    print(*f)

Buggy submission (verdict: Runtime Error):
H,W,K = map(int, input().split())
cnt = 0
ans = []
z = 1
for i in range(H):
    f = list(input())
    if f.count("#") == 0: 
        z += 1
    elif f.count("#") == 1:
        cnt += 1
        for i in range(z):
            ans.append([cnt]*W)
        z = 1
    else:
        _c = 0
        cnt += 1
        row = []
        for j,s in enumerate(f):
            if s == "#":
                if _c == 0:
                    _c += 1
                else:
                    _c += 1
                    cnt += 1
            row.append(cnt)
        for i in range(z):
            ans.append(row)
        z = 1

for z in range(z-1):
    ans.append(row)

for i in range(H):
    print(*ans[i])

Buggy submission (verdict: Runtime Error):
H,W,K = map(int, input().split())
fields = [list(input()) for i in range(H)]
bins = []
hs = [] #区切る位置
ws = []
for i,f in enumerate(fields):
    if "#" in f:
        hs.append(i)
hs = hs+[H]
ans = [[0]*(W+1) for i in range(H+1)]
cnt = 0
# print(hs)
for i in range(1,len(hs)):
    h_fr = hs[i-1] 
    h_to = hs[i]
    f = fields[h_fr:h_to]
    ws = []
    for j in range(W):
        for i in range(h_fr,h_to):
             if fields[i][j] == "#":
                 ws.append(j)
    ws += [W]
    # print(h_fr,h_to,ws)
    for j in range(1,len(ws)):
        cnt += 1
        w_fr = ws[j-1]
        w_to = ws[j]
        # print(cnt,[[cnt]*(w_to-w_fr) for i in range(h_to-h_fr)] )
        f = [[cnt]*(w_to-w_fr) for i in range(h_to-h_fr)]
        for i in range(h_fr,h_to):
            for j in range(w_fr,w_to):
              ans[i][j] = f[i-h_fr][j-w_to]


for i in range(H):
    prev = ans[i][-1]
    for j in range(W,-1,-1):
        if j == prev: continue
        if ans[i][j] == 0:
            ans[i][j] = prev
        if ans[i][j] != 0:
            prev = ans[i][j]

for i in range(H):
    print(*ans[1:])
Problem p02882 (python)

Fixed submission:
import numpy as np
a,b,V = map(int, input().split())
x = V/(a*a)
if b >= 2*x:
    tan = b**2/(2*a*x)
else:
    tan = 2*(b-x)/a
ans = np.arctan(tan)
print(ans*180/np.pi)

Buggy submission (verdict: Runtime Error):
a,b,V = map(int, input().split())
x = V/(a*a)

def f(theta):
    if np.tan(theta) < (2*x)/a:
        l = x + a*np.tan(theta)/2
    else:
        l = (2*a*x*np.tan(theta))**0.5
    return l >= b


eps = 10**-12
def  binarySearch(l,r):
    while r - l > eps:
        mid = (l+r)/2
        if f(mid):
            r = mid
        else:
            l = mid
    return r

print(binarySearch(-1*np.pi/180,(np.pi/2-eps))*180/np.pi)
Problem p02883 (python)

Fixed submission:
N, K  = map(int, input().split())
As = sorted(list(map(int, input().split())))
Fs = sorted(list(map(int, input().split())),reverse=True)
Xs = [a*f for a,f in zip(As,Fs)]
X_M = max(Xs)

def f(x):
    K_tmp = sum([max(0,a-x//f) for a,f in zip(As,Fs)])
    return K_tmp <= K

def  binarySearch(l,r,target):
    while r - l > 1:
        mid = (l+r)//2
        """計算を入れる"""
        # print(mid, guess)
        if f(mid):
            r = mid
        else:
            l = mid
    return r

print(binarySearch(-1,X_M,K))

Buggy submission (verdict: Runtime Error):
N, K  = map(int, input().split())
As = sorted(list(map(int, input().split())))
Fs = sorted(list(map(int, input().split())),reverse=True)
Xs = sorted([(a*f,i) for i,(a,f) in enumerate(zip(As,Fs))],reverse=True)
# Xs =[a*f for a,f in zip(As,Fs)]
# print(Xs)
# print(As)
# print(Fs)
import heapq
q = []
INF = 10**6
heapq.heapify(q)
for i,(a,f) in enumerate(zip(As,Fs)):
    num = -(a*f*INF+i)
    heapq.heappush(q,num)
if K >= sum(As):
    print(0)
    exit()
for k in range(K):
    num = heapq.heappop(q)
    i = (-num)%INF
    n = (-num)//INF
    print(i,n,As[i],Fs[i])
    As[i] -= 1
    upd = -(As[i]*Fs[i]*INF+i)
    heapq.heappush(q,upd)
num = heapq.heappop(q)
ans = (-num)//INF
print(ans)


x_min = min(Xs)
ns = [max(0,A-x_min//F) for A,F in zip(As,Fs)]
print(ns)
Problem p02903 (python)

Fixed submission:
h,w,a,b = map(int, input().split())
if h == 1:
    S = "1"*a+"0"*(w-a)
    print(S)
elif w == 1:
    S = [1]*b+[0]*(h-b)
    for s in S:
        print(s)
elif h >= 2 and w >= 2:
    # if b*w != a*h:
    #     print("No")
    #     exit()
    # else:
    for i in range(h):
        # print(a,i)
        if i < b:
            S = "1"*a + "0"*(w-a)
        else:
            S = "0"*a + "1"*(w-a)
        print(S)

Buggy submission (verdict: Runtime Error):
h,w,a,b = map(int, input().split())
if h == 1:
    S = "1"*a+"0"*(w-a)
    print(S)
elif w == 1:
    S = [1]*b+[0]*(h-b)
    for s in S:
        print(s)
elif h >= 2 and w >= 2:
    if b*w != a*h:
        print("No")
        exit()
    else:
        for i in range(h):
            S = ["0"]*w
            init = i//b
            for j in range(a*init,a*(init+1)):
                S[j] = "1"
            print("".join(S))
Problem p02921 (python)

Fixed submission:
S = input()
R = input()
cnt = 0
for s,r in zip(S,R):
    if s==r:
        cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):

cnt = 0
for i in range(3):
    if S[i] == T[i]:
        cnt += 1
print(cnt)
Problem p02936 (python)

Fixed submission:

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

# 木
G = [[] for i in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    G[u-1].append((v-1))
    G[v-1].append((u-1))

## クエリを覚えておく
scores = [0]*N
for i in range(Q):
    p,x = map(int, input().split())
    scores[p-1] += x 

# 和をとっていく
ans = [0]*N
ans[0] = scores[0]
used = [False] * N

"""
dfs TLEする。なんで
"""
# import sys
# sys.setrecursionlimit(100000)
# def dfs(G, v):
#    # vを訪問済みにする
#    used[v] = True
#    #グラフ中の次のVについて
#    for next_v in G[v]:
#        if used[next_v]: continue
#        ans[next_v] = ans[v] + scores[next_v]
#        dfs(G, next_v)
# dfs(G,0)
# print(*ans)

# 木をBFSをする
from collections import deque
used = [False] * N
used[0] = True # 始めどこから行くか
q = deque([0])
while len(q) > 0:
    v = q.popleft()
    Vs = G[v]
    for next_v in Vs: 
        if not used[next_v]:
            q.append(next_v)
            ans[next_v] = ans[v] + scores[next_v]
            used[next_v] = True
print(*ans)

Buggy submission (verdict: Runtime Error):

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

G = [[] for i in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    G[u-1].append((v-1))
    G[v-1].append((u-1))

## クエリを覚えておく
scores = [0]*N
for i in range(Q):
    p,x = map(int, input().split())
    scores[p-1] += x 

ans = [0]*N
ans[0] = scores[0]

used = [False] * N
def dfs(G, v):
   # vを訪問済みにする
   used[v] = True
   #グラフ中の次のVについて
   for next_v in G[v]:
       # すでに見られていたら無視
       if used[next_v]: continue
       # 見られていない場合、再帰的に探索
       # 親のスコアも足していく
       ans[next_v] = ans[v] + scores[next_v]
       dfs(G, next_v)

dfs(G,0)
print(*ans)
Problem p02937 (python)

Fixed submission:
from bisect import bisect_right
from collections import defaultdict

S = input()
S_set = set(S)
d = defaultdict(list)
for i,s in enumerate(S):
    d[s].append(i)
# print(d)

T = input()
T_set = set(T)
for t in T_set:
    if t not in S_set:
        print(-1)
        exit()

cnt = 0
loop = 0
prev = -1

for t in T:
    num = d[t]
    a = bisect_right(num,prev)
    # print(prev,a,num)
    
    if a >= len(num):
        loop += 1
        a = 0
    prev = num[a]

print(prev+1+(loop)*len(S))

Buggy submission (verdict: Runtime Error):

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

# 木
G = [[] for i in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    G[u-1].append((v-1))
    G[v-1].append((u-1))

## クエリを覚えておく
scores = [0]*N
for i in range(Q):
    p,x = map(int, input().split())
    scores[p-1] += x 

# 和をとっていく
ans = [0]*N
ans[0] = scores[0]
used = [False] * N

"""
dfs TLEする。なんで
"""
# import sys
# sys.setrecursionlimit(100000)
# def dfs(G, v):
#    # vを訪問済みにする
#    used[v] = True
#    #グラフ中の次のVについて
#    for next_v in G[v]:
#        if used[next_v]: continue
#        ans[next_v] = ans[v] + scores[next_v]
#        dfs(G, next_v)
# dfs(G,0)
# print(*ans)

# 木をBFSをする
from collections import deque
used = [False] * N
used[0] = True # 始めどこから行くか
q = deque([0])
while len(q) > 0:
    v = q.popleft()
    Vs = G[v]
    for next_v in Vs: 
        if not used[next_v]:
            q.append(next_v)
            ans[next_v] = ans[v] + scores[next_v]
            used[next_v] = True
print(*ans)
Problem p02941 (python)

Fixed submission:
import heapq
q = []
heapq.heapify(q)

n = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

zeros = [False]*n
Cs = [Bs[(i-1)%n]+Bs[(i+1)%n] for i in range(n)]
# Dが大きい順に操作していく
for i, (b,c,a) in enumerate(zip(Bs,Cs,As)):
    d = b-(c+a) 
    if d < 0: continue
    heapq.heappush(q,(-d,i))

cnt = 0
while len(q) > 0:
    d,i = heapq.heappop(q)
    # 自分と周りの影響を更新
    cnt_ = (Bs[i%n]-As[i%n]) // Cs[i%n]
    Bs[i%n] -= cnt_*Cs[i%n]    
    Cs[(i-1)%n] = Bs[(i-2)%n]+Bs[i%n]
    Cs[(i+1)%n] = Bs[(i+2)%n]+Bs[i%n]
    # 周りとの差
    d_m = Bs[(i-1)%n]-Cs[(i-1)%n]-As[(i-1)%n] 
    d_p = Bs[(i+1)%n]-Cs[(i+1)%n]-As[(i+1)%n]
    if d_m >= 0:
        heapq.heappush(q,(-d_m,i-1))
    if d_p >= 0:
        heapq.heappush(q,(-d_p,i+1))
    cnt += cnt_

if As == Bs:
    print(cnt)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
import heapq
q = []
heapq.heapify(q)

n = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

zeros = [False]*n
Cs = [Bs[(i-1)%n]+Bs[(i+1)%n] for i in range(n)]
Ds = [b-(c+a) for b,c,a in zip(Bs,Cs,As)]
# Dが大きい順に操作していく？
# 初期値
for i,d in enumerate(Ds):
    if d < 0: continue
    heapq.heappush(q,(-d,i))

# print("init",Bs,Cs)
cnt = 0
while len(q) > 0:
    d,i = heapq.heappop(q)
    # 自分と周りの影響を更新
    # print(Bs,Cs,i,"before")
    cnt_ = Bs[i%n] // Cs[i%n]
    Bs[i%n] %= Cs[i%n]    
    Cs[(i-1)%n] = Bs[(i-2)%n]+Bs[i%n]
    Cs[(i+1)%n] = Bs[(i+2)%n]+Bs[i%n]
    # print("after",Bs,Cs,i)
    # 周りとの差
    d_m = Bs[(i-1)%n]-Cs[(i-1)%n]-As[(i-1)%n] 
    d_p = Bs[(i+1)%n]-Cs[(i+1)%n]-As[(i+1)%n]
    d_ = Bs[i%n] - Cs[i%n] - As[i%n]
    # print("pop",d,i,d_m,d_p)
    if d_ >= 0:
        heapq.heappush(q,(-d_,i))
    if d_m >= 0:
        heapq.heappush(q,(-d_m,i-1))
    if d_p >= 0:
        heapq.heappush(q,(-d_p,i+1))        
    # print("debug",Bs,Cs)
    cnt += cnt_
if As == Bs:
    print(cnt)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
import heapq
q = []
heapq.heapify(q)

n = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

zeros = [False]*n
Cs = [Bs[(i-1)%n]+Bs[(i+1)%n] for i in range(n)]
Ds = [b-(c+a) for b,c,a in zip(Bs,Cs,As)]
# Dが大きい順に操作していく？
# 初期値
for i,d in enumerate(Ds):
    if d < 0: continue
    heapq.heappush(q,(-d,i))

# print("init",Bs,Cs)
cnt = 0
while len(q) > 0:
    d,i = heapq.heappop(q)
    # 自分と周りの影響を更新
    b_ = Bs[i%n] 
    c_ = Cs[i%n]
    cnt_ =  b_ // c_
    Bs[i%n] -= cnt_ * Cs[i%n]
    Cs[(i-1)%n] = Bs[(i-2)%n]+Bs[i%n]
    Cs[(i+1)%n] = Bs[(i+2)%n]+Bs[i%n]
    # 周りとの差
    d_m = Bs[(i-1)%n]-Cs[(i-1)%n]-As[(i-1)%n] 
    d_p = Bs[(i+1)%n]-Cs[(i+1)%n]-As[(i+1)%n]
    d_ = Bs[i%n] - Cs[i%n] - As[i%n]
    # print("pop",d,i,d_m,d_p)
    if d_ >= 0:
        heapq.heappush(q,(-d_,i))
    if d_m >= 0:
        heapq.heappush(q,(-d_m,i-1))
    if d_p >= 0:
        heapq.heappush(q,(-d_p,i+1))        
    # print("debug",Bs,Cs)
    cnt += cnt_
if As == Bs:
    print(cnt)
else:
    print(-1)