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

Fixed submission:
import math
def solve():
    N = int(input())
    X = list(map(int, input().split()))

    mn = float('inf')
    for i in range(1, 101):
        mn = min(mn, calc(X, i))
    print(mn)


def calc(X, P):
    retval = 0
    for x in X:
        retval += (x - P) ** 2 
    return retval
    
if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
from statistics import mean
import math
def solve():
    N = int(input())
    X = list(map(int, input().split()))
    mn = mean(X)
    if isinstance(mn, int) or mn.is_integer():
        P = mn
        print(calc(X, P))
    else:
        ans = min(calc(X, math.floor(mn)), calc(X, math.ceil(mn)))
        print(ans)

def calc(X, P):
    retval = 0
    for x in X:
        retval += (x - P) ** 2 
    return retval
    
if __name__ == '__main__':
    solve()
Problem p02787 (python)

Fixed submission:
def solve():
    h, n = map(int, input().split())
    l = [list(map(int, input().split())) for i in range(n)]

    mxA = 10 ** 4
    dp = [[10**15 for i in range(h+1+mxA)] for i in range(n+1)]

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

    for i in range(n):
        for sum_dam in range(h+1+mxA):
            if sum_dam-l[i][0]>=0:
                dp[i+1][sum_dam] = min(dp[i+1][sum_dam],  \
                                dp[i][sum_dam - l[i][0]] + l[i][1],  \
                                dp[i+1][sum_dam-l[i][0]] + l[i][1])
            dp[i+1][sum_dam] = min(dp[i+1][sum_dam], dp[i][sum_dam])

    print(min(dp[-1][h:h+mxA+1]))

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    N, W = map(int, input().split())
    MAX_V = 100000
    MAX_N = 100
    w = []
    v = []
    for i in range(N):
        tmpw, tmpv = map(int, input().split())
        w.append(tmpw)
        v.append(tmpv)

    dp = [[float('inf') for i in range(MAX_V+1)] for j in range(MAX_N+1)]

    dp[0][0] = 0

    for i in range(N):
        for sum_v in range(MAX_V):
            if sum_v - v[i] >= 0:
                dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v - v[i]] + w[i])
            
            dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])
        
    res = 0
    for sum_v in range(MAX_V+1):
        if dp[N][sum_v] <= W:
            res = sum_v
    print(res)

if __name__ == '__main__':
    solve()
Problem p02814 (python)

Fixed submission:
import fractions
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))

    div2cnt = 0
    a = A[0]
    while a % 2 == 0:
        a //= 2
        div2cnt += 1

    lcmval = A[0]//2
    for i in range(1, N):
        if A[i] % (2 ** (div2cnt+1)) == 0 or A[i] % (2 ** div2cnt) != 0:
            print(0)
            exit(0)
        
        lcmval = lcm(lcmval, A[i]//2)
        if lcmval > M:
            print(0)
            exit(0)
    
    print(int((M-lcmval)//(lcmval* 2)+ 1))

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

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import fractions
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))

    div2cnt = 0
    while int(A[0]/2) % (2 ** div2cnt+1) == 0:
        div2cnt += 1

    lcmval = 0
    for i in range(1, N):
        if int(A[i]/2) % (2 ** (div2cnt+1)) == 0 or int(A[i]/2) % (2 ** div2cnt) != 0:
            print(0)
            exit(0)
        
        if i == 1:
            lcmval = lcm(A[i-1]/2, A[i]/2)
        else:
            lcmval = lcm(lcmval, A[i]/2)
    
    print(int((M-lcmval)//(lcmval* 2)+ 1))

def lcm(x, y):
    return x * y // fractions.gcd(x, y)
if __name__ == '__main__':
    solve()
Problem p02843 (python)

Fixed submission:
def solve():
    X = input()

    if int(X) < 100:
        print('0')
        exit(0)

    twoDig = int(X[len(X)-2:])
    rest = int(X[:len(X)-2])

    ans = '1'
    for i in range(rest+1):
        if twoDig == 0:
            break
        for n in range(5, 0, -1):
            if twoDig - n >= 0:
                twoDig -= n
                break
    else:
        ans = '0'
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    X = input()
    twoDig = int(X[len(X)-2:])
    rest = int(X[:len(X)-2])

    ans = '1'
    for i in range(rest+1):
        if twoDig == 0:
            break
        for n in range(5, 0, -1):
            if twoDig - n >= 0:
                twoDig -= n
                break
    else:
        ans = '0'
    
    print(ans)

if __name__ == '__main__':
    solve()
Problem p02854 (python)

Fixed submission:
def solve():
    N = int(input())
    A = list(map(int,input().split()))

    sm = sum(A)
    agg = 0
    idx = 0
    mid = sm / 2
    shrink_cnt = sm - N
    while agg < mid and idx < N-1:
        
        agg += A[idx]
        idx += 1
    
    oneleft = agg
    oneright = sm - oneleft 
    twoleft = agg-A[idx-1]
    tworight = sm - twoleft
    ans = min(abs(oneleft-oneright), abs(twoleft-tworight))

    print(int(ans))

if __name__ == '__main__':
    solve()

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

    sm = sum(A)
    agg = 0
    idx = 0
    mid = sm / 2
    while agg < mid:
        agg += A[idx]
        idx += 1
    
    ans = min(abs(agg-mid) / 0.5, abs(agg-A[idx]-mid)/0.5)
    
    print(int(ans))

if __name__ == '__main__':
    solve()
Problem p02900 (python)

Fixed submission:
def solve():
    A, B = map(int, input().split())

    primeFactorA = prime_factorize(A)
    primeFactorB = prime_factorize(B)

    commonFactor = [1]
    for f in primeFactorB:
        if f in primeFactorA:
            commonFactor.append(f)
    
    print(len(commonFactor))


def prime_factorize(num):
    a = set() 
    while num % 2 == 0:
        a.add(2)
        num //= 2
    
    f = 3
    while f * f <= num:
        if num % f == 0:
            a.add(f)
            num //= f
        else:
            f += 2
    if num != 1:
        a.add(num)
    
    return a

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import math

def solve():
    A, B = map(int, input().split())

    C = math.gcd(A,B) 
    

    cnt = 1
    primeNum = []
    for i in range(3, C + 1, 2):
        if A % i == 0 and B % i == 0:
            if isPrime(i, primeNum):
                primeNum.append(i)
                cnt += 1

    if A % 2 == 0 and B % 2 == 0:
        cnt += 1

    if A == B:
        cnt += 1
    
    print(cnt)
        

def isPrime(num, primeNum):
    for n in primeNum:
        if n > math.sqrt(num):
            break 
        if num % n == 0:
            return False
        
    return True


if __name__ == '__main__':
    solve()
Problem p02924 (python)

Fixed submission:
def func():
    N = int(input())
    sum = 0
    for i in range(1, N + 1):
        sum += i

    print(sum-N)


if __name__ == '__main__':
    func()

Buggy submission (verdict: Runtime Error):
def func():
    N = int(input())
    serial = list(range(1, N + 1))

    print(sum(serial)-N)


if __name__ == '__main__':
    func()
Problem p02936 (python)

Fixed submission:
import sys
def solve():
    sys.setrecursionlimit(1000000)
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    tree = [[] for i in range(N)]
    counter = [0] * N
    arrived = [False] * N
    for i in range(N-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    
    for i in range(Q):
        p, x = map(int, input().split())
        counter[p-1] += x
    
    arrived[0] = True
    adder(0, counter, tree, arrived)

    print(*counter)

def adder(ind, counter, tree, arrived):
    for i in tree[ind]:
        if arrived[i]:
            continue
        counter[i] += counter[ind]
        arrived[i] = True
        adder(i, counter, tree, arrived)
    if not tree[ind]:
        return

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    N, Q = map(int, input().split())
    tree = [[] for i in range(N)]
    counter = [0] * N
    for i in range(N-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
    
    for i in range(Q):
        p, x = map(int, input().split())
        adder(p-1, x, counter, tree)

    print(counter)

def adder(ind, add_val, counter, tree):
    counter[ind] += add_val
    if not tree[ind]:
        return

    for i in tree[ind]:
        adder(i, add_val, counter, tree)

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    N, Q = map(int, input().split())
    tree = [[] for i in range(N)]
    counter = [0] * N
    for i in range(N-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
    
    for i in range(Q):
        p, x = map(int, input().split())
        adder(p-1, x, counter, tree)

    print(counter)

def adder(ind, add_val, counter, tree):
    counter[ind] += add_val
    if not tree[ind]:
        return

    for i in tree[ind]:
        adder(i, add_val, counter, tree)

if __name__ == '__main__':
    solve()
Problem p03044 (python)

Fixed submission:
from collections import defaultdict
import sys
sys.setrecursionlimit(10**5)
def solve():
    N = int(input())
    G = defaultdict(list)
    for i in range(N-1):
        u,v,w = map(int,input().split())
        u -= 1
        v -= 1
        G[u].append((v,w))
        G[v].append((u,w))

    res = [0] * N

    dfs(0,-1,1,G,res)

    print(*res, sep='\n')
def dfs(v,p,c,G,res):
    res[v] = c
    for e in G[v]:
        if e[0] == p:
            continue
        if e[1] % 2 != 0:
            dfs(e[0], v, 1-c,G,res)
        else:
            dfs(e[0],v,c,G,res)
        
if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def solve():
    N = int(input())
    G = defaultdict(list)
    for i in range(N-1):
        u,v,w = map(int,input().split())
        u -= 1
        v -= 1
        G[u].append((v,w))
        G[v].append((u,w))

    res = [0] * N

    dfs(0,-1,1,G,res)

    print(*res, sep='\n')
def dfs(v,p,c,G,res):
    res[v] = c
    for e in G[v]:
        if e[0] == p:
            continue
        if e[1] % 2 != 0:
            dfs(e[0], v, 1-c,G,res)
        else:
            dfs(e[0],v,c,G,res)
        
if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def solve():
    N = int(input())
    G = defaultdict(list)
    for i in range(N-1):
        u,v,w = map(int,input().split())
        u -= 1
        v -= 1
        G[u].append((v,w))
        G[v].append((u,w))

    res = [0] * N

    dfs(0,-1,1,G,res)

    print(*res, sep='\n')
def dfs(v,p,c,G,res):
    res[v] = c
    for e in G[v]:
        if e[0] == p:
            continue
        if e[1] % 2 == 0:
            dfs(e[0], v, 1-c,G,res)
        else:
            dfs(e[0],v,c,G,res)
        
if __name__ == '__main__':
    solve()
Problem p03161 (python)

Fixed submission:
def solve():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, N):
        lst = []
        for k in range(1,i+1):
            if k > K:
                break
            lst.append(abs(h[i] - h[i-k]) + dp[i-k])
        
        lst.append(dp[i])
        dp[i] = min(*lst)
    
    print(dp[N-1])

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    global h, dp, K
    N, K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [float('inf')] * N

    dp[0] = 0
    print(rec(N-1))

def rec(i):
    if dp[i] != float('inf'):
        return dp[i]
    if i == 0:
        return 0
    
    res = float('inf')
    for j in range(1,K+1):
        if i-j < 0:
            break
        res = min(res, rec(i-j) + abs(h[i] - h[i-j]))
    
    dp[i] = res
    return res

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1, N):
        lst = []
        for k in range(1,i+1):
            if k > K:
                break
            lst.append(abs(h[i] - h[i-k]) + dp[i-k])
        
        dp[i] = min(*lst, dp[i])
    
    print(dp[N-1])

if __name__ == '__main__':
    solve()
Problem p03262 (python)

Fixed submission:
import fractions


def func():
    N, init = map(int, input().split())
    x = list(map(int, input().split()))

    x.append(init)
    x.sort()

    ans = x[1] - x[0]
    for i in range(N):
        ans = fractions.gcd(ans, x[i+1] - x[i])

    print(ans)


if __name__ == '__main__':
    func()

Buggy submission (verdict: Runtime Error):
import math


def func():
    N, init = map(int, input().split())
    x = list(map(int, input().split()))

    x.append(init)
    x.sort()

    ans = x[1] - x[0]
    for i in range(N):
        ans = math.gcd(ans, x[i+1] - x[i])

    print(ans)


if __name__ == '__main__':
    func()
Problem p03282 (python)

Fixed submission:
def func():
    S = input()
    K = int(input())

    Slen = len(S)

    ans = 1
    for i in range(K):
        if i == Slen:
            break

        if S[i] != "1":
            ans = S[i]
            break

    print(ans)


if __name__ == '__main__':
    func()

Buggy submission (verdict: Runtime Error):
def func():
    S = input()
    K = int(input())

    ans = 0
    if S[0] == '1':
        ans = S[1]
    else:
        ans = S[0]

    print(ans)


if __name__ == '__main__':
    func()

Buggy submission (verdict: Runtime Error):
def func():
    S = input()
    K = int(input())

    ans = 0
    if S[0] == '1':
        ans = S[1]
    else:
        ans = S[0]

    print(ans)


if __name__ == '__main__':
    func()

Buggy submission (verdict: Runtime Error):
def func():
    S = input()
    K = int(input())

    if S[0] == "1":
        print(S[1])
    else:
        print(S[0])


if __name__ == '__main__':
    func()
Problem p03290 (python)

Fixed submission:
def func():
    D, G = map(int, input().split())
    p = [0] * D 
    bonus = [0] * D
    for i in range(D):
        p[i], bonus[i] = map(int, input().split())
    
    ans = 10000
    for i in range(2 ** D):
        chosen = []
        for j in range(D):
            if ((i >> j) & 1) == 1:
                chosen.append(j)
        
        pointSum = 0
        Nsolved = 0
        for idx in chosen:
            pointSum += (idx + 1) * 100 * p[idx] + bonus[idx]
            Nsolved += p[idx]
        
        if pointSum >= G:
            ans = min(ans, Nsolved)
        else:
            notChosen = list(filter(lambda x: x not in chosen, list(range(D))))
            highestDimIdx = notChosen[-1]
            # if pointSum + (highestDimIdx + 1) * 100 * (p[highestDimIdx]-1) >= G:
            #     rest = G - pointSum
            #     Nsolved += -(-(rest // ((highestDimIdx + 1) * 100)))
            #     ans = min(ans, Nsolved)

            Nsolved += min(p[highestDimIdx] - 1, -(-(G - pointSum) // ((highestDimIdx + 1) * 100)))
            pointSum += min(p[highestDimIdx] - 1, -(-(G - pointSum) // (highestDimIdx + 1 * 100))) * (highestDimIdx + 1) * 100
            
            if pointSum >= G:
                ans = min(ans, Nsolved)
            

    print(ans)
        
if __name__ == '__main__':
    func()

Buggy submission (verdict: Runtime Error):
def func():
    D, G = map(int, input().split())
    p = [0] * D 
    bonus = [0] * D
    for i in range(D):
        p[i], bonus[i] = map(int, input().split())
    
    ans = 10000
    for i in range(2 ** D):
        chosen = []
        for j in range(D):
            if ((i >> j) & 1) == 1:
                chosen.append(j)
        
        pointSum = 0
        Nsolved = 0
        for idx in chosen:
            pointSum += (idx + 1) * 100 * p[idx] + bonus[idx]
            Nsolved += p[idx]
        
        if pointSum > G:
            continue
        else:
            notChosen = list(filter(lambda x: x not in chosen, list(range(D))))
            highestDimIdx = notChosen[-1]
            if pointSum + (highestDimIdx + 1) * 100 * p[highestDimIdx] >= G:
                rest = G - pointSum
                Nsolved += -(-(rest // ((highestDimIdx + 1) * 100)))
                ans = min(ans, Nsolved)

    print(ans)
        
if __name__ == '__main__':
    func()
Problem p03353 (python)

Fixed submission:
def solve():
    S = input()
    K = int(input())

    N = len(S)

    st = set()

    for left in range(N):
        for right in range(left+1,N+1):
            if right - left > K:
                break
            st.add(S[left:right])
    
    tmp = list(st)
    tmp.sort()
    print(tmp[K-1])

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    S = input()
    K = int(input())

    N = len(S)

    st = set()

    for left in range(N):
        for right in range(left+1,N):
            if right - left > K:
                break
            st.add(S[left:right])
    
    tmp = list(st)
    tmp.sort()
    print(tmp[K-1])

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    S = input()
    K = int(input())

    N = len(S)

    st = set()

    for left in range(N-1):
        for right in range(left+1,N+1):
            if right - left > K:
                break
            st.add(S[left:right])
    
    tmp = list(st)
    tmp.sort()
    print(tmp[K-1])

if __name__ == '__main__':
    solve()
Problem p03569 (python)

Fixed submission:
def solve():
    S = input()
    N = len(S)

    left = 0
    right = N-1

    ans = 0
    while right - left > 0:
        if S[left] == S[right]:
            left += 1
            right -= 1
        else: 
            if S[left] == 'x':
                left += 1
                ans += 1
            elif S[right] == 'x':
                right -= 1
                ans += 1
            else:
                print('-1')
                return
    
    print(ans)
                
if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    S = input()
    N = len(S)

    left = 0
    right = N-1

    ans = 0
    while left != right:
        if S[left] == S[right]:
            left += 1
            right -= 1
        else: 
            if S[left] == 'x':
                left += 1
                ans += 1
            elif S[right] == 'x':
                right -= 1
                ans += 1
            else:
                print('-1')
                return
    
    print(ans)
                
if __name__ == '__main__':
    solve()
Problem p03994 (python)

Fixed submission:
def solve():
    s = list(input())
    K = int(input())

    for i in range(len(s)):
        distToA = (ord('z')+1 - ord(s[i])) % 26
        if distToA <= K:
            s[i] = 'a'
            K -= distToA
    else:
        s[-1] = chr(ord(s[-1]) + K%26)
    
    print("".join(s))

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
def solve():
    s = list(input())
    K = int(input())

    for i in range(len(s)):
        distToA = (ord('z')+1 - ord(s[i])) % ord('a')
        if distToA <= K:
            s[i] = 'a'
            K -= distToA
    else:
        s[-1] = chr(ord(s[-1]) + K)
    
    print("".join(s))

if __name__ == '__main__':
    solve()
Problem p02791 (python)

Fixed submission:
n=int(input())
l=list(map(int, input().split()))
ans=0
min=2*100000
for i in range(n):
    num_now=l[i]
    if num_now<=min:
        ans+=1
        min=num_now
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
l1=input().split()
l=[int(x)for x in l1]
ans=0
for i in range(n):
    list_now=l[0:i]
    num_now=l[i]
    list_now.sort()
    if num_now==list_now[0]:
        ans+=1
print(ans)
Problem p03381 (python)

Fixed submission:
import numpy as np
n=int(input())
x=list(map(int,input().split(' ')))
x1=x[:]
x.sort()
m1=x[int(n/2)-1]
m2=x[int(n/2)]
for i in range(n):
    now=x1[i]
    if x1[i]<=m1:
        print(m2)
    else:
        print(m1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=list(map(int,input().split(' ')))
x1=x[:]
x1.sort()
x2=x1[:]
for i in range(n):
    l=x[i]
    print(x1)
    x1.remove(l)
    print (x1[int(n/2)-1])
    print(x1)
    x1=x2[:]
Problem p03638 (python)

Fixed submission:
h,w=map(int,raw_input().split())
n=input()
g=map(int,raw_input().split())
l=[]
for i in range(n):
    l=l+[i+1]*g[i]
for i in range(h):
    l1=l[i*w:(i+1)*w]
    if i%2==1:
        l1.reverse()
    for j in l1:
        print j,
    print

Buggy submission (verdict: Runtime Error):
h,w=map(int,raw_input().split())
n=input()
g=map(int,raw_input().split())
l=[]
for i in range(n):
    l=l+[i+1]*g[i]
for i in range(h):
    l1=[]
    for j in range(w):
        l1.append(l[h*i+j])
    if i%2==1:
        l1.reverse()
    for j in l1:
        print j,
    print

Buggy submission (verdict: Runtime Error):
h,w=map(int,raw_input().split())
n=input()
g=map(int,raw_input().split())
l=[]
for i in range(n):
    l=l+[i+1]*g[i]
ans=[]
for i in range(h):
    l1=[]
    for j in range(w):
        l1.append(l[h*i+j])
    if i%2==0:
        ans=ans+[l1]
    elif i%2==1:
        l1.reverse()
        ans=ans+[l1]
for i in ans:
    for j in i:
        print j,
    print
Problem p03711 (python)

Fixed submission:
x,y=map(int,input().split())
a=[1,3,5,7,8,10,12]
b=[4,6,9,11]
c=[2]
if ((x in a)and(y in a))\
    or((x in b)and(y in b))\
    or((x in c)and(y in c)):
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
ans=[]
import sys
def s(a,b):
    if a%3==1:
        s=a*b
        x=(a//3)*b
        y=((a-(a//3))//2)*b
        z=s-x-y

        e=(a-(a//3))*(b//2)
        d=s-x-e
        ma=max(x,y,z)
        mi=min(x,y,z)
        mc=max(x,e,d)
        mn=min(x,e,d)
        return(min((ma-mi),(mc-mn)))
    elif a%3==2:
        s=a*b
        x=((a//3)+1)*b
        y=((a-(a//3)-1)//2)*b
        z=s-x-y
        e=(a-(a//3)-1)*(b//2)
        d=s-x-e
        ma=max(x,y,z)
        mi=min(x,y,z)
        mc=max(x,e,d)
        mn=min(x,e,d)
        return(min((ma-mi),(mc-mn)))
j=min(h,w)
if (h%3==0)or(w%3==0):
    print(0)
elif j==1:
    print(1)
    sys.exit()
    
else:
    k=s(h,w)
    ans.append(k)
    r=s(w,h)
    ans.append(r)
    
# print(ans)
print(min(ans))