Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import numpy as np


def parse():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))}
    return k, pc


def split_simple():
    k, pc = parse()
    lst = [[]]
    lst[0].append(pc.pop(0))
    while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
    return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst]


def inner(n, k, s, sim):
    if k < n or s <= 0:
        return sim.max()
    else:
        div = k // n
        mod = k % n
        return s * div + sim[:, :mod].max()


def main():
    k, data_set = split_simple()
    result = min([d.min() for d in data_set])
    for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        sim = np.array([[rep[i:i+p].sum()
                         for p in range(1, min(k, n) + 1)] for i in range(n)])
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
    print(result)


if __name__ == "__main__":
    main()


Fixed submission:
import numpy as np


def parse():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    pc = {i: (pi - 1, ci) for i, (pi, ci) in enumerate(zip(p, c))}
    return k, pc


def split_simple():
    k, pc = parse()
    lst = [[]]
    lst[0].append(pc.pop(0))
    while True:
        if not pc:
            break
        p, _ = lst[-1][-1]
        if p in pc:
            pcj = pc.pop(p)
            lst[-1].append(pcj)
        else:
            j = next(iter(pc.keys()))
            pcj = pc.pop(j)
            lst.append([pcj])
    return k, [np.array([ls[1] for ls in lst_], dtype=np.long) for lst_ in lst]


def inner(n, k, s, sim):
    # print(sim)
    # print(s)
    if sim.shape[1] == 1:
        return sim.max()
    if k < n or s <= 0:
        return sim.max()
    div = k // n
    mod = k % n
    # print(k, n, div, mod)
    if mod == 0:
        return max(s * div, s * (div - 1) + sim.max())
    try:
        return max(s * div + sim[:, :mod].max(), s * (div - 1) + sim[:, :mod].max())
    except ValueError:
        print(mod)
        print(sim)
        print(sim.shape)
        raise


def main():
    k, data_set = split_simple()
    result = min([d.min() for d in data_set])
    for data in data_set:
        n = data.size
        rep = np.repeat(data[None], 2, axis=0).flatten()
        # sim_ = np.array([[rep[i:i+p].sum()
        #                  for p in range(1, min(k, n) + 1)] for i in range(n)])
        sim = np.array([np.cumsum(rep[i:i+min(k, n)]) for i in range(n)])
        # print((sim == sim_).all())
        cand = inner(n, k, data.sum(), sim)
        result = max(result, cand)
    print(result)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import numpy as np

N, M = map(int, input().split())
A = np.full((N, M), 0)
for i in range(N):
    Ka = map(int, input().split())
    a = Ka[1:]
    for j in a:
        A[i, j-1] = 1
        
print(np.sum(np.prod(A, axis=0)))
        

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

like = [1] * M

for i in range(N):
    K, *A = map(int, input().split())
    for i in range(M):
        if i + 1 not in A:
            like[i] = 0
print(sum(like))
Buggy python submission (verdict: Runtime Error):
import numpy as np

N = input()
A = np.fromiter(map(int, input().split()), dtype=np.int64)
print(np.gcd.reduce(A))

Fixed submission:
import numpy as np
from functools import reduce

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)


N = input()
A = list(map(int, input().split()))
print(reduce(gcd, A, A[0]))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))

c = [None] * len(h)
c[0] = 0
c[1] = abs(h[0] - h[1])
for i in range(2, len(c)):
    c[i] = min(c[i-k] + abs(h[i-k] - h[i]) for k in range(1, min(i+1, K+1)))
print(c[-1])

Fixed submission:
import sys
import numpy as np
def frog2(h, K):
    c = np.full_like(h, 0)
    c[0] = 0
    c[1] = abs(h[0] - h[1])
    for i in range(2, len(c)):
        k = min(i, K)
        c[i] = np.min(c[i-k:i] + abs(h[i-k:i] - h[i]))
        # c[i] = min(c[i-k] + abs(h[i-k] - h[i]) for k in range(1, min(i+1, K+1)))
    return c[-1]


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

print(frog2(h, K))

Buggy python submission (verdict: Time Limit Exceeded):


def frog2(h, K):
    c = [None] * len(h)
    c[0] = 0
    c[1] = abs(h[0] - h[1])
    for i in range(2, len(c)):
        c[i] = min(c[i-k] + abs(h[i-k] - h[i]) for k in range(1, min(i+1, K+1)))
    return c[-1]


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

Fixed submission:
import sys
import numpy as np
def frog2(h, K):
    c = np.full_like(h, 0)
    c[0] = 0
    c[1] = abs(h[0] - h[1])
    for i in range(2, len(c)):
        k = min(i, K)
        c[i] = np.min(c[i-k:i] + abs(h[i-k:i] - h[i]))
        # c[i] = min(c[i-k] + abs(h[i-k] - h[i]) for k in range(1, min(i+1, K+1)))
    return c[-1]


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

print(frog2(h, K))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

def frog2(h, K):
    c = np.full_like(h, 0)
    c[0] = 0
    c[1] = abs(h[0] - h[1])
    for i in range(2, len(c)):
        c[i] = min(c[i-k] + abs(h[i-k] - h[i]) for k in range(1, min(i+1, K+1)))
    return c[-1]


N, K = map(int, input().split())
h = np.array(list(map(int, input().split())))
print(frog2(h, K))

Fixed submission:
import sys
import numpy as np
def frog2(h, K):
    c = np.full_like(h, 0)
    c[0] = 0
    c[1] = abs(h[0] - h[1])
    for i in range(2, len(c)):
        k = min(i, K)
        c[i] = np.min(c[i-k:i] + abs(h[i-k:i] - h[i]))
        # c[i] = min(c[i-k] + abs(h[i-k] - h[i]) for k in range(1, min(i+1, K+1)))
    return c[-1]


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

print(frog2(h, K))
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
seen = set()
circuits = []

for x in range(N):
    cir = []
    point = 0
    while True:
        if x in seen: 
            if cir != []: circuits.append((cir,point))
            break
        seen.add(x)
        point += C[x]
        cir.append(x)
        x = P[x] - 1
ans = -1000000000000
for x in circuits:
    tmp = 0
    if x[1] > 0 and K >= len(x[0]) * 2: 
        tmp = (K // len(x[0]) - 1) * x[1]
        r = K % len(x[0]) + len(x[0])
    else:
        r = K
    for y in x[0]:
        tmp2 = 0
        for _ in range(r):
            y = P[y] - 1
            tmp2 += C[y]
            ans = max(ans, tmp + tmp2)
print(ans)



Fixed submission:
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
seen = set()
circuits = []

for x in range(N):
    cir = []
    point = 0
    while True:
        if x in seen: 
            if cir != []: circuits.append((cir,point))
            break
        seen.add(x)
        point += C[x]
        cir.append(x)
        x = P[x] - 1
ans = -1000000000000
for x in circuits:
    tmp = 0
    if  K >= len(x[0]) * 2: 
        if x[1] > 0: tmp = (K // len(x[0]) - 1) * x[1]
        r = K % len(x[0]) + len(x[0])
    else:
        r = K
    for y in x[0]:
        tmp2 = 0
        for _ in range(r):
            y = P[y] - 1
            tmp2 += C[y]
            ans = max(ans, tmp + tmp2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
seen = set()
circuits = []

for x in range(N):
    cir = []
    point = 0
    while True:
        if x in seen: 
            if cir != []: circuits.append((cir,point))
            break
        seen.add(x)
        point += C[x]
        cir.append(x)
        x = P[x] - 1
ans = -1000000000000
for x in circuits:
    tmp = 0
    if x[1] > 0 and K >= len(x[0]) * 2: 
        tmp = (K // len(x[0]) - 1) * x[1]
        r = K % len(x[0]) + len(x[0])
    else:
        r = K
    for y in x[0]:
        tmp2 = 0
        for _ in range(r):
            y = P[y] - 1
            tmp2 += C[y]
            ans = max(ans, tmp + tmp2)
print(ans)



Fixed submission:
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
seen = set()
circuits = []

for x in range(N):
    cir = []
    point = 0
    while True:
        if x in seen: 
            if cir != []: circuits.append((cir,point))
            break
        seen.add(x)
        point += C[x]
        cir.append(x)
        x = P[x] - 1
ans = -1000000000000
for x in circuits:
    tmp = 0
    if  K >= len(x[0]) * 2: 
        if x[1] > 0: tmp = (K // len(x[0]) - 1) * x[1]
        r = K % len(x[0]) + len(x[0])
    else:
        r = K
    for y in x[0]:
        tmp2 = 0
        for _ in range(r):
            y = P[y] - 1
            tmp2 += C[y]
            ans = max(ans, tmp + tmp2)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

div = [0 for _ in range(10**7+5)];

for i in range(1,10**7+1):
    j = 1
    while i * j <= N:
        div[i * j] += 1
        j += 1

ans = 0

for i in range(1,N+1):
    ans += div[i] * i
print(ans)


Fixed submission:
N = int(input())

div = [0 for _ in range(10**7+5)];

for i in range(1,10**7+1):
    j = 1
    while i * j <= N:
        div[i * j] += 1
        j += 1

ans = 0

for i in range(1,N+1):
    ans += div[i] * i
print(ans)
Buggy python submission (verdict: Runtime Error):
N , M ,X = map(int,input().split())
L = []
for _ in range(N):
    l = list(map(int,input().split()))
    L.append(l)
ans = []
for i in range(2 ** N):
    rikai = [0 for _ in range(M)]
    cost = 0
    for b in range(N):
        if i  & (1 <<  b):
            for x in range(1,M + 1):
                rikai[x-1] += L[b][x] 
            cost += L[b][0]
    for x in rikai:
        if x < X: break
    else:
        ans.append(cost)

print(min(ans))


Fixed submission:
N , M ,X = map(int,input().split())
L = []
for _ in range(N):
    l = list(map(int,input().split()))
    L.append(l)
ans = []
for i in range(2 ** N):
    rikai = [0 for _ in range(M)]
    cost = 0
    for b in range(N):
        if i  & (1 <<  b):
            for x in range(1,M + 1):
                rikai[x-1] += L[b][x] 
            cost += L[b][0]
    for x in rikai:
        if x < X: break
    else:
        ans.append(cost)
try:
    print(min(ans))
except:
    print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K -= L.index(A[now - 1])
        if K >= 0: K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]
print(L[K + L.index(A[now - 1])])
if len(L) == 1:
    while True:
        print("hoge")



Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K -= L.index(A[now - 1])
        if K >= 0: K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]
#print(L)
#print(K)
print(L[K + L.index(A[now - 1])])
Buggy python submission (verdict: Runtime Error):
x = int(input())
import math
for A in range(-10**5,10**5):
    A5 = A ** 5
    B = round(math.pow(x - A5,1/5),0)
    if  A5 == x + int(B) ** 5:
        print(A,int(B))
        quit()


Fixed submission:
x = int(input())
import math
f = 1
for A in range(-10**5,10**5):
    A5 = A ** 5
    if x - A5 > 0:
        f = -1
        B = round(math.pow(x - A5,1/5),0)
    else:
        B = round(math.pow(-(x - A5),1/5),0)
    if  A5 == x + int(B*f) ** 5:
        print(A,int(B)*f)
        quit()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = map(int,input().split())
A = sorted([(b,a) for a, b in enumerate(a)])[::-1]
dp = [[-1 for _ in range(N+10)]for _ in range(N+10)]
#dp[x][y]: 活発度順でx+y人詰めた時,左にx人右にy人詰めたときの最大値
dp[0][0] = 0
import sys
sys.setrecursionlimit(100000000)

def memo(x, y):
    if x < 0 or y < 0: return -1000000000000 #ありえん
    if dp[x][y] != -1: return dp[x][y]
    val, pre = A[x+y-1][0], A[x+y-1][1]
    dp[x][y] = max(
            #x-1からくるパターン
            memo(x - 1,y) + abs(pre - (x - 1)) * val,
            #y-1からくるパターン
            memo(x, y - 1) + abs(pre - (N - y )) *val,
            )
    return dp[x][y]
ans = 0
for i in range(N): ans = max(ans, memo(i,N - i))
print(ans)


Fixed submission:
N = int(input())
a = map(int,input().split())
A = sorted([(b,a) for a, b in enumerate(a)])[::-1]
dp = [[-1 for _ in range(N+10)]for _ in range(N+10)]
#dp[x][y]: 活発度順でx+y人詰めた時,左にx人右にy人詰めたときの最大値
dp[0][0] = 0
import sys
sys.setrecursionlimit(100000000)

def memo(x, y):
    if x < 0 or y < 0: return -1000000000000 #ありえん
    if dp[x][y] != -1: return dp[x][y]
    val, pre = A[x+y-1][0], A[x+y-1][1]
    dp[x][y] = max(
            #x-1からくるパターン
            memo(x - 1,y) + abs(pre - (x - 1)) * val,
            #y-1からくるパターン
            memo(x, y - 1) + abs(pre - (N - y )) *val,
            )
    return dp[x][y]
ans = 0
for i in range(N): ans = max(ans, memo(i,N - i))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
d = deque()
length = 0
for x in input():
    d.append(x)
    length += 1
Q = int(input())
f = 0
for _ in range(Q):
    l = list(input().split())
    if l[0] == '1':
        f ^= 1
    else:
        length += 1
        if f == 0: 
            if l[1] == '2': d.append(l[2])
            else: d.appendleft(l[2])
        else:
            if l[1] == '2': d.appendleft(l[2])
            else: d.append(l[2])
ans = ''
for x in range(length):
    ans += d.pop()
if f == 0:
    print(ans[::-1])    
else:
    print(ans)


Fixed submission:
from collections import deque
d = deque()
length = 0
for x in input():
    d.append(x)
    length += 1
Q = int(input())
f = 0
for _ in range(Q):
    l = list(input().split())
    if l[0] == '1':
        f ^= 1
    else:
        length += 1
        if f == 0: 
            if l[1] == '2': d.append(l[2])
            else: d.appendleft(l[2])
        else:
            if l[1] == '2': d.appendleft(l[2])
            else: d.append(l[2])
ans = ''
for x in range(length):
    ans += d.pop()
if f == 0:
    print(ans[::-1])    
else:
    print(ans)
Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
S = []
for i in range(M):
    s = tuple(map(int,input().split()))
    S.append(s)
ans = [-1 for _ in range(M)]

for x in S:
    if ans[x[0]-1] == -1 or ans[x[0]-1] == x[1]:
        ans[x[0]-1] = x[1]
    else:
        print(-1)
        exit()
if N > 1 and ans[0] == 0:
    print(-1)
    exit()

for i in range(len(ans)):
    if ans[i] == -1 :    
        if i == 0:
            print('1',end='')
        else:
            print('0',end='')
    else:
        print(ans[i],end='')
print()




Fixed submission:
N , M = map(int,input().split())
L = []
for _ in range(M):
    m = tuple(map(int,input().split()))
    L.append(m)
if N == 1:
    for ans in range(0,10):
        ans = str(ans)
        for x in L:
            if ans[x[0] - 1] != str(x[1]):
                break
        else:
            print(ans)
            exit()
    print(-1)
    exit()
else:
    for ans in range(10 ** (N - 1),10 ** N ):
        ans = str(ans)
        for x in L:
            if ans[x[0] - 1] != str(x[1]):
                break
        else:
            print(ans)
            exit()
    print(-1)
    exit()

Buggy python submission (verdict: Runtime Error):
N , M = map(int,input().split())
L = []
for _ in range(M):
    m = tuple(map(int,input().split()))
    L.append(m)
if N == 1:
    for ans in range(0,10):
        ans = str(ans)
        for x in L:
            if ans[x[0] - 1] != str(x[1]):
                break
        else:
            eixt()
    print(-1)
    exit()
else:
    for ans in range(10 ** (N - 1),10 ** N ):
        ans = str(ans)
        for x in L:
            if ans[x[0] - 1] != str(x[1]):
                break
        else:
            print(ans)
            exit()
    print(-1)
    exit()



Fixed submission:
N , M = map(int,input().split())
L = []
for _ in range(M):
    m = tuple(map(int,input().split()))
    L.append(m)
if N == 1:
    for ans in range(0,10):
        ans = str(ans)
        for x in L:
            if ans[x[0] - 1] != str(x[1]):
                break
        else:
            print(ans)
            exit()
    print(-1)
    exit()
else:
    for ans in range(10 ** (N - 1),10 ** N ):
        ans = str(ans)
        for x in L:
            if ans[x[0] - 1] != str(x[1]):
                break
        else:
            print(ans)
            exit()
    print(-1)
    exit()
Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce
MOD = 10 ** 9 + 7
def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom 
N,K = map(int,input().split())
for i in range(1,1+K):
    ans = combinations_count(1+N-K,i)  % MOD
    ans *= combinations_count(K-1,k-i) % MOD
    print(ans%MOD)


Fixed submission:
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


N,K = map(int,input().split())
for i in range(1,1+K):
    ans = cmb(1+N-K,i,mod)
    ans *= cmb(K-1,K-i,mod)
    print(ans%mod)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
path = [[] for _ in range(N*3+3)]
for _ in range(M):
    u,v = map(int,input().split())
    path[u].append(v+N)
    path[u+N].append(v+N+N)
    path[u+N+N].append(v)

S,T = map(int,input().split())
import collections

q = collections.deque()
vis = [-1 for _ in range(N*3+3)]
def bfs():
    while len(q) > 0:
        now,cnt = q.popleft()
        #print(now,T,cnt,path[now],q)
        vis[now] = cnt
        if now == T: 
            return cnt
        for x in path[now]:
            if vis[x] == -1:
                q.append((x,cnt+1))
vis[S] = 1
q.append([S,0])
ans = bfs()
if vis[T] == -1: print(-1)
else: print(ans//3)




Fixed submission:
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


N,K = map(int,input().split())
for i in range(1,1+K):
    ans = cmb(1+N-K,i,mod)
    ans *= cmb(K-1,K-i,mod)
    print(ans%mod)
Buggy python submission (verdict: Runtime Error):
if B / A >= C:
  print(C)
else:
  print(int(B/A))

Fixed submission:
A, B, C = map(int ,input().split())
if B / A >= C:
  print(C)
elif B/ A < C:
  print(int(B/A))
Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
cnt = 0


while True:
    for i in range(len(s)-1):
        if s[i] + s[i+1] == "01" or s[i] + s[i+1] == "10":
            del s[i]
            del s[i]
            cnt += 2
            break
    else:
        break
print(cnt)


Fixed submission:
s = list(input())
cnt = 0

def counter(array):
    from collections import Counter
    return list(Counter(array).most_common())
c = counter(s)
if len(c) == 2:
    cnt = min(c[0][1],c[1][1])
print(cnt*2)
Buggy python submission (verdict: Runtime Error):
N = int(input())
L = list(map(int,inptut().split()))
while True:
  if len(L) == 1:
    print(L[0])
    quit()
  L = sorted(L)
  m = L[0]
  for i in range(1,len(L)):
    L[i] %= m
  L = list(filter(lambda x:x!=0,L))  
  

Fixed submission:
N = int(input())
L = list(map(int,input().split()))
while True:
  if len(L) == 1:
    print(L[0])
    quit()
  L = sorted(L)
  m = L[0]
  for i in range(1,len(L)):
    L[i] %= m
  L = list(filter(lambda x:x!=0,L))
Buggy python submission (verdict: Runtime Error):
diN, M = map(int,input().split())
L = sorted(list(map(int,input().split())))
distance = []
for i in range(len(L)-1):
    distance.append(abs(L[i]-L[i+1]))
distance = sorted(distance)
for _ in range(N-1):
    if len(distance) > 0:
        del distance[-1]
    else:
        print(0)
        quit()
print(sum(distance))


Fixed submission:
N, M = map(int,input().split())
L = sorted(list(map(int,input().split())))
distance = []
for i in range(len(L)-1):
    distance.append(abs(L[i]-L[i+1]))
distance = sorted(distance)
for _ in range(N-1):
    try:
        del distance[-1]
    except:
        print(0)
        quit()
print(sum(distance))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
H = list(map(int,input().split()))

#DP[i] i番目までの足場を見たときの足場iにたどり着く最小値

DP = [float('inf') for _ in range(N + 1)] 
DP[0] = 0
for i in range(N):
    for k in range(1,K + 1):
        if 0 <= i - k and i - k < N:
            cost_min = float('inf')
            cost_min = min(cost_min, DP[i - k] +  abs(H[i] - H[i - k]))
            DP[i] = min(DP[i],cost_min)
print(DP[N - 1])



Fixed submission:
N, K  = map(int,input().split())
L = list(map(int,input().split()))
dp = [10**10 for _ in range(N+2)]
dp[0] = L[0]
for i in range(N-1):
    for k in range(1,1+K):
        try:
            dp[i+k] = min(dp[i]+abs(L[i]-L[i+k]),dp[i+k])
        except:
            pass
print(dp[N-1]-dp[0])

Buggy python submission (verdict: Time Limit Exceeded):
N, K  = map(int,input().split())
L = list(map(int,input().split()))
dp = [10**10 for _ in range(N+2)]
dp[0] = L[0]
for i in range(N-1):
    for k in range(1,1+K):
        try:
            dp[i+k] = min(dp[i]+abs(L[i]-L[i+k]),dp[i+k])
        except:
            pass
print(dp[N-1]-dp[0])



Fixed submission:
N, K  = map(int,input().split())
L = list(map(int,input().split()))
dp = [10**10 for _ in range(N+2)]
dp[0] = L[0]
for i in range(N-1):
    for k in range(1,1+K):
        try:
            dp[i+k] = min(dp[i]+abs(L[i]-L[i+k]),dp[i+k])
        except:
            pass
print(dp[N-1]-dp[0])