Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

k = int(input())
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans += gcd(a,b,c)
print(ans)


Fixed submission:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(input())
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans += gcd(a,b,c)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(input())
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans += gcd(a,b,c)
print(ans)

Fixed submission:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(input())
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans += gcd(a,b,c)
print(ans)
Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())

import numpy as np
dp = np.zeros((h,w),np.int32)
s = np.full((h,w),'.')
for H in range(h):
    S = input()
    for W in range(w):
        s[H][W] = S[W]
        own = s[H][W]
        if H==0 and W==0: dp[H][W] = 0 if own=='.' else 1
        elif H==0: dp[H][W] = dp[H][W-1]+1 if s[H][W-1]=='.' and own=='#' else dp[H][W-1]
        elif W==0: dp[H][W] = dp[H-1][W]+1 if s[H-1][W]=='.' and own=='#' else dp[H-1][W]
        else:
            dp[H][W] = min(dp[H][W-1], dp[H-1][W])
            if own=='#':
                if s[H][W-1]=='.' and s[H-1][W]=='.': dp[H][W] += 1
                elif s[H][W-1]=='#' and s[H-1][W]=='.': dp[H][W] = min(dp[H][W-1], dp[H-1][W]+1)
                elif s[H][W-1]=='.' and s[H-1][W]=='#': dp[H][W] = min(dp[H][W-1]+1, dp[H-1][W])
print(dp[h-1,w-1])                


Fixed submission:
h,w = map(int,input().split())

import numpy as np
dp = np.zeros((h,w),np.int32)
s = np.full((h,w),'.',np.str)
for H in range(h):
    S = input()
    for W in range(w):
        s[H][W] = S[W]
        own = s[H][W]
        if H==0 and W==0: dp[H][W] = 0 if own=='.' else 1
        elif H==0: dp[H][W] = dp[H][W-1]+1 if s[H][W-1]=='.' and own=='#' else dp[H][W-1]
        elif W==0: dp[H][W] = dp[H-1][W]+1 if s[H-1][W]=='.' and own=='#' else dp[H-1][W]
        else:
            dp[H][W] = min(dp[H][W-1], dp[H-1][W])
            if own=='#':
                if s[H][W-1]=='.' and s[H-1][W]=='.': dp[H][W] += 1
                elif s[H][W-1]=='#' and s[H-1][W]=='.': dp[H][W] = min(dp[H][W-1], dp[H-1][W]+1)
                elif s[H][W-1]=='.' and s[H-1][W]=='#': dp[H][W] = min(dp[H][W-1]+1, dp[H-1][W])
print(dp[h-1,w-1])
Buggy python submission (verdict: Runtime Error):
s = input()
if s=='AAA' or s='BBB':
    print('No')
else:
    print('Yes')

Fixed submission:
s = input()
if s=='AAA' or s=='BBB':
    print('No')
else:
    print('Yes')
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())
flg = False
for i in range(q):
    query = input()
    if query[0] == '1': flg = not flg
    else:
        t,f,c = query.split()
        if flg:
            s = c+s if f=='2' else s+c
        else:
            s = c+s if f=='1' else s+c
if flg: s = ''.join(list(reversed(s)))
print(s)

Fixed submission:
from collections import deque

t = input()
s = deque()
for i in range(len(t)): s.append(t[i])
q = int(input())
flg = False
for i in range(q):
    query = input()
    if query[0] == '1': flg = not flg
    else:
        t,f,c = query.split()
        if flg:
            s.appendleft(c) if f=='2' else s.append(c)
        else:
            s.appendleft(c) if f=='1' else s.append(c)
if flg: s = reversed(s)
print(''.join(s))
Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())
p = list(map(int,input().split()))

t = [0]*n
n = n-(k-1)
for i in range(n):
    for j in range(i,i+k):
        t[i] += p[j]
T = t.index(max(t))
ans = 0
for i in range(k):
    ans += (p[T+i]+1)/2
print(ans)

Fixed submission:
n,k = map(int,input().split())
p = list(map(int,input().split()))

t = [0]*n
for j in range(k):
    t[0] += p[j]
for i in range(1,n-k+1):
    t[i] = t[i-1]-p[i-1]+p[i+k-1]
T = t.index(max(t))
ans = 0
for i in range(k):
    ans += (p[T+i]+1)/2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())
p = list(map(int,input().split()))

t = [0]*n
for i in range(n-(k-1)):
    for j in range(i,i+k):
        t[i] += p[j]
T = t.index(max(t))
ans = 0
for i in range(k):
    ans += (p[T+i]+1)/2
print(ans)

Fixed submission:
n,k = map(int,input().split())
p = list(map(int,input().split()))

t = [0]*n
for j in range(k):
    t[0] += p[j]
for i in range(1,n-k+1):
    t[i] = t[i-1]-p[i-1]+p[i+k-1]
T = t.index(max(t))
ans = 0
for i in range(k):
    ans += (p[T+i]+1)/2
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
h,n = map(int,input().split())
ab = [tuple(map(int,input().split())) for _ in range(n)]
# ありえない値にしたいと仮定すると、最大値が10**7取りうるので10**7+1にする？
dp = [10**8] * (h+1)
dp[0] = 0

for i in range(1,h+1):
    for a,b in ab:
        if i >= a:
            # HPより小さいとき、減らした後のHPに対して消費するMPを足す
            atk = b + dp[i-a]
            if atk < dp[i]:
                dp[i] = atk
        elif b < dp[i]:
            # どの攻撃よりも小さいHPであれば、一番MP消費が少ない攻撃のMPを記録する
            dp[i] = b
# 配列の最後が、求めたいHP
print(dp[-1])

Fixed submission:
h,n = map(int,input().split())
ab = [tuple(map(int,input().split())) for _ in range(n)]
# ありえない値にしたいと仮定すると、最大値が10**7取りうるので10**7+1にする？
dp = [10**8] * (h+1)
dp[0] = 0

for i in range(1,h+1):
    for a,b in ab:
        if i >= a:
            # HPより小さいとき、減らした後のHPに対して消費するMPを足す
            atk = b + dp[i-a]
            if atk < dp[i]:
                dp[i] = atk
        elif b < dp[i]:
            # どの攻撃よりも小さいHPであれば、一番MP消費が少ない攻撃のMPを記録する
            dp[i] = b
# 配列の最後が、求めたいHP
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n):
    if p[i] <= min(p[:i+1]):
        count += 1
print(count)

Fixed submission:
n = int(input())
p = list(map(int,input().split()))
count = 0
MIN = p[0]
for i in range(n):
    if p[i] <= MIN:
        count += 1
        MIN = p[i]
        if MIN == 1:
            break
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import fractions

n = int(input())
a = list(map(int,input().split()))
# lcm
x = a[0]
for  i in range(1,n):
    x = x * a[i] // fractions.gcd(x,a[i])
b = [x//a[i] for i in range(n)]
print(sum(b)%(10**9 + 7))

Fixed submission:
from fractions import gcd
n = int(input())
a = list(map(int,input().split()))
x = a[0]
for  i in range(1,n):
    x = x * a[i] // gcd(x,a[i])
ans = 0
for i in range(n):
    ans += x//a[i]
print(ans%1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
import fractions

n = int(input())
a = list(map(int,input().split()))
# lcm
x = a[0]
for  i in range(1,n):
    x = x * a[i] // fractions.gcd(x,a[i])
b = [x//a[i] for i in range(n)]
print(sum(b)%(10**9 + 7))

Fixed submission:
from fractions import gcd
n = int(input())
a = list(map(int,input().split()))
x = a[0]
for  i in range(1,n):
    x = x * a[i] // gcd(x,a[i])
ans = 0
for i in range(n):
    ans += x//a[i]
print(ans%1000000007)
Buggy python submission (verdict: Runtime Error):
# 21:09
a,b = map(int,input().split())
import math
lcm = a*b // math.gcd(a,b)
print(lcm)

Fixed submission:
# 21:09
a,b = map(int,input().split())
import fractions
lcm = a*b // fractions.gcd(a,b)
print(lcm)
Buggy python submission (verdict: Time Limit Exceeded):
# 21:19
n = int(input())
a = list(map(int,input().split()))
ans = 0
count = 1
i = 0
while True:
    if a[i] != count:
        ans += 1
        a.pop(i)
    else:
        count += 1
        i += 1
    if n == ans + i:
        break
if len(a) == 0:
    print(-1)
else:
    print(ans)

Fixed submission:
# 21:19
from collections import deque
n = int(input())
a = deque(list(map(int,input().split())))
ans = 0
count = 1
i = 0
while True:
    if a[i] != count:
        ans += 1
        a.popleft()
    else:
        count += 1
        i += 1
    if n == ans + i:
        break
if len(a) == 0:
    print(-1)
else:
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
arr = np.array(a)
for i in range(m):
    ind = arr.argmax()
    arr[ind] = arr[ind]//2
print(arr.sum(dtype=np.int64))

Fixed submission:
import heapq
n,m = map(int,input().split())
a = list(map(lambda x: -int(x),input().split()))
heapq.heapify(a)
for _ in range(m):
    tmp = (-heapq.heappop(a)) // 2
    heapq.heappush(a, -tmp)
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# 12:10 
import numpy as np
n,m = map(int,input().split())
a = list(map(int,input().split()))
arr = np.array(a)
for i in range(m):
    ind = arr.argmax()
    arr[ind] = arr[ind]//2
print(arr.sum(dtype=np.int64))

Fixed submission:
import heapq
n,m = map(int,input().split())
a = list(map(lambda x: -int(x),input().split()))
heapq.heapify(a)
for _ in range(m):
    tmp = (-heapq.heappop(a)) // 2
    heapq.heappush(a, -tmp)
print(-sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# 12:10
import numpy as np
n,m = map(int,input().split())
a = list(map(int,input().split()))
for i in range(m):
    ind = np.argmax(a)
    a[ind] = a[ind]//2
print(sum(a))

Fixed submission:
import heapq
n,m = map(int,input().split())
a = list(map(lambda x: -int(x),input().split()))
heapq.heapify(a)
for _ in range(m):
    tmp = (-heapq.heappop(a)) // 2
    heapq.heappush(a, -tmp)
print(-sum(a))
Buggy python submission (verdict: Runtime Error):
# 21:15
import math as math

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

def F(n,c,d):
    x = n
    x -= n//c
    x -= n//d
    lcm = c*d//math.gcd(c,d)
    x += n//lcm
    return x

print(F(b,c,d) - F(a-1,c,d))

Fixed submission:
# 21:15
import fractions as math

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

def F(n,c,d):
    x = n
    x -= n//c
    x -= n//d
    lcm = c*d//math.gcd(c,d)
    x += n//lcm
    return x

print(F(b,c,d) - F(a-1,c,d))

Buggy python submission (verdict: Runtime Error):
# 21:15
import math as math

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

def F(n,c,d):
    x = n
    x -= n//c
    x -= n//d
    lcm = c*d//math.gcd(c,d)
    x += n//lcm
    return x

print(F(b,c,d) - F(a-1,c,d))

Fixed submission:
# 21:15
import fractions as math

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

def F(n,c,d):
    x = n
    x -= n//c
    x -= n//d
    lcm = c*d//math.gcd(c,d)
    x += n//lcm
    return x

print(F(b,c,d) - F(a-1,c,d))

Buggy python submission (verdict: Runtime Error):
# 21:15
import math

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

def F(n,c,d):
    x = n
    x -= n//c
    x -= n//d
    lcm = c*d//math.gcd(c,d)
    x += n//lcm
    return x

print(F(b,c,d) - F(a-1,c,d))

Fixed submission:
# 21:15
import fractions as math

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

def F(n,c,d):
    x = n
    x -= n//c
    x -= n//d
    lcm = c*d//math.gcd(c,d)
    x += n//lcm
    return x

print(F(b,c,d) - F(a-1,c,d))
Buggy python submission (verdict: Runtime Error):
N = int(input())

nums = []
max = 0

for i in range(0, N):
    nums.append(int(input()))

mx = sum(nums)
checker = [0 for i in range(0, mx)]

now = 0
res = 0
for i in range(0, N):
    v = nums[i]
    for j in range(now - 1, -1, -1):
        if checker[j] == 1:
            idx = checker[j] + v
            if idx > res and idx % 10 != 0:
                res = idx
            checker[idx] = 1
            checker[j] = 0
    checker[v] = 1
    now = now + v


print(str(res))


Fixed submission:
N = int(input())

nums = []
max = 0

for i in range(0, N):
    nums.append(int(input()))

mx = sum(nums)
if mx % 10 != 0:
    print(str(mx))
    quit()

checker = [False for i in range(0, mx+1)]

now = 0
res = 0
for i in range(0, N):
    v = nums[i]
    for j in range(now, -1, -1):
        if checker[j]:
            idx = j + v
            if idx > res and idx % 10 != 0:
                res = idx
            checker[idx] = True
    checker[v] = True
    now = now + v

print(str(res))

Buggy python submission (verdict: Time Limit Exceeded):
def rec(nums, idx):
    if len(nums) == idx + 1:
        if nums[idx] % 10 == 0:
            return 0
        else:
            return nums[idx]
    A = nums[idx] + rec(nums, idx + 1)
    B = rec(nums, idx + 1)
    if A % 10 == 0:
        A = 0
    if B % 10 == 0:
        B = 0
    return max(A, B)

N = int(input())

nums = []
for i in range(0, N):
    nums.append(int(input()))

print(str(rec(nums, 0)))

Fixed submission:
N = int(input())

nums = []
max = 0

for i in range(0, N):
    nums.append(int(input()))

mx = sum(nums)
if mx % 10 != 0:
    print(str(mx))
    quit()

checker = [False for i in range(0, mx+1)]

now = 0
res = 0
for i in range(0, N):
    v = nums[i]
    for j in range(now, -1, -1):
        if checker[j]:
            idx = j + v
            if idx > res and idx % 10 != 0:
                res = idx
            checker[idx] = True
    checker[v] = True
    now = now + v

print(str(res))
Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))

a = []
c = []
for i in range(0, N + M):
    s = list(map(int, input().split()))
    if  i < N:
        a.append(s)
    else:
        c.append(s)

for i in range(0, N):
    min_idx = 0
    min_dis = 999999999999
    for j in range(0, M):
        dis = abs(a[i][0] - c[j][0]) + abs(a[i][1] - c[j][1])
        if min_dis > dis:
            min_dis = dis
            min_idx = j + 1
    print(str(min_idx))

Fixed submission:
N, S = list(map(int, input().split()))
s = N + S
if s >= 24:
    s = s - 24
print(str(s))
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def check(t, n, idx):
    if t == n + idx or t == n - idx:
        return idx
    if idx >= t and t > 0:
        return t
    elif t < 0 and idx * -1 <= t:
        return idx
    return min(check(t, n + idx, idx + 1), check(t, n - idx, idx + 1), check(t, n, idx + 1))

X = int(input())
print(check(X, 0, 1))

Fixed submission:
N = int(input())
 
for i in range(1, N + 1):
    if N <= (i * (i + 1) / 2):
        print (str(i))
        break

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def check(t, n, idx):
    if t == n + idx or t == n - idx:
        return idx
    if idx >= t:
        return t
    return min(check(t, n + idx, idx + 1), check(t, n - idx, idx + 1), check(t, n, idx + 1))

X = int(raw_input())
print(check(X, 0, 1))

Fixed submission:
N = int(input())
 
for i in range(1, N + 1):
    if N <= (i * (i + 1) / 2):
        print (str(i))
        break

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def check(t, now, idx):
    if t < idx:
        return 99999999999
    # use
    use = 999999999999
    if t == now + idx:
        use = idx
    elif t > now + idx:
        use = check(t, now + idx, idx + 1)
    # not use
    nuse = check(t, now, idx + 1)

    return min(use, nuse)

X = int(raw_input())
print(str(check(X, 0, 1)))

Fixed submission:
N = int(input())
 
for i in range(1, N + 1):
    if N <= (i * (i + 1) / 2):
        print (str(i))
        break

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def check(t, now, idx):
    if t < idx:
        return 99999999999
    # use
    use = -1
    if t == now + idx:
        use = idx
    else:
        use = check(t, now + idx, idx + 1)
    # not use
    nuse = check(t, now, idx + 1)

    return min(use, nuse)

X = int(raw_input())
print(str(check(X, 0, 1)))

Fixed submission:
N = int(input())
 
for i in range(1, N + 1):
    if N <= (i * (i + 1) / 2):
        print (str(i))
        break
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def check(s, ans):
    if len(s) != len(ans):
        return False;
    for i in range(0, len(s)):
        pre = i - 1
        next = i + 1
        if i == 0:
            pre = len(s) - 1
        if i == len(s) - 1:
            next = 0
                
        if ans[i] == "S":
            if s[i] == "o":
                if ans[pre] != ans[next]:
                    return False
            else:
                if ans[pre] == ans[next]:
                    return False
        else:
            if s[i] == "x":
                if ans[pre] != ans[next]:
                    return False
            else:
                if ans[pre] == ans[next]:
                    return False
    return True
                                
def getAns(s, ans, idx, n):
    if idx == n - 1:
        if check(a, ans):
            return ans
        else:
            return []
    if idx == 0:
        if ans[idx] == "S":
            if s[idx] == "o":
                ans[n - 1] = ans[1]
            else:
                if ans[1] == "S":
                    ans[n - 1] = "W"
                else:
                    ans[n - 1] = "S"
        else:
            if s[idx] == "x":
                ans[n - 1] = ans[1]
            else:
                if ans[1] == "S":
                    ans[n - 1] = "W"
                else:
                    ans[n - 1] = "S"
        return getAns(s, ans, idx + 1, n)
    pre  = idx - 1
    next = idx + 1
    # 羊
    if ans[idx] == "S":
        if s[idx] == "o":
            ans[next] = ans[pre]
        else:
            if ans[pre] == "S":
                ans[next] = "W"
            else:
                ans[next] = "S"
    # 狼
    else:
        if s[idx] == "x":
            ans[next] = ans[pre]
        else:
            if ans[pre] == "S":
                ans[next] = "W"
            else:
                ans[next] = "S"
    return getAns(s, ans, idx + 1, n)

def arrayToString(arry):
    s = ""
    for i in range(0, len(arry)):
        s+= arry[i]
    return s

n = int(input())
a = input()
s = [a[i] for i in range(0, len(a))]
ans = ["S" for i in range(0, len(a))]
ans1 = ans[:]
ans1 = getAns(s, ans1, 0, n)
if len(ans1) != 0:
    print(arrayToString(ans1))
    quit()
ans2 = ans[:]
ans2[0] = "W"
ans2 = getAns(s, ans2, 0, n)
if len(ans2) != 0:
    print(arrayToString(ans2))
    quit()
ans3 = ans[:]
ans3[1] = "W"
ans3 = getAns(s, ans3, 0, n)
if len(ans3) != 0:
    print(arrayToString(ans3))
    quit()
ans4 = ans[:]
ans4[0] = "W"
ans4[1] = "W"
ans4 = getAns(s, ans4, 0, n)
if len(ans4) != 0:
    print(arrayToString(ans4))
    quit()
print("-1")



Fixed submission:
# -*- coding: utf-8 -*-
n = int(input())
print(n * 800 - (200 * int(n / 15)))
Buggy python submission (verdict: Runtime Error):
n = input().split()

ar = int(n[0])
br = int(n[1])

a = []
b = []
for i in range(0, ar):
    a.append(input())

for j in range(0, br):
    b.append(input())

find = False
for i in range(0, ar - br):
    idx = a[i].find(b[0])
    if idx != -1:
        match = True
        for j in range(1, br):
            idx2 = a[i + j].find(b[j])
            if idx != idx2:
                match = False
        if match:
            find = True
            break
if find:
    print("Yes")
else:
    print("No")

Fixed submission:
c = input().split()
a = int(c[0])
b = int(c[1])
if a == 1:
    a = 14
if b == 1:
    b = 14

if a > b:
    print("Alice")
elif b > a:
    print("Bob")
else:
    print("Draw")
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import numpy as np
n = int(input())
nums = input().split()
nums = [int(nums[i]) for i in range(0, len(nums))]
l = len(nums)
res = nums[:]

while l > 1:
    fst = res[0]
    tmp = res[1:]
    res = []
    flg = False
    find = -1
    # even
    if fst % 2 == 0:
        for i in range(0, len(tmp)):
            if flg == False and tmp[i] % 2 == 0:
                find = fst + tmp[i]
                flg = True
            else:
                res.append(tmp[i])
        if flg:
            res.append(find)
    else:
        for i in range(0, len(tmp)):
            if flg == False and tmp[i] % 2 == 1:
                find = fst + tmp[i]
                flg = True
            else:
                res.append(tmp[i])
        if flg:
            res.append(find)
    if flg == False and len(tmp) != 1:
        res = []
        res.append(fst)
        fst = tmp[0]
        tmp = tmp[1:]
        # even
        if fst % 2 == 0:
            for i in range(0, len(tmp)):
                if flg == False and tmp[i] % 2 == 0:
                    find = fst + tmp[i]
                    flg = True
                else:
                    res.append(tmp[i])
            if flg:
                res.append(find)
        else:
            for i in range(0, len(tmp)):
                if flg == False and tmp[i] % 2 == 1:
                    find = fst + tmp[i]
                    flg = True
                else:
                    res.append(tmp[i])
            if flg:
                res.append(find)
    if flg == False:
        print ("NO")
        quit()
    else:
        if l == len(res):
            print("something wrong!")
            print(res)
            quit()
        l = len(res)
print("YES")

Fixed submission:
# -*- coding: utf-8 -*-
import numpy as np
n = int(input())
nums = input().split()
nums = [int(nums[i]) for i in range(0, len(nums))]

odd = 0
even = 0
for i in range(0, len(nums)):
    if nums[i] % 2 == 0:
        even += 1
    else:
        odd += 1
if odd % 2 == 0:
    print("YES")
else:
    print("NO")
Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(x) for x in input().split()]
hs = [int(x) for x in input().split()]
dp = {}
dp[0] = 0 # start

for i in range(n):
    for j in range(1, k + 1):
        to = i + j
        if to >= n:
            break
        cost = abs(hs[to] - hs [i]) + dp[i]
        if to in dp:
            if dp[to] > cost:
                dp[to] = cost
        else:
            dp[to] = cost

print(dp[n-1])


Fixed submission:
n, k = [int(x) for x in input().split()]
hs = [int(x) for x in input().split()]
dp = {}
dp[0] = 0 # start

for i in range(n):
    for j in range(1, k + 1):
        to = i + j
        if to >= n:
            break
        cost = abs(hs[to] - hs [i]) + dp[i]
        if to in dp:
            if dp[to] > cost:
                dp[to] = cost
        else:
            dp[to] = cost

print(dp[n-1])