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

Fixed submission:
def examA():
    X = LI()
    for i in range(len(X)):
        if X[i]==0:
            ans = i+1
    print(ans)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examA()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Buggy submission (verdict: Runtime Error):

def examD():
    def make_divisors(n):
        divisors = [1]
        for i in range(2, int(n ** 0.5) + 1):
            if n % i == 0:
                divisors.append(i)
                if i != n // i:
                    divisors.append(n // i)
        # divisors.sort()
        return divisors
    N = I()
    A = LI()
    C = set()
    D = set()
    ans = 0
    for a in A:
        if a in D:
            C.discard(a)
            continue
        D.add(a)
        C.add(a)

    for a in C:
        flag = 2
        cur = make_divisors(a)
        for c in cur:
            if c in D:
                flag -= 1
            if flag==0:
                break
        if flag>0:
            ans += 1
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""
Problem p02883 (python)

Fixed submission:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)
N, K =LI()
A = LI()
F = LI()
A.sort(reverse=True)
F.sort()

xupper = max(A)*max(F)
xlower = int(0)
xcandi = xupper//2

sumA = sum(A)

while (True):
#    print(xlower,xupper, xcandi)
    k = int(0)
    for i in range(N):
        k += max((A[i]*F[i]-xcandi+(F[i]-1))//F[i],0)
#    print(k)

    if k>K:
        xlower = xcandi
        xcandi = ((xcandi + xupper) // 2)+1
    else:
        if xupper-xlower<=1:
            ans = xcandi
            break
        else:
            xupper = xcandi
            xcandi = (xcandi + xlower) // 2

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = []
N, K =LI()
A = LI()
F = LI()
A.sort(reverse=True)
F.sort()

disA = [0]*N
for i in range(N-1):
    disA[i] = A[i] - A[i+1]
disA[N-1] = A[-1]

sumA = sum(A)
#print(A)
k = int(-1)
cou = sumA
while k<N:
    if cou<=K:
        break
    k +=1
    cou = cou - disA[-k-1]*(N-k)

basic = A[-k-1]
#print(k)
for i in range(N-k-1):
    A[i] = basic
K = K - cou
#print(K)
for i in range(K):
    A[i]-=1
#print(A)
A.sort(reverse=True)

for i in range(N):
    ans.append(A[i]*F[i])
#print(A)
#print(F)
#print(ans)
if k ==-1:
    print("0")
else:
    print(max(ans))
Problem p02885 (python)

Fixed submission:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
A,B = LI()
ans = int(0)

ans = max(A-2*B,0)


print(ans)

Buggy submission (verdict: Runtime Error):
#################################
##ワーシャルフロイド法 n~3
#################################
def warshall_floyd(n,d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
##############################
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra,floyd_warshall
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)
N,M,L =LI()
s = []
t = []
dp = [[inf] * (N) for _ in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c
Q = I()
for i in range(Q):
    a,b = LI()
    s.append(a-1)
    t.append(b-1)

for i in range(N):
    dp[i][i] = 0

#print(warshall_floyd(N,dp))
#d1 = [[0] * (N) for _ in range(N)]
#d1 = warshall_floyd(N,dp)
G = csgraph_from_dense(dp, null_value=10**9)
d1 = floyd_warshall(G)

newdp = [[inf] * (N) for _ in range(N)]
for i in range(N):
    for j in range(i+1,N):
        if d1[i][j]<=L:
            newdp[i][j] = 1
            newdp[j][i] = 1

for i in range(N):
    newdp[i][i] = 0
#print(newdp)
#print(warshall_floyd(N,newdp))
#d2 = [[0] * (N) for _ in range(N)]
#d2 = warshall_floyd(N,newdp)
G = csgraph_from_dense(newdp, null_value=10**9)
d2 = floyd_warshall(G)

for i in range(Q):
    ans = d2[s[i]][t[i]]-1
    if ans!= inf:
        print(int(ans))
    else:
        print("-1")
Problem p02924 (python)

Fixed submission:
N = int(input())

ans = int(0)

ans = (N-1)*N//2

print(ans)

Buggy submission (verdict: Runtime Error):
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N = L()
ans = int(0)

ans = N*(N-1)//2
print(ans)
Problem p02936 (python)

Fixed submission:

N, Q = map(int, input().split())
a = []
p = []
x = [0]*(N)
A = [0]*(N)

for i in range(N-1):
    t=list(map(int,input().split()))
    if t[0]>t[1]:
        swap = t[1]
        t[1] = t[0]
        t[0] =swap
    a.append((int(t[0]),int(t[1])))
for i in range(Q):
    t=list(map(int,input().split()))
    p.append((int(t[0]),int(t[1])))

a =sorted(a, key=lambda x: x[0])
p =sorted(p, key=lambda x: x[0])

#print(a)
for i in range(Q):
    x[p[i][0]-1] += p[i][1]
for i in range(N):
    A[i] = x[i]
for i in range(N-1):
    La = a[i][0]-1
    Lb = a[i][1]-1

    A[Lb] = A[La]+x[Lb]

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

Buggy submission (verdict: Runtime Error):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N, Q = LI()
P = [0] * N
V = [[] for _ in range(N)]

w = [0]*N
for i in range(N-1):
    A, B = LI()
    V[A-1].append(B-1)
    V[B-1].append(A-1)
#    AB[i] = LI()
for i in range(Q):
    p, x = LI()
    P[p-1] += x

#print(V)
for i in range(N):
    w[i] += P[i]

#i = int(0)
#for l in V:
#   for j in l:
#        print(i, j)
#        w[j] += w[i]
#        print(w)
#    i +=1
def solve(v,i):
#    print(v,i)
#    print(w)
#    print("#####")
    if not v[i]:
        return
    else:
        for l in v[i]:
            w[l] += w[i]
            v[l].remove(i)
            solve(v,l)
        return
i =int(0)
solve(V,i)

print(" ".join(map(str,w)))
Problem p02941 (python)

Fixed submission:
def examA():
    S = SI(); N = len(S)
    prev = S[0]; cur = ""
    k = 0; ans = 1
    while(k<N-1):
        k +=1
        cur += S[k]
        if cur==prev:
            continue
        prev = cur
        cur = ""
        ans +=1
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    return

def examC():
    N = I()
    A = LI()
    B = LI()
    ans = 0
    que = []
    for i in range(N):
        heappush(que,(-B[i],i))
    flag = True
    while(que):
        b,i = heappop(que)
        b *=(-1)
        if b == A[i]:
            continue
        a, c = B[(i - 1) % N], B[(i + 1) % N]
        if b - A[i] < a + c:
            flag = False
            break
        r = (b - A[i]) // (a + c)
        b -= (a + c) * r
        B[i] = b
        ans += r
        if b != A[i]:
            heapq.heappush(que, (-b, i))
#    print(que)
#    print(K)
    if flag:
        print(ans)
    else:
        print(-1)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examC()

Buggy submission (verdict: Runtime Error):
def examA():
    S = SI(); N = len(S)
    prev = S[0]; cur = ""
    k = 0; ans = 1
    while(k<N-1):
        k +=1
        cur += S[k]
        if cur==prev:
            continue
        prev = cur
        cur = ""
        ans +=1
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    return

def examC():
    N = I()
    A = LI()
    B = LI()
    ans = 0
    que = []
    for i in range(N):
        que.heappush(que,(-B[i],i))
    flag = True
    while(que):
        b,i = heappop(que)
        b *=(-1)
        if b == A[i]:
            continue
        a, c = B[(i - 1) % N], B[(i + 1) % N]
        if b - A[i] < a + c:
            flag = False
            break
        r = (b - A[i]) // (a + c)
        b -= (a + c) * r
        B[i] = b
        ans += r
        if b != A[i]:
            heapq.heappush(que, (-b, i))
#    print(que)
#    print(K)
    if flag:
        print(ans)
    else:
        print(-1)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examC()
Problem p02949 (python)

Fixed submission:
#####コピペ
def BellmanFord(edges, num_v, source):
    # グラフの初期化
    inf = float("inf")
    dist = [-inf for i in range(num_v)]
    dist[source - 1] = 0

    # 辺の緩和
    buf = 0
    for i in range(num_v * 2):
        for edge in edges:
            if dist[edge[1] - 1] < dist[edge[0] - 1] + edge[2]:
                dist[edge[1] - 1] = dist[edge[0] - 1] + edge[2]
                if i >= num_v:
                    dist[edge[1] - 1] = inf
        if i == num_v - 1:
            buf = dist[-1]
    if dist[-1] != buf or dist[-1] == -inf:
        return -1
    return max(dist[-1], 0)
##########################################################################
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = []

N, M, P = LI()
cou = int(0)
maxC = 10000
d = [[float("inf") for i in range(N)] for i in range(N)]
#d[u][v] : 辺uvのコスト(存在しないときはinf)
V = []*M
for i in range(M):
    a, b, c = list(map(int,input().split()))
    V.append((a,b,c-P))
print(BellmanFord(V, N, 1))

#print(ans)

Buggy submission (verdict: Runtime Error):
#####コピペ
def BellmanFord(edges, num_v, source):
    # グラフの初期化
    inf = float("inf")
    dist = [-inf for i in range(num_v)]
    dist[source - 1] = 0

    # 辺の緩和
    buf = 0
    for i in range(num_v * 2):
        for edge in edges:
            if dist[edge[1] - 1] < dist[edge[0] - 1] + edge[2]:
                dist[edge[1] - 1] = dist[edge[0] - 1] + edge[2]
                if i >= num_v:
                    dist[edge[1] - 1] = inf
        if i == num_v - 1:
            buf = dist[-1]
    if dist[-1] != buf or dist[-1] == -inf:
        return -1
    return max(dist[-1], 0)
##########################################################################
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra,floyd_warshall
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = []

N, M, P = LI()
cou = int(0)
maxC = 10000
d = [[float("inf") for i in range(N)] for i in range(N)]
#d[u][v] : 辺uvのコスト(存在しないときはinf)
V = []*M
for i in range(M):
    a, b, c = list(map(int,input().split()))
    V.append((a,b,c-P))
print(BellmanFord(V, N, 1))


#print(ans)
Problem p02999 (python)

Fixed submission:
X, A = list(map(int,input().split()))
print(0) if X<A else print(10)

Buggy submission (verdict: Runtime Error):
X, A = list(map(int(input().split()))
print(0) if X<A else print(10)
Problem p03006 (python)

Fixed submission:
def examB():
    N = I()
    X = [0]*N; Y = [0]*N
    for i in range(N):
        X[i],Y[i] = LI()
    pq = defaultdict(int)
    for i in range(N):
        for j in range(i+1,N):
            curx = X[j]-X[i]
            cury = Y[j]-Y[i]
            pq[curx,cury] +=1
            pq[-curx, -cury] += 1
    if N>1:
        ans = N - max(pq.values())
    else:
        ans = 1
    print(ans)

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examB()

Buggy submission (verdict: Runtime Error):
def examB():
    N = I()
    X = [0]*N; Y = [0]*N
    for i in range(N):
        X[i],Y[i] = LI()
    pq = defaultdict(int)
    for i in range(N):
        for j in range(i+1,N):
            curx = X[j]-X[i]
            cury = Y[j]-Y[i]
            pq[curx,cury] +=1
    ans = N - max(pq.values())
    print(ans)

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examB()
Problem p03007 (python)

Fixed submission:
def examC():
    N = I()
    A = LI()
    negative = []; positive = []
    ans = 0; ansC = []
    for i in A:
        if i<0:
            negative.append(i)
        else:
            positive.append(i)
    if negative==[]:
        positive.sort()
        if N>2:
            cur = positive[0] - positive[1]
            ansC.append([positive[0], positive[1]])
            ans = sum(positive) - 2 * positive[0]
            for i in range(2, N - 1):
                ansC.append([cur, positive[i]])
                cur -= positive[i]
            ansC.append([positive[N - 1], cur])
        else:
            ansC.append([positive[1],positive[0]])
            ans = positive[1]-positive[0]

    elif positive==[]:
        negative.sort(reverse=True)
        cur = negative[0] - negative[1]
        ansC.append([negative[0],negative[1]])
        ans = -sum(negative)+2*negative[0]
        for i in range(2,N):
            ansC.append([cur,negative[i]])
            cur -=negative[i]
    else:
        positive.sort()
        negative.sort(reverse=True)
        ans = sum(positive) - sum(negative)
        curN = negative[0]
        curP = positive[0]
        for i in range(len(positive)-1):
            ansC.append([curN,positive[i+1]])
            curN -=positive[i+1]
        for i in range(len(negative)-1):
            ansC.append([curP,negative[i+1]])
            curP -=negative[i+1]
        ansC.append([curP,curN])

    print(ans)
    for v in ansC:
        print(" ".join(map(str,v)))

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [0]*N
A = list(map(int, input().split()))
ans = int(0)
x = int(0)
y = int(0)

As = sorted(A)

if As[0]>0:
    for i in range(N):
        ans += As[i]

    ans -=As[0]*2
    print(ans)

    x = As[0]
    for i in range(N-2):
        y = As[i+1]
        print(x,y)
        x = x - y

    y = x
    x = As[N-1]
    print(x,y)

elif As[N-1]<0:
    for i in range(N):
        As[i] *= -1
    for i in range(N):
        ans += As[i]

    ans -=As[N-1]*2
    print(ans)

    As = sorted(As)

    x = As[0]
    for i in range(N-2):
        y = As[i+1]
        print(-x,-y)
        x = x - y

    y = x
    x = As[N-1]
    print(-y,-x)

##################################
else:
    for i in range(N):
        ans += abs(As[i])

    print(ans)

    x = As[0]
    i = int(0)
    while(i<N-2 and A[N-i-1]>0):
        y = As[N-i-1]
        print(x,y)
        x = x - y
        i +=1
#    print("ok")
    y = x
    while(i<N-2):
        x = As[i]
        print(x,y)
        y = x - y
        i +=1


    x = As[N-i]
    print(x,y)
Problem p03039 (python)

Fixed submission:
def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

N,M,K = map(int,input().split())

C = comb(N*M-2, K-2)
#print(int(C))

A = int(0)
for d in range(N):
    a = (N-d)*(M**2)

    A += int(d*a)

B = int(0)
for d in range(M):
    b = (M-d)*(N**2)
    B += int(d*b)

ans = int((A+B)*C)
ans = ans%(1000000007)
print(ans)

Buggy submission (verdict: Runtime Error):
class combination():
    #素数のmod取るときのみ　速い
    def __init__(self, n, mod):
        self.n = n
        self.fac = [1] * (n + 1)
        self.inv = [1] * (n + 1)
        for j in range(1, n + 1):
            self.fac[j] = self.fac[j - 1] * j % mod

        self.inv[n] = pow(self.fac[n], mod - 2, mod)
        for j in range(n - 1, -1, -1):
            self.inv[j] = self.inv[j + 1] * (j + 1) % mod

    def comb(self, n, r, mod):
        if r > n or n < 0 or r < 0:
            return 0
        return self.fac[n] * self.inv[n - r] * self.inv[r] % mod


import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, M, K = LI()

cou = int(0)
#maxL = N + M -2
L = int(0)
#距離i+1の組み合わせ

#for i in range(maxL):
for j in range(N):
    L += (N - j - 1) * (M ** 2) * (j + 1)
    L %= mod
for j in range(M):
    L += (M - j - 1) * (N ** 2) * (j + 1)
    L %= mod
#    for j in range(i+2):
#        if i>1 and i+1>j>0:
#            L[i] += max((N-j)*(M-(1+i)+j),0)*(i+1)*2
#        else:
#            L[i] += max(((N - j) * (M - (1 + i) + j)), 0) * (i + 1)
C = combination(N*M-2,mod)
cou = C.comb(N*M-2,K-2)*L%mod

print(cou)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb

N,M,K = map(int,input().split())

C = comb(N*M-2, K-2)
print(int(C))

A = int(0)
for d in range(N):
    a = (N-d)*(M**2)

    A += int(d*a)

B = int(0)
for d in range(M):
    b = (M-d)*(N**2)
    B += int(d*b)

ans = int((A+B)*C)
print(ans)
Problem p03047 (python)

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

ans = int(N-K+1)

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
K = int(input())
 
ans = int(N-K+1)

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
K = int(input())
 
ans = int(N-K+1)
 
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
K = int(input())
 
ans = int(N-K+1)
 
print ans

Buggy submission (verdict: Runtime Error):
N = int(input())
K = int(input())
 
ans = int(N-K+1)
 
print(ans)

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

ans = int(N-K+1)

print(ans)
Problem p03048 (python)

Fixed submission:
R,G,B,N = map(int, input().split())

ans = int(0)
x = int(N//R)

for i in range(x+1):
    t = N-i*R
    y = int(t//G)
    for j in range(y+1) :
        s = int(t-G*j)
        if s%B==0 : ans +=1

print(ans)

Buggy submission (verdict: Runtime Error):
R = int(input())
G = int(input())
B = int(input())

N = int(input())

ans = int(0)
x = int(N//R)
y = int(N//G)
z = int(N//B)

for i in range(x+1):
    for j in range(y+1) :
        s = N-R*i-G*j
        if s%B==0 and s>=0 : ans +=1

print(ans)
Problem p03160 (python)

Fixed submission:
N = int(input())
h = list(map(int, input().split()))
dp = [0]*N
ans = int(0)
dp[1] = abs(h[1]-h[0])


for i in range(2,N):
    dp[i] = min(dp[i-1] + abs(h[i]-h[i-1]),dp[i-2] + abs(h[i]-h[i-2]))

ans = dp[N-1]

print(ans)

Buggy submission (verdict: Runtime Error):

N, W = map(int, input().split())
w, v = [0] * N, [0] * N
for i in range(N):
	w[i], v[i] = map(int, input().split())

dp = [[0] * (W + 1) for _ in range(N + 1)]
# dp[i][j](j=0,1,...,W)の値が求まっている状態で、dp[i+1][j]を更新することを考える
for i in range(N):
	for j in range(W + 1):
		# 品物(w[i],v[i])を選ぶとき、j-w[i]>=0に限って、価値v[i]が加算される
		if j - w[i] >= 0:
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i])
		# 品物(w[i],v[i])を選ばないとき、何も加算しない
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
print(dp[N][W])
Problem p03161 (python)

Fixed submission:
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
ans = int(0)
c = int(0)
if N < K:
    c = N-1
else:
    c = K
for i in range(c):
    dp[i+1] = abs(h[i+1]-h[0])

for i in range(2,N):
    if i<K:
        c = i
    else:
        c = K
    dpcandi = [0] * c
    for j in range(c):
        dpcandi[j] = dp[i-j-1] + abs(h[i]-h[i-j-1])
    dp[i] = min(dpcandi)

ans = dp[N-1]

print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
ans = int(0)
for i in range(K):
    dp[i+1] = abs(h[i+1]-h[0])

for i in range(2,N):
    if i<K:
        c = i
    else:
        c = K
    dpcandi = [0] * c
    for j in range(c):
        dpcandi[j] = dp[i-j-1] + abs(h[i]-h[i-j-1])
    dp[i] = min(dpcandi)

ans = dp[N-1]

print(ans)