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

Fixed submission:
s = input()
t = input()
a = 0
if s[0] == t[0]: a = a + 1
if s[1] == t[1]: a = a + 1
if s[2] == t[2]: a = a + 1
print(a)

Buggy submission (verdict: Runtime Error):
s = int(input())
t = int(input())
a = 0
if s[0] == t[0]: a = a + 1
if s[1] == t[1]: a = a + 1
if s[2] == t[2]: a = a + 1
print(a)
Problem p02947 (python)

Fixed submission:
n = int(input())

s = []
for i in range(n):
    s.append("".join(sorted(input())))
s.sort()

a = 0
r = 0
i = 1
l = len(s)
while i < l:
    if s[i] == s[i-1]:
        r = r+1
    else:
        a = a + sum(range(r+1))
        r = 0
    i = i+1

if r > 0: a = a + sum(range(r+1))
print(a)

Buggy submission (verdict: Runtime Error):
for i in range(n):
    s.append("".join(sorted(input())))
s.sort()

a = 0
r = 0
i = 1
l = len(s)
while i < l:
    if s[i] == s[i-1]:
        r = r+1
    else:
        a = a + sum(range(r+1))
        r = 0
    i = i+1

if r > 0: a = a + sum(range(r+1))
print(a)
Problem p02923 (python)

Fixed submission:
N = int(input())
H = list(map(int, input().split()))
c = 0
m = 0
for i in range(N-1):
  if H[i] >= H[i+1]:
    c +=1
  else:
    m = c if c>m else m
    c = 0
m = c if c>m else m
print(m)

Buggy submission (verdict: Runtime Error):
N = int(input())
H = list(map(int, input().split()))
C = [ 0 for _ in range(N)]
r = 0
for i in range(N-1):
  if H[i]  H[i+1]:
    if i ==0:
      C[i] = 1
      continue
    C[i] = C[i-1] + 1
    if C[r] >=C[i]:
      r = i
print(r)
Problem p02927 (python)

Fixed submission:
 # -*- coding: utf-8 -*-
# スペース区切りの整数の入力
M, D = map(int, input().split())
result =0
for i in range(4,M+1):
    for j in range(22, D+1):
        d1 =(j % 10)
        d2 =(j // 10)
        if d1 <2 or d2<2 or (d1*d2!=i):
            continue
        result += 1
print(result)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
As = list(map(int, input().split())) *K

count_base =0
for i in range(N*K):
    for j in range(i, N*K):
        if As[i] > As[j]:
            count_base += 1
print(int(count_base % (pow(10, 9) + 7)))
Problem p02952 (python)

Fixed submission:
a = int(input())
if a <10:
  print("{}".format(a))
elif a <100:
  print("9")
elif a < 1000:
  print("{}".format(9+a-99))
elif a < 10000:
  print('909')
elif a < 100000:
  print("{}".format(909+a-9999))
else:
  print('90909')

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
a = int(input())
if a <=9:
  print("{}".format(a));
elif a <=99:
  print("9")
elif a < 999:
  print("{}".format(9+a-99))
elif a <= 9999:
  print('909')
else
  print("{}".format(909+a-1000))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
a = int(input())
if a <=9:
  print("{}".format(a));
  return
if a <=99:
  print('9')
  return;
if a < 999:
  print("{}".format(9+a-99))
  return
if a <= 9999:
  print('909')
  return
print("{}".format(909+a-9999))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
l = int(input())
if l <=9:
  print("{}".format(a));
  return
if l <=99:
  print('9')
  return;
if l < 999:
  print("{}".format(9+a-99))
  return
if l <= 9999:
  print('909')
  return
print("{}".format(909+a-9999))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
a = int(input())
if l <=9:
  print("{}".format(a));
  return
if l <=99:
  print('9')
  return;
if l < 999:
  print("{}".format(9+a-99))
  return
if l <= 9999:
  print('909')
  return
print("{}".format(909+a-9999))
Problem p02953 (python)

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

prev = H[0]
r = True
for i in range(1,N):
  if prev < H[i]:
    H[i] -= 1
    prev = H[i]
    continue
  if prev == H[i]:
    continue
  r = False
  break

if r:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
N = int(input())
H = list(map(int, input().split()))

prev = H[0]
for i in range(1,N):
  if prev < H[i]:
    H[i] -= 1
    prev = H[i]
    continue
  if prev == H[i]:
    continue
  print('No')
  return
print('Yes')

Buggy submission (verdict: Runtime Error):
N = int(input())
H = list(map(int, input().split()))

prev = H[0]
for i in range(1,N):
  if prev < H[i]:
    H[i] -= 1
    prev = H[i]
    continue
  if prev == H[i]
    continue
  print('No')
  return
print('Yes')
Problem p03029 (python)

Fixed submission:
a,b =input().split()
print((int(a)*3+int(b))//2)

Buggy submission (verdict: Runtime Error):
a , b = input().split()
print((a*3+b)//2)

Buggy submission (verdict: Runtime Error):
a, b = input()
print((a*3+b)//2)
Problem p02922 (python)

Fixed submission:
import math

a, b =map(int, input().split())

if b!=1:
  num = math.ceil((b-1)/(a-1))
else:
  num = 0

print(num)

Buggy submission (verdict: Runtime Error):
import math

a, b =map(int, input().split())

if b!=1:
  num = math.ceil(((b-1)/(a-1))
else:
  num = b

print(num)
Problem p02969 (python)

Fixed submission:
r = int(input())
print(3 * r * r)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
ans = []

for i in range(n):
	j = n - 1 - i
	k = (j + 1) + (j + 1)
	if k > n:
		if a[j] == 1:
			ans.append(j+1)
	else:
		count = 0
		while k <= n:
			if k in ans:
				count = count + 1
			k = k + (j + 1)
		count_01 = count % 2
		if a[j] == 0:
			if count_01 == 1:
				ans.append(j+1)
		elif a[j] == 1:
			if count_01 == 0:
				ans.append(j+1)
if not ans:
	print(0)
else:
	print(str(len(ans))+"\n" + " ".join(list(map(str,ans))))
Problem p02983 (python)

Fixed submission:
l, r = list(map(int,input().split()))
lmod = l % 2019
rmod = r % 2019
if(r-l) >= 2018:
	answer = 0
elif(lmod > rmod):
	answer = 0
else:
	answer = (rmod * lmod)% 2019
	for i in range(rmod - lmod + 1):
		for j in range(rmod - lmod + 1):
			if(i < j):
				if(answer > (lmod + i) * (lmod + j) % 2019):
					answer = (lmod + i) * (lmod + j) % 2019
print(answer)

Buggy submission (verdict: Runtime Error):
l, r = list(map(int,input().split()))
Set = set([l%2019])
for i in range(r-l):
	Set.add((l+1+i)%2019)
List = list(Set)
answer = (l * r) % 2019
k = 0
for i in range(len(List)):
	for j in range(len(List)):
		if(i < j):
			if(k == 0 or (i >= 45 and j >= 46))
				if(answer > (List[i] * List[j])% 2019):
					answer = (List[i] * List[j])% 2019
					k = 1
print(answer)
Problem p03029 (python)

Fixed submission:

a, p = map(int, input().split())
num = (a*3 + p) // 2
print(num)

Buggy submission (verdict: Runtime Error):
a, p = map(int, input().split())
num = a*3 // p
print(num)
Problem p03068 (python)

Fixed submission:
n = int(input())
s = input()
k = int(input())
ans = ''
for i in range(n):
    if s[i] == s[k-1]:
        ans += s[k-1]
    else:
        ans += '*'
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
k = int(input())
ans = ''
for i in range(n):
    if s[i] == s[k]:
        ans += s[k]
    else:
        ans += '*'
print(ans)
Problem p03071 (python)

Fixed submission:

a, b = map(int, input().split())
 
if a > b:
    print(a+a-1)
elif a < b:
    print(b+b-1)
else:
    print(a+b)

Buggy submission (verdict: Runtime Error):

a, b = map(int, input().split())
 
if a > b:
    print(a+a-1)
elif a < b:
    print(b+b-1)
else
    print(a+b)

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())
 
if a > b:
  print(a+a-1)
elif a < b:
  print(b+b-1)
else
  print(a+b)

Buggy submission (verdict: Runtime Error):
a,b = map(int, input())
if a > b:
  print(a*2-1)
else:
  print(b*2-1)

Buggy submission (verdict: Runtime Error):
a,b = map(int, input())
print(max(a,b)*2-1)
Problem p03061 (python)

Fixed submission:
from collections import deque

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

n = int(input())
a = [int(i) for i in input().split()]

left_gcd = deque()
left_gcd.append(0)
right_gcd = deque()
right_gcd.append(0)

for i in range(n):
    left_gcd.append(gcd(left_gcd[-1], a[i]))
    right_gcd.appendleft(gcd(right_gcd[0], a[(n - 1) - i]))

max_gcd = 0

for i in range(n):
    get_gcd = gcd(left_gcd[i], right_gcd[i + 1])
    if get_gcd > max_gcd:
        max_gcd = get_gcd

print(max_gcd)

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
a = [int(i) for i in input().split()]

left_gcd = [0]
right_gcd = [0]

for i in range(n):
    left_gcd.append(math.gcd(left_gcd[-1], a[i]))
    right_gcd.insert(0, math.gcd(right_gcd[0], a[(n - 1) - i]))

max_gcd = 0

for i in range(n):
    gcd = math.gcd(left_gcd[i], right_gcd[i + 1])
    if gcd > max_gcd:
        max_gcd = gcd

print(max_gcd)
Problem p02922 (python)

Fixed submission:
lst = [int(x) for x in input().split()]
A = lst[0]
B = lst[1]

if B >= 2:
    m = 0
    while (A-1) * m + 1 < B:
        m += 1
    print(m)

if B == 1:
    print(0)

Buggy submission (verdict: Runtime Error):
S = input()
List = [input() for i in range(1)]
T = List[0]

Slst = list(S)
Tlst = list(T)
M = 0

for x in range(3):
    if Slst[x] == Tlst[x]:
        M += 1
print(M)
Problem p02999 (python)

Fixed submission:
x, a=map(int, input().split())
if	x < a:
	l = 0
else:
	l = 10
print(l)

Buggy submission (verdict: Runtime Error):
n,x=map(int,input().split())
li=list(int,map(input().split()))
d=0
c=1
for l in li:
  	d=d+l
	if d>x:
		break
	
	c=c+1
print(c)

Buggy submission (verdict: Runtime Error):
n,x=map(int,input().split())
li=list(map(input().split()))
d=0
c=1
for l in li:
  	d=d+l
	if d>x:
		break
	
	c=c+1
print(c)

Buggy submission (verdict: Runtime Error):
n,x=map(int,input().split())
li=map(input().split())
d=0
c=1
for l in li:
  	d=d+l
	if d>x:
		break
	
	c=c+1
	
print(c)

Buggy submission (verdict: Runtime Error):
n,x=map(int,input().split())
li=input().split()
d=0
c=0
for l in li:
	if d>x:
		break
	d=d+l
	c=c+1
	
print(c)

Buggy submission (verdict: Runtime Error):
x, a=map(int, input().split())
if	x < a
	l = 0
else
	l = 10
print(l)

Buggy submission (verdict: Runtime Error):
x, a=map(int, input().split())
if	x < a
	l = 0
else
	l = 10
print (l)

Buggy submission (verdict: Runtime Error):
x, a=map(int, input().split())
if	x < a
	l = 0
if x >= a
	l = 10
print (l)

Buggy submission (verdict: Runtime Error):
x,a=map(int,input().split())
if	x < a
	l = 0
if x >= a
	l = 10
print (l)

Buggy submission (verdict: Runtime Error):
x,a=map(int,input()split())
if	x<a
	l=0
if x>=a
	l=10
print (l)
Problem p02923 (python)

Fixed submission:
N = int(input())
H = [int(i) for i in input().split()]
m_max = 0
count = 0
for j in range(1, N):
  if H[j] <= H[j-1]:
    count += 1
    if j == N-1:
      if count > m_max:
        m_max = count
  else:
    if count > m_max:
      m_max = count
    count = 0
print(m_max)

Buggy submission (verdict: Runtime Error):
N = int(input())
H = [int(i) for i in input().split()]
m_max = 0
count = 0
for j in range(1, N):
  if H[j] <= H[j-1]:
  	count += 1
  	if j == N-1:
      if count > m_max:
      	m_max = count
  else:
    if count > m_max:
      m_max = count
    count = 0
print(m_max)

Buggy submission (verdict: Runtime Error):
N = int(input())
H = [int(i) for i in input().split()]
m_max = 0
count = 0
for j in range(1, N):
  if H[j] <= H[j-1]:
    count += 1
  	if j == N-1:
    	if count > m_max:
      		m_max = count
    	count = 0
  else:
    if count > m_max:
      m_max = count
    count = 0
print(m_max)
Problem p02936 (python)

Fixed submission:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
 
 
n,q = na()
g = [[] for i in range(n)]
c = [0]*n
 
for i in range(n-1):
    a,b = na()
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(q):
    p,x = na()
    c[p-1] += x
 
def f(b,a,r):
    r += c[a]
    c[a] = r
    for i in g[a]:
        if i != b:
            f(a,i,r)
 
f(-1,0,0)
print(' '.join([str(s) for s in c]))

Buggy submission (verdict: Runtime Error):
def ct(pp, ls, xx, tct):
  tct[pp-1] += xx
  nodel = [i for i, x in enumerate(ls) if x == pp]
  if bool(nodel):
    for j in nodel:
      ct(j, ls, xx, tct)
  return
 
N, Q = map(int, input().split())
nodeR = [0 for nn in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  nodeR[b-1] = a
tree_ct = [0 for nn in range(N)]
for j in range(Q):
  p, x = map(int, input().split())
  ct(p, nodeR, x, tree_ct)
print(' '.join(map(str, tree_ct)))

Buggy submission (verdict: Runtime Error):
def ct(pp, ls, xx, tct):
  tct[pp-1] += xx
  nodel = [i for i, x in enumerate(ls) if x == pp]:
  if bool(nodel):
    for j in nodel:
      ct(j, ls, xx, tct)
  return
 
N, Q = map(int, input().split())
nodeR = [0 for nn in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  nodeR[b-1] = a
tree_ct = [0 for nn in range(N)]
for j in range(Q):
  p, x = map(int, input().split())
  ct(p, nodeR, x, tree_ct)
print(' '.join(map(str, tree_ct)))

Buggy submission (verdict: Runtime Error):
def ct(pp, ls, xx, tct):
  tct[pp-1] += xx
  nodel = [i for i, x in enumerate(ls) if x == pp]:
  if bool(nodel):
    for j in nodel:
      ct(j, ls, xx, tct)
  return

N, Q = map(int, input().split())
nodeR = [0 for nn in range(N)]
for i in range(N):
  a, b = map(int, input().split())
  nodeR[b-1] = a
tree_ct = [0 for nn in range(N)]
for j in range(Q):
  p, x = map(int, input().split())
  ct(p, nodeR, x, tree_ct)
print(' '.join(map(str, tree_ct)))
Problem p03044 (python)

Fixed submission:
import sys
from collections import defaultdict
N = int(input())


sys.setrecursionlimit(1000000)

uvw = [[int(item) for item in input().split()] for _ in range(N-1)]

d = defaultdict(lambda: {})
for u, v, w in uvw:
    d[u][v] = w
    d[v][u] = w


dist = {1: 0}


def calc_dist(i, up):
    if not(i in dist):
        dist[i] = dist[up]+d[up][i]
        [calc_dist(k, i) for k in list(d[i].keys())]


[calc_dist(k, 1) for k in list(d[1].keys())]

for i in range(1, N+1):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
N = int(input())


sys.setrecursionlimit(10000)

uvw = [[int(item) for item in input().split()] for _ in range(N-1)]

d = defaultdict(lambda: {})
for u, v, w in uvw:
    d[u][v] = w
    d[v][u] = w


dist = {1: 0}


def calc_dist(i, up):
    if not(i in dist):
        dist[i] = dist[up]+d[up][i]
        [calc_dist(k, i) for k in list(d[i].keys())]


[calc_dist(k, 1) for k in list(d[1].keys())]

for i in range(1, N+1):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N = int(input())

uvw = [[int(item) for item in input().split()] for _ in range(N-1)]

d = defaultdict(lambda: {})
for u, v, w in uvw:
    d[u][v] = w
    d[v][u] = w


dist = {1: 0}


def calc_dist(i, up):
    if not(i in dist):
        dist[i] = dist[up]+d[up][i]
        [calc_dist(k, i) for k in list(d[i].keys())]


[calc_dist(k, 1) for k in list(d[1].keys())]

for i in range(1, N+1):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N = int(input())

color = [-1]*N

uvw = [[int(item) for item in input().split()] for _ in range(N-1)]

d = defaultdict(lambda: [])
for u, v, w in uvw:
    if w % 2 == 1:
        d[u].append(v)
        d[v].append(u)


def set_color(color, i, up=None):
    if color[i-1] == -1:
        if up is None:
            color[i-1] = 0
        else:
            color[i-1] = int(not color[up-1])
        for child in d[i]:
            color = set_color(color, child, i)
    return color


for k in d:
    color = set_color(color, k)


for i in range(N):
    print(max(color[i], 0))

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

color = [0]*N

uvw = [[int(item) for item in input().split()] for _ in range(N-1)]

for u, v, w in uvw:
    if w % 2 == 1:
        color[v-1] = int(not color[u-1])

for i in range(N):
    print(color[N])
Problem p03074 (python)

Fixed submission:
import re
import numpy as np
N, K = [int(item) for item in re.split(r"\s", input())]
S = [int(item) for item in list(input())]

if sum(S) == len(S) or sum(S) == 0:
    print(len(S))
else:
    S = S + [2]

    index = [-1]
    for i in range(N):
        if S[i] != S[i + 1]:
            index.append(i)

    count = np.diff(np.array(index)).tolist()

    if S[0] == 0:
        count = [0] + count
    if S[-2] == 0:
        count = count + [0]

    # print(index)
    #print("count", count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)
    win = min(2 * K + 1, c_len - 1)
    # print(cumsum)
    # print(win)

    print(max([cumsum[i + win] - cumsum[i] for i in range(0, c_len - win, 2)]))

Buggy submission (verdict: Runtime Error):
import re
import numpy as np
N, K = [int(item) for item in re.split(r"\s", input())]
S = [int(item) for item in list(input())]

if sum(S) == len(S) or sum(S) == 0:
    print(len(S))
else:
    S = S + [2]

    index = [-1]
    for i in range(N):
        if S[i] != S[i + 1]:
            index.append(i)

    count = np.diff(np.array(index)).tolist()

    # print(index)
    # print(count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)
    win = min(2 * K, c_len)
    if S[0] == 1:
        win += 1

    print(max([cumsum[i + win] - cumsum[i] for i in range(0, c_len - win, 2)]))

Buggy submission (verdict: Runtime Error):
import re
import numpy as np
N, K = [int(item) for item in re.split(r"\s", input())]
S = [int(item) for item in list(input())]

if sum(S) == len(S) or sum(S) == 0:
    print(len(S))
else:
    S = S + [2]

    index = [-1]
    for i in range(N):
        if S[i] != S[i + 1]:
            index.append(i)

    count = np.diff(np.array(index)).tolist()

    start = 0
    if S[0] == 0:
        start = 1

    # print(index)
    # print(count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)
    win = min(2 * K + 1, c_len - 1)
    print(max([cumsum[i + win] - cumsum[i] for i in range(start, c_len - win, 2)]))
Problem p03081 (python)

Fixed submission:
N, Q = map(int, input().split())
s = input()
TD = [input().split() for i in range(Q)]


def simulate(n):
    if n == -1:
        return -1
    if n == N:
        return 1
    for t, d in TD:
        if t == s[n]:
            if d == "L":
                n -= 1
            else:
                n += 1
        if n == -1:
            return -1
        elif n == N:
            return 1
    return 0


l = -1
r = N
while l+1 < r:
    n = (l+r)//2
    sim = simulate(n)
    if sim == -1:
        l = n
    else:
        r = n
L = l

l = -1
r = N
while l+1 < r:
    n = (l+r)//2
    sim = simulate(n)
    if sim == 1:
        r = n
    else:
        l = n
R = r
print(R-L-1)

Buggy submission (verdict: Runtime Error):
import re

N, Q = [int(item) for item in re.split(r"\s", input())]
s = input()
t = []
d = []
for _ in range(Q):
    t_in, d_in = [item for item in re.split(r"\s", input())]
    t.append(t_in)
    d.append(d_in)


def check(i, LR):
    """Return False when drop

    Args:
        i ([type]): [description]
    """

    for q in range(Q):
        if t[q] == s[i]:
            if d[q] == "L":
                i -= 1
            if d[q] == "R":
                i += 1

            if LR == "L" and i < 0 or LR == "R" and i > N-1:
                return False
    return True


s_index = 0  # include
e_index = N-1  # include
while e_index-s_index >= 2:
    c_index = (e_index-s_index+1)//2+s_index  # center or +1
    if check(c_index, "L"):
        # doesn't drop
        e_index = c_index
    else:
        s_index = c_index

pat1, pat2 = check(s_index, "L"), check(e_index, "L")
if pat1:
    left = s_index-1
elif pat2:
    left = s_index
else:
    left = e_index


s_index = 0  # include
e_index = N-1  # include
while e_index-s_index >= 2:
    c_index = (e_index-s_index+1)//2+s_index  # center or +1
    if check(c_index, "R"):
        # doesn't drop
        s_index = c_index
    else:
        e_index = c_index

pat1, pat2 = check(s_index, "R"), check(e_index, "R")
if pat2:
    right = e_index+1
elif pat1:
    right = e_index
else:
    right = s_index

print(right-left-1)
Problem p03147 (python)

Fixed submission:
import numpy as np

N = int(input())
h = [0]+list(map(int, input().split()))+[0]

diff = np.diff(h).tolist()
max_list = []
min_list = []

sign = 1
for i, d in enumerate(diff):
    if d > 0 and sign < 0:
        sign = d
        min_list.append(h[i])

    if d < 0 and sign > 0:
        sign = d
        max_list.append(h[i])


def add(val, max_list, min_list):
    if len(max_list) > 1:
        min_val = min(min_list)
        split_index = min_list.index(min_val)+1
        return min_val-val + \
            add(min_val, max_list[:split_index], min_list[:split_index-1]) + \
            add(min_val, max_list[split_index:], min_list[split_index:])
    else:
        return max_list[0]-val


if max(h) == 0:
    print(0)
else:
    print(add(0, max_list, min_list))

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
h = list(map(int, input().split()))

diff = np.diff(h)
max_list = []
min_list = []

sign = 1
for i, d in enumerate(diff):
    if d > 0 and sign == -1:
        sign = 1
        min_list.append(h[i])

    if d < 0 and sign == 1:
        sign = -1
        max_list.append(h[i])
if diff[-1] > 0:
    max_list.append(h[-1])


def add(val, max_list, min_list):
    if len(max_list) > 1:
        min_val = min(min_list)
        split_index = min_list.index(min_val)+1
        return min_val-val + \
            add(min_val, max_list[:split_index], min_list[:split_index-1]) + \
            add(min_val, max_list[split_index:], min_list[split_index:])
    else:
        return max_list[0]-val


print(add(0, max_list, min_list))
Problem p02936 (python)

Fixed submission:
# -*- coding:utf-8 -*-

from collections import defaultdict

N, Q = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(N-1)]
px = [tuple(map(int, input().split())) for _ in range(Q)]

parent_map = {b: a for a, b in ab}
child_map = defaultdict(list)
for parent, child in ab:
    child_map[parent].append(child)

operation_map = defaultdict(int)
for p, x in px:
    operation_map[p] += x

counter = [0] * N

counter[0] = operation_map[1]
stack = list(child_map[1])
node_no = 0
while True:
    try:
        node_no = stack.pop()
    except IndexError:
        break

    parent_no = parent_map[node_no]
    counter[node_no - 1] = counter[parent_no - 1] + operation_map[node_no]
    stack += list(child_map[node_no])

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

Buggy submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-

from collections import defaultdict

N, Q = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(N-1)]
px = [tuple(map(int, input().split())) for _ in range(Q)]

parent_map = {b: a for a, b in ab}
child_map = defaultdict(list)
for parent, child in ab:
    child_map[parent].append(child)

operation_map = defaultdict(int)
for p, x in px:
    operation_map[p] += x

counter = [0] * N


def count(parent_no, parent_counter):
    for child_no in child_map[parent_no]:
        child_count = parent_counter + operation_map[child_no]
        counter[child_no-1] = child_count
        count(child_no, child_count)


counter[0] = operation_map[1]
count(1, counter[0])

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