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

Fixed submission:
from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def dfs(i):
        res = 1
        for j in to[i]:
            res = res * dfs(j) % md
        return res + 1

    md = 998244353
    n = II()
    xd = LLI(n)
    xd.sort(reverse=True)
    to = [[] for _ in range(n)]
    hp = []
    for i, (x, d) in enumerate(xd):
        while hp and hp[0][0] < x + d:
            px, pi = heappop(hp)
            to[i].append(pi)
        heappush(hp, (x, i))
    ans = 1
    while hp:
        _, i = heappop(hp)
        ans = ans * dfs(i) % md
    print(ans)

main()

Buggy submission (verdict: Runtime Error):
from heapq import *
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class mint:
    def __init__(self, x):
        self.__x = x % md

    def __str__(self):
        return str(self.__x)

    def __add__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x + other)

    def __sub__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x - other)

    def __rsub__(self, other):
        return mint(other - self.__x)

    def __mul__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * other)

    __radd__ = __add__
    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * pow(other, md - 2, md))

    def __rtruediv__(self, other):
        return mint(other * pow(self.__x, md - 2, md))

    def __pow__(self, power, modulo=None):
        return mint(pow(self.__x, power, md))

md = 998244353

def main():
    def dfs(i):
        res=mint(1)
        for j in to[i]:
            res*=dfs(j)
        return res+1

    n=II()
    xd=LLI(n)
    xd.sort(reverse=True)
    to=[[] for _ in range(n)]
    hp=[]
    for i,(x,d) in enumerate(xd):
        while hp and hp[0][0]<x+d:
            px,pi=heappop(hp)
            to[i].append(pi)
        heappush(hp,(x,i))
    ans=1
    while hp:
        _,i=heappop(hp)
        ans*=dfs(i)
    print(ans)

main()
Problem p02840 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 総和は一定なので高橋君が何点(?)とるかの問題に置き換えられる
# d=0かつx=0のときは1、x!=0ならn+1で確定
# d,xと-d,-xは同値なので、dを正にすることで増加数列で考える
# d,xを同じ数で割っても答えは変わらないので、互いに素にする
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a % b)

def main():
    n, x, d = MI()
    if d == 0:
        if x == 0:
            print(1)
        else:
            print(n + 1)
        exit()
    if d < 0: d, x = -d, -x
    g = gcd(d, abs(x))
    d, x = d // g, x // g
    #print(x, d)
    # aをk個選ぶと和sはs=k*x+c*dで、cは最小で0からk-1の和
    # 最大でn-kからn-1の和
    # kを固定するとs//dは1ずつの単調増加なので、最小と最大で個数が分かる
    # sはs mod d = x*k mod dが同じなので、それで分類する
    kk = list(range(n + 1))
    kk.sort(key=lambda k: (k * x % d, k))
    #print(kk)
    ans = 0
    pmd = -1
    pl, pr = 1, 0
    for k in kk:
        l = (k * x + (k - 1) * k // 2 * d) // d
        r = (k * x + (2 * n - k - 1) * k // 2 * d) // d
        md = x * k % d
        if md == pmd and l <= pr and pl<=r:
            pl,pr=min(pl,l),max(pr,r)
        else:
            ans += pr - pl + 1
            pl, pr = l, r
        pmd = md
        #print(k, l, r, pl, pr, ans)
    ans += pr - pl + 1
    print(ans)

main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n, x, d = MI()
    ad = abs(d)
    # 数列からk個選んだ和の範囲をkx mod dで分けて記録
    range_by_mod = [[None, None] for _ in range(ad)]
    ans = 0
    for k in range(n + 1):
        l = (k * (k - 1) * d // 2 + k * x) // ad
        r = ((2 * n - k - 1) * k * d // 2 + k * x) // ad
        if l > r: l, r = r, l
        pl, pr = range_by_mod[k * x % ad]
        if pl == None:
            range_by_mod[k * x % ad] = [l, r]
        else:
            if l <= pr:
                range_by_mod[k * x % ad][1] = r
            else:
                ans += pr - pl + 1
                range_by_mod[k * x % ad] = [l, r]
        #print(k, l, r, k * x % ad, ans)
        #print(range_by_mod)
    for l, r in range_by_mod:
        if l == None: continue
        ans += r - l + 1
    print(ans)

main()
Problem p02903 (python)

Fixed submission:
from heapq import *

def main():
    h, w, a, b = map(int, input().split())
    r = []
    c = []
    t = [[0] * w for _ in range(h)]
    br = False
    for x in range(h + 1):
        for y in range(w + 1):
            if a * (h - x) + (w - a) * x == b * (w - y) + (h - b) * y:
                br = True
                break
        if br: break
    else:
        print("No")
        exit()
    for i in range(x):
        heappush(r, [-w + a, i])
    for i in range(x, h):
        heappush(r, [-a, i])
    for j in range(y):
        heappush(c, [-h + b, j])
    for j in range(y, w):
        heappush(c, [-b, j])
    while r:
        e0, i = heappop(r)
        tmp = []
        for _ in range(-e0):
            e1, j = heappop(c)
            t[i][j] = 1
            if -e1 > 1: tmp.append([e1 + 1, j])
        for l in tmp:
            heappush(c, l)
    for x in t:
        print(*x, sep="")

main()

Buggy submission (verdict: Runtime Error):
def main():
    h, w, a, b = map(int, input().split())
    r = [w - a] * h
    c = [h - b] * w
    t = [[0] * w for _ in range(h)]
    br = False
    for x in range(h, -1, -1):
        for y in range(w, -1, -1):
            if a * x + (w - a) * (h - x) == b * y + (h - b) * (w - y):
                br = True
                break
        if br: break
    else:
        print("No")
        exit()
    for i in range(x):
        r[h - 1 - i] = a
    for i in range(y):
        c[w - 1 - i] = b
    for i in range(h):
        j = 0
        while r[i] > 0:
            if c[j] == 0:
                j += 1
                continue
            t[i][j] = 1
            r[i] -= 1
            c[j] -= 1
            j += 1
    for x in t:
        print(*x, sep="")

main()

Buggy submission (verdict: Runtime Error):
def main():
    h, w, a, b = map(int, input().split())
    r = [w - a] * h
    c = [h - b] * w
    t = [[0] * w for _ in range(h)]
    for y in range(w, -1, -1):
        x = (b * y + (h - b) * (w - y) - h * (w - a)) / (2 * a - w)
        if int(x) == x and 0 <= x <= h:
            x = int(x)
            break
    else:
        print("No")
        exit()
    for i in range(x):
        r[h - 1 - i] = a
    for i in range(y):
        c[w - 1 - i] = b
    for i in range(h):
        j = 0
        while r[i] > 0:
            if c[j] == 0:
                j += 1
                continue
            t[i][j] = 1
            r[i] -= 1
            c[j] -= 1
            j += 1
    for x in t:
        print(*x, sep="")

main()
Problem p02920 (python)

Fixed submission:
def main():
    inf = 10 ** 9 + 5
    n = int(input())
    n2 = 2 ** n
    s = list(map(int, input().split()))
    s.sort(reverse=True)
    # print(s)
    pp = [s[0]]
    s[0] = inf
    for _ in range(n):
        pp.sort()
        i = 1
        np = pp[:]
        while pp:
            p = pp.pop()
            while 1:
                if s[i] < p:
                    np.append(s[i])
                    s[i] = inf
                    break
                else:
                    i += 1
                if i == n2:
                    print("No")
                    exit()
        pp = np
    print("Yes")

main()

Buggy submission (verdict: Runtime Error):
def main():
    inf = 10 ** 9 + 5
    n = int(input())
    n2 = 2 ** n
    s = list(map(int, input().split()))
    s.sort(reverse=True)
    # print(s)
    pp = [s[0]]
    s[0] = inf
    for _ in range(n):
        pp.sort()
        i = 1
        np = pp[:]
        while pp:
            p = pp.pop()
            while 1:
                if s[i] < p:
                    np.append(s[i])
                    s[i] = inf
                    i += 1
                    break
                else:
                    i += 1
                if i == n2:
                    print("No")
                    exit()
        pp = np
    print("Yes")

main()
Problem p02949 (python)

Fixed submission:
# YouTube
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)

def dfs(u):
    ok[u] = 1
    for v in to[u]:
        if ok[v]: continue
        dfs(v)

def rdfs(u):
    ok[u] += 2
    for v in fr[u]:
        if ok[v] > 1: continue
        rdfs(v)

inf = 10 ** 9
to = defaultdict(list)
fr = defaultdict(list)
abc = []
n, m, p = map(int, input().split())
ok = [0] * n
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    c = -(c - p)
    to[a].append(b)
    fr[b].append(a)
    abc.append([a, b, c])
dfs(0)
rdfs(n - 1)
score = [inf] * n
score[0] = 0
for i in range(n):
    stop = True
    for a, b, c in abc:
        if ok[a] < 3 or ok[b] < 3: continue
        tmp = score[a] + c
        if tmp < score[b]:
            score[b] = tmp
            stop = False
    if stop:
        print(max(0, -score[n - 1]))
        break
else:
    print(-1)

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

def dfs(u):
    ok[u] = 1
    for v in to[u]:
        if ok[v]: continue
        dfs(v)

def rdfs(u):
    ok[u] += 2
    for v in fr[u]:
        if ok[v] > 1: continue
        rdfs(v)

inf = 10 ** 9
to = defaultdict(list)
fr = defaultdict(list)
abc = []
n, m, p = map(int, input().split())
ok = [0] * n
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    c = -(c - p)
    to[a].append(b)
    fr[b].append(a)
    abc.append([a, b, c])
dfs(0)
rdfs(n - 1)
score = [inf] * n
score[0] = 0
for i in range(n):
    stop = True
    for a, b, c in abc:
        if ok[a] < 3 or ok[b] < 3: continue
        tmp = score[a] + c
        if tmp < score[b]:
            score[b] = tmp
            stop = False
    if stop:
        print(max(0, -score[n - 1]))
        break
else:
    print(-1)
Problem p02973 (python)

Fixed submission:
import sys
from bisect import *

input=sys.stdin.readline

def f(n):
    mi=-1
    r = [inf]*n
    for _ in range(n):
        a = -int(input())
        i = bisect_right(r, a)
        r[i]=a
        if i>mi:
            mi=i
    print(mi+1)

n = int(input())
inf=10**10
f(n)

Buggy submission (verdict: Runtime Error):
from bisect import *

def f(n):
    r = []
    for _ in range(n):
        a = int(input())
        if not r:
            r.append(a)
            continue
        i = bisect_left(r, a)
        if i:
            r[i]=a
        else:
            r = [a] + r
    print(len(r))

n = int(input())
f(n)
Problem p02974 (python)

Fixed submission:
def f(n,k):
    md = 10 ** 9 + 7
    if k % 2:
        print(0)
        exit()
    k //= 2
    dp = [[[0] * (n ** 2) for _ in range(n + 2)] for __ in range(n + 1)]
    dp[0][0][0] = 1
    for i in range(1, n + 1):
        dpi1 = dp[i - 1]
        for j in range(i + 1):
            for s in range(j, k + 1):
                # 片側、水平、両方上
                #            print(i,j,s)
                tmp = dpi1[j][s - j] * j * 2 + dpi1[j][s - j] + dpi1[j + 1][s - j] * (j + 1) ** 2
                if j:
                    # 両方保留
                    tmp += dp[i - 1][j - 1][s - j]
                dp[i][j][s] = tmp % md
    print(dp[n][0][k])

n, k = map(int, input().split())
f(n,k)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
md = 10 ** 9 + 7
if k % 2:
    print(0)
    exit()
k //= 2
dp = [[[0] * (n ** 2) for _ in range(n + 1)] for __ in range(n + 1)]
dp[0][0][0] = 1
for i in range(1, n + 1):
    dpi1 = dp[i - 1]
    for j in range(i + 1):
        for s in range(j, k + 1):
            # 片側、水平、両方上
            tmp = dpi1[j][s - j] * j * 2 + dpi1[j][s - j] + dpi1[j + 1][s - j] * (j + 1) ** 2
            if j:
                # 両方保留
                tmp += dp[i - 1][j - 1][s - j]
            dp[i][j][s] = tmp % md
print(dp[n][0][k])
Problem p02985 (python)

Fixed submission:
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)

def per(a, b):
    t = (a, b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re = re * (a - i) % md
    memo[t] = re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko) % md
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re = re * dfs(ki, i) % md
    return re

md = 10 ** 9 + 7
memo = {}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))

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

def per(a, b):
    t=(a,b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re *= a - i
    memo[t]=re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko)
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re *= dfs(ki, i)
    return re%md

md = 10 ** 9 + 7
memo={}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))

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

def per(a, b):
    re = 1
    for i in range(b):
        re *= a - i
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko)
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re *= dfs(ki, i)
    return re%md

md = 10 ** 9 + 7
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))
Problem p02992 (python)

Fixed submission:
def f(n,k):
    lim = int((n + 0.1) ** 0.5) + 1
    ws = []
    s = 0
    for i in range(1, lim):
        w = n // i - n // (i + 1)
        ws.append(w)
        s += w
    ws += [1] * (n - s)
    m = len(ws)
    dp0 = ws[::-1]
    dp1 = [0] * m
    for _ in range(k - 1):
        s = 0
        for j in range(m):
            s += dp0[j]
            dp1[m - j - 1] = (s * ws[j]) % md
        dp0 = dp1[:]
    print(sum(dp0) % md)
md=10**9+7
n,k=map(int,input().split())
f(n,k)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
md=10**9+7
a=[0]*n
for i in range(1,n+1):
    a[i-1]=n//i
#print(a)
ans=a[:]
for _ in range(k-2):
    ans=[sum(ans[:ak])%md for ak in a]
#    print(ans)
print(sum(ans)%md)
Problem p02995 (python)

Fixed submission:
def gcd(x,y):
    while y:
        x,y=y,x%y
    return x

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

def kosu(b, c, d):
    return b - ((b // c) + (b // d) - (b // lcm(c, d)))

a, b, c, d = map(int, input().split())
print(kosu(b, c, d) - kosu(a - 1, c, d))

Buggy submission (verdict: Runtime Error):
from math import gcd

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

def kosu(b, c, d):
    return b - ((b // c) + (b // d) - (b // lcm(c, d)))

a, b, c, d = map(int, input().split())
print(kosu(b, c, d) - kosu(a - 1, c, d))
Problem p03006 (python)

Fixed submission:
n=int(input())
xy=[list(map(int,input().split())) for _ in range(n)]
if n==1:
    print(1)
    exit()
pq={}
for i in range(n):
    x,y=xy[i]
    for j in range(n):
        if j==i:continue
        xx,yy=xy[j]
        p=xx-x
        q=yy-y
        pq.setdefault((p,q),0)
        pq[(p,q)]+=1
print(n-max(pq.values()))

Buggy submission (verdict: Runtime Error):
n=int(input())
xy=[list(map(int,input().split())) for _ in range(n)]
pq={}
for i in range(n):
    x,y=xy[i]
    for j in range(n):
        if j==i:continue
        xx,yy=xy[j]
        p=xx-x
        q=yy-y
        pq.setdefault((p,q),0)
        pq[(p,q)]+=1
print(n-max(pq.values()))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

def cost(i,p=100,q=100,s=1,used=set()):
    if len(used)==n:
        return s
    if not used:
        used.add(i)
        x0,y0=xy[i]
        re=[]
        for j in range(n):
            if j==i:continue
            x,y=xy[j]
            used.add(j)
            re+=[cost(j,x-x0,y-y0,1,used)]
            used.remove(j)
        return min(re)
    x0,y0=xy[i]
    re=[]
    for j in range(n):
        if j in used:continue
        x,y=xy[j]
        used.add(j)
        if (x-x0,y-y0)==(p,q):
            re+=[cost(j,p,q,s,used)]
        else:
            re += [cost(j,p,q,s+1,used)]
        used.remove(j)
    return min(re)

n=int(input())
xy=[list(map(int,input().split())) for _ in range(n)]
mn=100
for i in range(n):
    c=cost(i)
    if c<mn:
        mn=c
print(mn)

Buggy submission (verdict: Runtime Error):
def cost(i,p=100,q=100,s=1,used=set()):
    if len(used)==n:
        return s
    if not used:
        used.add(i)
        x0,y0=xy[i]
        re=[]
        for j in range(n):
            if j==i:continue
            x,y=xy[j]
            used.add(j)
            re+=[cost(j,x-x0,y-y0,1,used)]
            used.remove(j)
        return min(re)
    x0,y0=xy[i]
    re=[]
    for j in range(n):
        if j in used:continue
        x,y=xy[j]
        used.add(j)
        if (x-x0,y-y0)==(p,q):
            re+=[cost(j,p,q,s,used)]
        else:
            re += [cost(j,p,q,s+1,used)]
        used.remove(j)
    return min(re)

n=int(input())
xy=[list(map(int,input().split())) for _ in range(n)]
mn=100
for i in range(n):
    c=cost(i)
    if c<mn:
        mn=c
print(mn)

Buggy submission (verdict: Runtime Error):
def cost(i,p=100,q=100,s=1,used=set()):
    if len(used)==n:
        return s
    if not used:
        used.add(i)
        x0,y0=xy[i]
        re=[]
        for j in range(n):
            if j==i:continue
            x,y=xy[j]
            used.add(j)
            re+=[cost(j,x-x0,y-y0,1,used)]
            used.remove(j)
        return min(re)
    x0,y0=xy[i]
    re=[]
    for j in range(n):
        if j in used:continue
        x,y=xy[j]
        used.add(j)
        if (x-x0,y-y0)==(p,q):
            re+=[cost(j,p,q,s,used)]
        else:
            re += [cost(j,p,q,s+1,used)]
        used.remove(j)
    return min(re)

n=int(input())
xy=[list(map(int,input().split())) for _ in range(n)]
mn=100
for i in range(n):
    c=cost(i)
    if c<mn:
        mn=c
print(mn)
Problem p03007 (python)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
R=a.pop()
L=a.pop(0)
s=L
if a:
    while a[-1]>0:
        r=a.pop()
        ans.append([s,r])
        s=s-r
        if not a:break
ans.append([R,s])
s=R-s
while a:
    L=a.pop()
    ans.append([s,L])
    s=s-L
print(s)
for x in ans:
    print(*x)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
R=a.pop()
L=a.pop(0)
s=L
if not a:
    while a[-1]>0:
        r=a.pop()
        ans.append([s,r])
        s=s-r
        if not a:break
ans.append([R,s])
s=R-s
while a:
    L=a.pop()
    ans.append([s,L])
    s=s-L
print(s)
for x in ans:
    print(*x)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
R=a.pop()
L=a.pop(0)
s=L
while a[-1]>0:
    r=a.pop()
    ans.append([s,r])
    s=s-r
    if not a:break
ans.append([R,s])
s=R-s
while a:
    L=a.pop()
    ans.append([s,L])
    s=s-L
print(s)
for x in ans:
    print(*x)
Problem p03032 (python)

Fixed submission:
def solve(k, koho, cho=[], syo=0):
    if syo == 2:
        lenc=len(cho)
        for i in range(min(k,lenc)):
            mn = min(cho)
            if mn < 0:
                cho.remove(mn)
            else:
                break
        return sum(cho)
    if syo == 1:
        koho = koho[::-1]
    re = []
    lenk = len(koho)
    for i in range(min(k, lenk)+1):
        re.append(solve(k - i, koho[i:], cho+koho[:i], syo + 1))
    return max(re)

n, k = map(int, input().split())
vs = list(map(int, input().split()))
print(solve(k, vs))

Buggy submission (verdict: Runtime Error):
import copy

def solve(i, vs, hv):
    if i == 0:
        return sum(hv)
    re = []
    re += [solve(i - 1, vs, hv)]
    re += [solve(i - 1, vs[1:], hv + [vs[0]])]
    re += [solve(i - 1, vs[:-1], hv + [vs[-1]])]
    for hk in hv:
        if hk < 0:
            nhv=copy.deepcopy(hv)
            nhv.remove(hk)
            re += [solve(i - 1, vs + [hk], nhv)]
            re += [solve(i - 1, [hk] + vs, nhv)]
    return max(re)

n, k = map(int, input().split())
vs = list(map(int, input().split()))
print(solve(k, vs, []))
Problem p03044 (python)

Fixed submission:
n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1]=1
nodes=[1]
used=set()
used.add(1)
ansi=1
while nodes:
    nn=[]
    for i in nodes:
        ansi=ans[i]
        for u,w in pea[i]:
            if u in used:continue
            ans[u]=(ansi+w)%2
            used.add(u)
            nn+=[u]
    nodes=nn
for x in ans[1:]:
    print(x)

Buggy submission (verdict: Runtime Error):
import sys

input = sys.stdin.readline

def hantei(i, oya=-1):
    if ans[i] != -1:
        return ans[i]
    for u, w in pea[i]:
        if u == oya: continue
        if hantei(u, i):
            re = ans[i] = (hantei(u, i) + w) % 2
            return re

n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1] = 1
for i in range(1, n + 1):
    print(hantei(i))