Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

n, t = MAP()
a = LIST()
get = 0
ans = 0
b = [a[0]]
for i in range(1, n - 1):
    if a[i - 1] < a[i] < a[i + 1]:
        continue
    b.append(a[i])
b.append(a[-1])
d = [b[i] - min(b[:i]) for i in range(1, len(b))]
mx = max(d)
print(d.count(mx))


Fixed submission:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

n, t = MAP()
a = LIST()
get = 0
ans = 0
b = [a[0]]
for i in range(1, n - 1):
    if a[i - 1] < a[i] < a[i + 1]:
        continue
    b.append(a[i])
b.append(a[-1])
d = [0 for i in range(len(b) - 1)]
mn = inf
for i in range(len(b) - 1):
    if mn > b[i]:
        mn = b[i]
    d[i] = b[i + 1] - mn
mx = max(d)
print(d.count(mx))
Buggy python submission (verdict: Time Limit Exceeded):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

n = INT()
ans = [0, 0]
for i in range(n):
    t,a = MAP()
    k = 1
    while True:
        if ans[0] <= k * t and ans[1] <= k * a:
            break
        k += 1
    ans[0] = k * t
    ans[1] = k * a
    #print('a', t+a, ans)
print(sum(ans))


Fixed submission:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

n = INT()
ans = [1, 1]
for i in range(n):
    t,a = MAP()
    k = max((ans[0] + t - 1) // t, (ans[1] + a - 1) // a)
    ans[0] = k * t
    ans[1] = k * a
    #print('a', t+a, ans)
print(sum(ans))
Buggy python submission (verdict: Runtime Error):
import sympy

n = int(input())

tmp = n - 1
ret = len(sympy.divisors(tmp)) - 1

tmplen = sympy.divisors(n)

for item in tmplen:
    if item == 1:
        continue
    val = n
    while True:
        if val % item == 0:
            val = val // item
        else:
            if val % item == 1:
                ret += 1
            break

print(ret)


Fixed submission:
def make_divisors(n):
    lower_divisors, upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]


n = int(input())

tmp = n - 1
ret = len(make_divisors(tmp)) - 1

tmplen = make_divisors(n)

for item in tmplen:
    if item == 1:
        continue
    val = n
    while True:
        if val % item == 0:
            val = val // item
        else:
            if val % item == 1:
                ret += 1
            break

print(ret)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
my_dict = dict()
n = int(input())
for i in range(n):
    my_input = input()
    if my_input in my_dict:
        my_dict[my_input] = my_dict[my_input] + 1
    else:
        my_dict[my_input] = 1

max_k_list = [kv[0]
              for kv in my_dict.items() if kv[1] == max(my_dict.values())]

max_k_list.sort()

print("\n".join(max_k_list))


Fixed submission:
# -*- coding: utf-8 -*-
my_dict = dict()
n = int(input())
for i in range(n):
    my_input = input()
    if my_input in my_dict:
        my_dict[my_input] += 1
    else:
        my_dict[my_input] = 1

max_val = max(my_dict.values())
keys_of_max_val = [key for key in my_dict if my_dict[key] == max_val]

keys_of_max_val.sort()

print("\n".join(keys_of_max_val))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
my_dict = dict()
n = int(input())
for i in range(n):
    my_input = input()
    if my_input in my_dict:
        my_dict[my_input] = my_dict[my_input] + 1
    else:
        my_dict[my_input] = 1

max_k_list = [kv[0]
              for kv in my_dict.items() if kv[1] == max(my_dict.values())]

max_k_list.sort()

print("\n".join(max_k_list))


Fixed submission:
# -*- coding: utf-8 -*-
my_dict = dict()
n = int(input())
for i in range(n):
    my_input = input()
    if my_input in my_dict:
        my_dict[my_input] += 1
    else:
        my_dict[my_input] = 1

max_val = max(my_dict.values())
keys_of_max_val = [key for key in my_dict if my_dict[key] == max_val]

keys_of_max_val.sort()

print("\n".join(keys_of_max_val))
Buggy python submission (verdict: Runtime Error):
n = input()

dp = [[0] * 2 for _ in range(100000)]

digit = int(n[-1])

dp[0][0] = 10 - digit
dp[0][1] = digit

for i in range(1, len(n) + 1):
    if i == len(n):
        digit = 0
    else:
        digit = int(n[-i-1])
    dp[i][0] = min(dp[i-1][0] + 10 - digit - 1, dp[i-1][1] + 10 - digit)
    dp[i][1] = min(dp[i-1][0] + digit + 1, dp[i-1][1] + digit)


print(min(dp[len(n)][1], dp[len(n)-1][1]))


Fixed submission:
n = input()

dp = [[0] * 2 for _ in range(2)]

digit = int(n[-1])

dp[0][0] = 10 - digit
dp[0][1] = digit

for i in range(1, len(n) + 1):
    if i == len(n):
        digit = 0
    else:
        digit = int(n[-i-1])

    if i % 2 == 0:
        dp[0][0] = min(dp[1][0] + 10 - digit - 1, dp[1][1] + 10 - digit)
        dp[0][1] = min(dp[1][0] + digit + 1, dp[1][1] + digit)
    else:
        dp[1][0] = min(dp[0][0] + 10 - digit - 1, dp[0][1] + 10 - digit)
        dp[1][1] = min(dp[0][0] + digit + 1, dp[0][1] + digit)


print(min(dp[0][1], dp[1][1]))

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

dp = [[0] * 2 for _ in range(101)]

digit = int(n[-1])

dp[0][0] = 10 - digit
dp[0][1] = digit

for i in range(1, len(n) + 1):
    if i == len(n):
        digit = 0
    else:
        digit = int(n[-i-1])
    dp[i][0] = min(dp[i-1][0] + 10 - digit - 1, dp[i-1][1] + 10 - digit)
    dp[i][1] = min(dp[i-1][0] + digit + 1, dp[i-1][1] + digit)


print(min(dp[len(n)][1], dp[len(n)-1][1]))


Fixed submission:
n = input()

dp = [[0] * 2 for _ in range(2)]

digit = int(n[-1])

dp[0][0] = 10 - digit
dp[0][1] = digit

for i in range(1, len(n) + 1):
    if i == len(n):
        digit = 0
    else:
        digit = int(n[-i-1])

    if i % 2 == 0:
        dp[0][0] = min(dp[1][0] + 10 - digit - 1, dp[1][1] + 10 - digit)
        dp[0][1] = min(dp[1][0] + digit + 1, dp[1][1] + digit)
    else:
        dp[1][0] = min(dp[0][0] + 10 - digit - 1, dp[0][1] + 10 - digit)
        dp[1][1] = min(dp[0][0] + digit + 1, dp[0][1] + digit)


print(min(dp[0][1], dp[1][1]))
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def calc_expect(n):
    return (n+1) / 2


n, k = map(int, input().split())
p_list = list(map(int, input().split()))

p_expect_list = list(map(calc_expect, p_list))

max_val = 0

for i in range(len(p_expect_list)):
    val = sum(p_expect_list[i:i+k])
    if val > max_val:
        max_val = val

print(max_val)


Fixed submission:
# -*- coding: utf-8 -*-
def calc_expect(n):
    return (n+1) / 2


n, k = map(int, input().split())
p_list = list(map(int, input().split()))

p_expect_list = list(map(calc_expect, p_list))

max_val = 0

res = [0] * len(p_expect_list)
res[0] = p_expect_list[0]
for i in range(len(res)-1):
    res[i+1] = res[i] + p_expect_list[i+1]

if (n == k):
    print(res[-1])
    exit()

for i in range(len(p_expect_list)-k):
    val = res[i+k]-res[i]
    if val > max_val:
        max_val = val

print(max_val)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def calc_expect(n):
    return (n+1) / 2


n, k = map(int, input().split())
p_list = list(map(int, input().split()))

p_expect_list = list(map(calc_expect, p_list))

max_val = 0

for i in range(len(p_expect_list)):
    val = sum(p_expect_list[i:i+k])
    if val > max_val:
        max_val = val

print(max_val)


Fixed submission:
# -*- coding: utf-8 -*-
def calc_expect(n):
    return (n+1) / 2


n, k = map(int, input().split())
p_list = list(map(int, input().split()))

p_expect_list = list(map(calc_expect, p_list))

max_val = 0

res = [0] * len(p_expect_list)
res[0] = p_expect_list[0]
for i in range(len(res)-1):
    res[i+1] = res[i] + p_expect_list[i+1]

if (n == k):
    print(res[-1])
    exit()

for i in range(len(p_expect_list)-k):
    val = res[i+k]-res[i]
    if val > max_val:
        max_val = val

print(max_val)
Buggy python submission (verdict: Time Limit Exceeded):
h, n = map(int, input().split())

ab_list = [0]*n

for i in range(n):
    ab_list[i] = list(map(int, input().split()))

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])


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

ab_list = [0]*n

for i in range(n):
    ab_list[i] = list(map(int, input().split()))

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])

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

ab_list = []

for i in range(n):
    ab_list.append(list(map(int, input().split())))

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])


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

ab_list = [0]*n

for i in range(n):
    ab_list[i] = list(map(int, input().split()))

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])
Buggy python submission (verdict: Runtime Error):
s, t = input.split()
print(t+s)


Fixed submission:
s, t = input().split()
print(t+s)
Buggy python submission (verdict: Time Limit Exceeded):
def check(bag, mtx):
    shojiki = set()
    not_shojiki = set()
    for i in range(len(bag)):
        if bag[i] == 1:
            shojiki.add(i)
    shojiki_list = list(shojiki)
 
    while True:
        if shojiki_list == []:
            break
        i = shojiki_list.pop()
        for j in range(len(mtx[i])):
            if mtx[i][j] == 0 and j in shojiki:
                return False
            if mtx[i][j] == 1:
                if j in not_shojiki:
                    return False
                if j not in shojiki:
                    shojiki_list.append(j)
                    shojiki.add(j)
            if mtx[i][j] == 0:
                not_shojiki.add(j)
    return True
 
 
n = int(input())
 
mtx = [[-1] * n for i in range(n)]
 
for i in range(n):
    a = int(input())
    for j in range(a):
        x, y = map(int, input().split())
        mtx[i][x-1] = y
 
ret = 0
 
for i in range(2 ** n):
    bag = []
    for j in range(n):
        if ((i >> j) & 1):
            bag.append(1)
        else:
            bag.append(0)
    if check(bag, mtx):
        if ret < sum(bag):
            ret = sum(bag)
 
print(ret)


Fixed submission:
def check(bag, mtx):
    shojiki = set()
    not_shojiki = set()
    for i in range(len(bag)):
        if bag[i] == 1:
            shojiki.add(i)
    shojiki_list = list(shojiki)
 
    while True:
        if shojiki_list == []:
            break
        i = shojiki_list.pop()
        for j in range(len(mtx[i])):
            if mtx[i][j] == 0 and j in shojiki:
                return False
            if mtx[i][j] == 1:
                if j in not_shojiki:
                    return False
                if j not in shojiki:
                    shojiki_list.append(j)
                    shojiki.add(j)
            if mtx[i][j] == 0:
                not_shojiki.add(j)
    return True
 
 
n = int(input())
 
mtx = [[-1] * n for i in range(n)]
 
for i in range(n):
    a = int(input())
    for j in range(a):
        x, y = map(int, input().split())
        mtx[i][x-1] = y
 
ret = 0
 
for i in range(2 ** n):
    bag = []
    for j in range(n):
        if ((i >> j) & 1):
            bag.append(1)
        else:
            bag.append(0)
    if check(bag, mtx):
        if ret < sum(bag):
            ret = sum(bag)
 
print(ret)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from collections import defaultdict
from heapq import heappop, heappush


class Graph(object):
    """
    隣接リストによる有向グラフ
    """

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    """
    ダイクストラ法（二分ヒープ）による最短経路探索
    計算量: O((E+V)logV)
    """

    def __init__(self, graph, start):
        self.g = graph.graph

        # startノードからの最短距離
        # startノードは0, それ以外は無限大で初期化
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        # 最短経路での1つ前のノード
        self.prev = defaultdict(lambda: None)

        # startノードをキューに入れる
        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            # 優先度（距離）が最小であるキューを取り出す
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist[goal]

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]


def query(src, dst, d_list, d_dict, l):
    count = 0
    now_l = l
    d = d_list[src-1]
    distance = d.shortest_distance(dst-1)
    if distance <= l:
        print(0)
        return

    path = d.shortest_path(dst-1)

    if(len(path) == 1):
        print(-1)
        return

    now_s = path[0]

    for item in path[1:]:
        length = d_dict[str(now_s)+":"+str(item)]
        if length > l:
            print(-1)
            return
        elif length > now_l:
            count += 1
            now_l = l - length
        else:
            now_l = now_l - length
        now_s = item
    print(count)


n, m, l = map(int, input().split())
inputs = []
d_dict = dict()

for i in range(m):
    a, b, c = map(int, input().split())
    inputs.append((a-1, b-1, c))
    d_dict[str(a-1)+":"+str(b-1)] = c
    d_dict[str(b-1)+":"+str(a-1)] = c

g = Graph()
for src, dst, weight in inputs:
    g.add_edge(src, dst, weight)
    g.add_edge(dst, src, weight)

d_list = []

for i in range(n):
    d_list.append(Dijkstra(g, i))

q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    query(s, t, d_list, d_dict, l)


Fixed submission:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
a, b = map(int, input().split())

print(max(0, a - 2 * b))
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def c_div(a, b):
    my_list = []
    for i in range(2, min(a, b)+1):
        flag = True
        for item in my_list:
            if i % item == 0:
                flag = False
                break
        if flag and a % i == 0 and b % i == 0:
            my_list.append(i)
    return len(my_list)+1


# スペース区切りの整数の入力
a, b = map(int, input().split())
print(c_div(a, b))


Fixed submission:
# -*- coding: utf-8 -*-
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


# スペース区切りの整数の入力
a, b = map(int, input().split())
a_list = prime_factorize(a)
a_set = set(a_list)
b_list = prime_factorize(b)
b_set = set(b_list)

print(len(a_set & b_set)+1)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-                                                         
tmp = input()
my_str = input()

start = 0
end = 1
max_len = 0

while (end < len(my_str)):
    if my_str[start:end] in my_str[end:]:
        if end - start > max_len:
            max_len = end - start
     	end += 1
    else:
        start += 1

print(max_len)


Fixed submission:
# -*- coding: utf-8 -*-
tmp = input()
my_str = input()

start = 0
end = 1
max_len = 0

while (end < len(my_str)):
    if my_str[start:end] in my_str[end:]:
        if end - start > max_len:
            max_len = end - start
        end += 1
    else:
        start += 1

print(max_len)
Buggy python submission (verdict: Runtime Error):
n = input()
h_string = input()
h_list = h_string.split(" ")
for item in h_list:
    item = int(item)


longest = 0
now_value = -1
now_length = 0


for value in h_list:
    if value <= now_value:
        now_length += 1
        now_value = value
        if now_length > longest:
            longest = now_length
    else:
        if now_length > longest:
            longest = now_length
        now_value = value
        now_length = 0

print(longest)


Fixed submission:
n = input()
h_string = input()
h_list = h_string.split(" ")
for i in range(len(h_list)):
    h_list[i] =	int(h_list[i])


longest = 0
now_value = -1
now_length = 0


for value in h_list:
    if value <= now_value:
        now_length += 1
        now_value = value
        if now_length > longest:
            longest = now_length
    else:
        if now_length > longest:
            longest = now_length
        now_value = value
        now_length = 0

print(longest)
Buggy python submission (verdict: Time Limit Exceeded):
n = input()
n = int(n)

ret = 0

for i in range(n-1):
    ret += (i+1)

print(ret)


Fixed submission:
n = input()
n = int(n)

max_value = n - 1

ret = max_value * (max_value + 1) // 2

print(ret)
Buggy python submission (verdict: Runtime Error):
n, q = map(int, input().split())
my_dict = dict()

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

stk = []

my_p = dict()

for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    if p in my_p:
        my_p[p] += x
    else:
        my_p[p] = x

my_ret = dict()

stk = []
stk.append((0, my_p[0]))

visited = set()

while stk:
    ptr, pnt = stk.pop()
    visited.add(ptr)
    my_ret[ptr] = pnt
    for item in my_dict[ptr]:
        if item in visited:
            continue
        tmp = 0
        if item in my_p:
            tmp = my_p[item]
        stk.append((item, pnt + tmp))

ret = []

for i in range(n):
    ret.append(str(my_ret[i]))

print(' '.join(ret))


Fixed submission:
from collections import deque

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

deq = deque()
pnt = [0] * n

for _ in range(q):
    p, x = map(int, input().split())
    pnt[p-1] += x

visited = set()

deq.append(0)
visited.add(0)

while deq:
    ptr = deq.pop()
    for item in rg[ptr]:
        if item not in visited:
            visited.add(item)
            pnt[item] += pnt[ptr]
            deq.append(item)

print(' '.join(map(str, pnt)))

Buggy python submission (verdict: Runtime Error):
n, q = map(int, input().split())
my_dict = dict()

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

stk = []

my_p = dict()

for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    if p in my_p:
        my_p[p] += x
    else:
        my_p[p] = x

my_ret = dict()

stk = []
stk.append((0, my_p[0]))

visited = set()

while stk:
    ptr, pnt = stk.pop()
    visited.add(ptr)
    my_ret[ptr] = pnt
    for item in my_dict[ptr]:
        if item in visited:
            continue
        tmp = 0
        if item in my_p:
            tmp = my_p[item]
        stk.append((item, pnt + tmp))

ret = []

for i in range(n):
    ret.append(str(my_ret[i]))

print(' '.join(ret))


Fixed submission:
from collections import deque

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

deq = deque()
pnt = [0] * n

for _ in range(q):
    p, x = map(int, input().split())
    pnt[p-1] += x

visited = set()

deq.append(0)
visited.add(0)

while deq:
    ptr = deq.pop()
    for item in rg[ptr]:
        if item not in visited:
            visited.add(item)
            pnt[item] += pnt[ptr]
            deq.append(item)

print(' '.join(map(str, pnt)))