qyliang/CodeNanoFix huggingface dataset:
# Buggy code:
# -*- coding: utf-8 -*-

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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        return min_height

    left_list = list_h[:zero_indexes[0]]
    right_list = list_h[zero_indexes[0]+1:]
    return calc(left_list) + calc(right_list)


print(calc(h))


# Fixed code:
# -*- coding: utf-8 -*-

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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        return min_height

    sum_cnt = 0
    left_list = list_h[:zero_indexes[0]]
    right_list = list_h[zero_indexes[0]+1:]
    sum_cnt += calc(left_list) + calc(right_list)

    return sum_cnt + min_height


print(calc(h))
# Buggy code:
n=int(input())
h=list(map(int,input().split()))
f=[0]*n
cnt=0
while 1:
    flag=0
    for i in range(n):
        if h[i]!=f[i]:
            f[i]+=1
            flag=1
        elif h[i]==f[i] and flag==1:
            cnt+=1
            flag=0
    if flag:==1
        cnt+=1

    if h == f:
        break

print(cnt)


# Fixed code:
n=int(input())
h=list(map(int,input().split()))
f=[0]*n
cnt=0
while 1:
    flag=0
    for i in range(n):
        if h[i]!=f[i]:
            f[i]+=1
            flag=1
        elif h[i]==f[i] and flag==1:
            cnt+=1
            flag=0
    if flag==1:
        cnt+=1

    if h==f:
        break

print(cnt)
# Buggy code:
N = int(input())
h = list(map(int, input().split()))

def minus(l,x):
    for i in range(len(l)):
        l[i] = l[i] - x
    

count = 0
count += min(h)
minus(h, min(h))

print(h)
for i in range(N-1):
    if h[i] > h[i+1]:
        count -= h[i+1]-h[i]
        
count += h[-1]
print(count)
    



# Fixed code:
N = int(input())
h = list(map(int, input().split()))

def minus(l,x):
    for i in range(len(l)):
        l[i] = l[i] - x
    

count = 0
count += min(h)
minus(h, min(h))


for i in range(N-1):
    if h[i] > h[i+1]:
        count -= h[i+1]-h[i]
        
count += h[-1]
print(count)
# Buggy code:
import numpy as np

n = int(input())
h = list(map(int, input().split(" ")))

h = np.array(h)

count = 0
while(sum(h) != 0):
    s = 200
    e = n
    for i in range(n):
        if(s == 200):
            if(h[i] != 0): s = i
        elif(h[i] == 0):
            e = i
            break

    count += min(h[s:e])
    h[s:e] -= min(h[s:e])

    print(h)

print(count)

# Fixed code:
import numpy as np

n = int(input())
h = list(map(int, input().split(" ")))

h = np.array(h)

count = 0
while(sum(h) != 0):
    s = 200
    e = n
    for i in range(n):
        if(s == 200):
            if(h[i] != 0): s = i
        elif(h[i] == 0):
            e = i
            break

    count += min(h[s:e])
    h[s:e] -= min(h[s:e])

print(count)
# Buggy code:
n=int(input())
h=list(map(int,input.split()))
hm=max(h)
t=1
for i in range(hm,-1,-1):
    for j in range(n)
        if h[j]=i:
            ans+=t
            t=0
        else:
            t=1

print(ans)


# Fixed code:
n=int(input())
h=list(map(int,input().split()))
hm=max(h)

ans=0
for i in range(hm,0,-1):
    t=1
    for j in range(n):
        if h[j]>=i:
            ans+=t
            t=0
        else:
            t=1

print(ans)
# Buggy code:
import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=999999999999999
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    #print(sum)
    B=bunkai(lis)
    #print(B)
    if len(B)==0:
        #print("return: "+str(sum))
        return sum
    for l in B:
        #print(l)
        x=min(l)
        sum+=x
        li=mizu(l,x)
        sum=sso(li,sum)
    #print("return end:"+str(sum))
    return sum


sum=0
x=sso(H,sum)

# Fixed code:
import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=101
    if len(lis)==0:
        return 0
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    #print(sum)
    B=bunkai(lis)
    #print(B)
    if len(B)==0:
        #print("return: "+str(sum))
        return sum
    for l in B:
        #print(l)
        x=min(l)
        #print("+"+str(x))
        sum+=x
        li=mizu(l,x)
        #print("li"+str(li))
        sum=sso(li,sum)
    #print("return end:"+str(sum))
    return sum

#print(min([]))
sum=0
x=sso(H,sum)

    
print(x)
# Buggy code:
N = int(input())
# h = list(map(int, input().split()))
h = [0]
for _ in range(N):
    h.append(int(input()))
answer = 0
for i in range(1, N+1):
    answer += max(H[i] - H[i-1], 0)

print(answer)

# Fixed code:
N = int(input())
H = [0] + [int(a) for a in input().split()]
# for _ in range(N):
#     h.append(int(input()))
answer = 0
for i in range(1, N+1):
    answer += max(H[i] - H[i-1], 0)

print(answer)
# Buggy code:
N = int(input())
h = list(map(int, input().split()))

peak = []
trough = []
before = h[0]
for a, b in zip(h[:-1], h[1:]):
    after = b - a
    if after == 0:
        continue
    elif before > 0 and after < 0:
        peak.append(a)
    elif before < 0 and after > 0:
        trough.append(a)
    before = after

print(sum(peak) - sum(trough))

# Fixed code:
N = int(input())
h = list(map(int, input().split()))

peak = []
trough = []
h = [0] + h + [0]
before = 0
for a, b in zip(h[:-1], h[1:]):
    after = b - a
    if after == 0:
        continue
    elif before > 0 > after:
        peak.append(a)
    elif before < 0 < after:
        trough.append(a)
    before = after

print(sum(peak) - sum(trough))
# Buggy code:
n, *H = map(int, open(0).read().split())

inc = True
peaks = [], []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            peaks.append(h0)
            inc = False
    else:
        if h0 < h1:
            peaks.append(-h0)
            inc = True
if inc:
    peaks.append(H[-1])
print(sum(peaks))

# Fixed code:
n, *H = map(int, open(0).read().split())

inc = True
maxes, mins = [], []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            maxes.append(h0)
            inc = False
    else:
        if h0 < h1:
            mins.append(h0)
            inc = True
if inc:
    maxes.append(H[-1])
print(sum(maxes)-sum(mins))
# Buggy code:
n = int(input())
h = map(int, input().split())

ans = 0
for i in range(1, n):
  d = h[i] - h[i-1]
  if d > 0:
    ans += d
print(ans)

# Fixed code:
n = int(input())
h = list(map(int, input().split()))
 
ans = h[0]
for i in range(1, n):
  d = h[i] - h[i-1]
  if d > 0:
    ans += d
print(ans)
# Buggy code:
import numpy as np
N = int(input())
H = np.array(list(map(int,input().split())))

ans = 0
while np.max(H) != 0:
    min_h_p = 0
    for i in range(N):
        if H[i] == 0 and min_h_p > 0:
            ans += min_h_p
            H[st:i] -= min_h_p
            break
        elif i == N-1:
            if min_h_p == 0:
                min_h_p = H[i]
            if st == 0:
                H[i] -= min_h_p
                ans += min_h_p
            else:
                ans += min_h_p
                H[st:] -= min_h_p
            break
        else:
            if min_h_p == 0:
                min_h_p = H[i]
                st = i
            else:
                min_h_p = min(H[i],min_h_p)

print(ans)


# Fixed code:
import numpy as np
N = int(input())
H = np.array(list(map(int,input().split())))

ans = 0
while np.max(H) != 0:

    min_h_p = 0
    st = -1
    for i in range(N):
        if st == -1:
            if H[i] > 0:
                st = i
            if i == N-1:
                ans += H[i]
                H[i] = 0
                break
        elif H[i] == 0:
            min_h_p = min(H[st:i])
            ans += min_h_p
            H[st:i] -= min_h_p
            break
        elif i == N-1:
            min_h_p = min(H[st:])
            ans += min_h_p
            H[st:] -= min_h_p
            break
print(ans)
# Buggy code:
n=int(input())
h=list(map(int,input().split()))
ans=0
while any(h)==True:
    flag=0
    for i in range(n):
        if h[i]>0:
            h[i]-=1
            flag=1
        else:
            if flag==1:
                ans+=1
                flag=0
        if i==n-2:
            if h[n-1]>0:
                h[n-1]-=1
                ans+=1
                break
            else:
                if h[i]>0:
                    ans+=1
                break
print(ans)


# Fixed code:
n=int(input())
h=list(map(int,input().split()))
ans=0
if n==1:
    print(h[0])
    exit()
while any(h)==True:
    flag=0
    for i in range(n-1):
        if h[i]>0:
            h[i]-=1
            flag=1
        else:
            if flag==1:
                ans+=1
                flag=0
        if i==n-2:
            if h[n-1]>0:
                h[n-1]-=1
                ans+=1
            else:
                if flag==1:
                    ans+=1
print(ans)
# Buggy code:
import numpy as np
N = int(input())
h = list(map(int, input().split()))
h = np.array(h)
h_diff = h[1:] - h[:N-1]
count = 0
print(h_diff)
for i in range(N-1):
    count -= min(h_diff[i], 0)
    
print(count + h[N-1])

# Fixed code:
import numpy as np
N = int(input())
h = list(map(int, input().split()))
h = np.array(h)
h_diff = h[1:] - h[:N-1]
count = 0
for i in range(N-1):
    count -= min(h_diff[i], 0)
    
print(count + h[N-1])
# Buggy code:
n= int(input())
h= list(map(int, input().split()))
x=max(h)
ans=0
count=0
for i in range(10009):
    count=0
    if h[-1]==0:
        break
    ans+=1
    for j in range(n):
        if h[j]==0 and count==0:
            continue
        elif h[j]>0:
            count=1
            h[j]-=1
        else:
            break



print(ans)

# Fixed code:
n= int(input())
h= list(map(int, input().split()))
x=max(h)
ans=0
count=0
for i in range(10009):
    count=0
    if max(h)==0:
        break
    ans+=1
    for j in range(n):
        if h[j]==0 and count==0:
            continue
        elif h[j]>0:
            count=1
            h[j]-=1
        else:
            break



print(ans)
# Buggy code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
for i in range(N-1):
  if h[i+1] > h[i]:
    ans += h[i+1]-h[i]
print(ans)

# Fixed code:
N = int(input())
h = list(map(int, input().split()))
ans = h[0]
for i in range(N-1):
  if h[i+1] > h[i]:
    ans += h[i+1]-h[i]
print(ans)
# Buggy code:
n = int(input())
l = list(map(int,input().split()))
count = 0
prev = 0
for i in l:
  if i > prev:
    count += i - prev
    prev = i
print(count)

# Fixed code:
n = int(input())
l = list(map(int,input().split()))
count = 0
prev = 0
for i in l:
  if i > prev:
    count += i - prev
  prev = i
    
print(count)
# Buggy code:
n = int(input())
h = list(map(int,input().split()))
h.insert(0,0)
sum = 0
bottom = 0
for i, hn in enumerate(h):
  if (i == 0) :
    continue
  if (hn < h[i-1]):
    print(h[i-1])
    sum += (h[i-1] - bottom)
    bottom = hn
end = bottom if h[n-1] < h[n] else 0
print(sum+end)

# Fixed code:
n = int(input())
h = list(map(int,input().split()))
h.insert(0,0)
sum = 0
bottom = 0
for i, hn in enumerate(h):
  if (i == 0) :
    continue
  if (hn < h[i-1]):
    bottom = hn
  else:
    sum += hn - h[i-1]
print(sum)
# Buggy code:
n, k = map(int, input().split())
h = [int(i) for i in input().split()]

def chmin(a, b):
    if a > b:
        return b
    else:
        return a
f_inf = pow(10,9)+99

dp = [f_inf] * (N+1)
dp[0] = 0
for i in range(1, n):
    for j in range(1, k+1):
        if i - j >= 0:
            dp[i] = chmin(dp[i], dp[i-j]+ abs(h[i] - h[i - j]))
print(dp[n-1])


# Fixed code:
n, k = map(int, input().split())
h = [int(i) for i in input().split()]

def chmin(a, b):
    if a > b:
        return b
    else:
        return a
f_inf = pow(10,9)+99

dp = [f_inf] * (10**5+10)
dp[0] = 0
for i in range(1, n):
    for j in range(1, k+1):
        if i - j >= 0:
            dp[i] = chmin(dp[i], dp[i-j]+ abs(h[i] - h[i - j]))
print(dp[n-1])
# Buggy code:
n,k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10 ** 18] * (n+1)
dp[1] = 0
for i,v in enumerate(h,1):
  for j in range(i+1,min(n+1,i+k+1)):
    dp[j] = min(dp[j],dp[i]+v)
print(dp[n])


# Fixed code:
n,k = map(int, input().split())
h = list(map(int, input().split()))
dp = [10 ** 18] * (n+1)
dp[1] = 0
for i,v in enumerate(h,1):
  for j in range(i+1,min(n+1,i+k+1)):
    dp[j] = min(dp[j],dp[i]+abs(v-h[j-1]))
print(dp[n])
# Buggy code:
import numpy as np

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

cost = np.full(N,10**9)
cost[0] = 0
for i in range(1 , N):
    
    size =  min(i,K)
    cost[i] = (np.abs(a[i] - a[i-size:i]) + cost[i-size:i])

print(int(cost[-1]))

# Fixed code:
import numpy as np

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

cost = np.full(N,10**9)
cost[0] = 0
for i in range(1 , N):
    
    size =  min(i,K)
    cost[i] = (np.abs(a[i] - a[i-size:i]) + cost[i-size:i]).min()

print(int(cost[-1]))
# Buggy code:
n,k=map(int,input().split())
h=list(map(int,input().split()))
cost=[float("inf")]*n
cost[0]=0
cost[1]=abs(h[1]-h[0])

for i in range(1,n-1):
    for j in range(1,k+1):
        if i+j>n-1:
            continue
        else:
            cost[i+j]=min(abs(h[i+j]-h[i])+cost[i] ,cost[i+j])

print(cost[n-1])


# Fixed code:
n,k=map(int,input().split())
h=list(map(int,input().split()))
cost=[float("inf")]*n
cost[0]=0

for i in range(n):
    for j in range(1,k+1):
        if i+j>n-1:
            continue
        else:
            cost[i+j]=min(abs(h[i+j]-h[i])+cost[i] ,cost[i+j])

print(cost[n-1])
# Buggy code:
import sys
input = sys.stdin.readline


N, K = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [float("inf")] * (N)
dp[0] = 0
print(dp)

for n in range(1, N):
    k = max(0, n - K)
    dp[n] = min(abs(h[n] - h[i]) + dp[i] for i in range(k, n))

print(dp)

print(dp[-1])



# Fixed code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
h = tuple(map(int,input().split()))

dp = [0] + [float("inf")]*(n-1)
#print(dp)

for i in range(1,n):
    for j in range(max(0,i-k),i):
        dp[i] = min(dp[i],dp[j]+abs(h[i]-h[j]))
#print(dp)

print(dp[-1])
# Buggy code:
n,h,l=int(input()),int(input()),list(map(int,input().split()))
c=[0]*n
c[1]=abs(l[0]-l[1])
for i in range(2,n):
  c[i]=min( c[i-j]+abs(l[i]-l[i-j]) for j in range(1,h+1) if i-j > -1 )
print(c[-1])

# Fixed code:
n,h=map(int,input().split())
l=list(map(int,input().split()))
c=[0]*n
c[1]=abs(l[0]-l[1])
for i in range(2,n):
    c[i]=min( c[j]+abs(l[i]-l[j]) for j in range(max(0,i-h),i) )
print(c[-1])
# Buggy code:
N = int(input())
h_s = list(map(int, input().split()))+[0]*10
# print(h_s)
dp = [float("inf")]*(N+10)
dp[0] = 0

for i in range(N):
    for j in range(1, 3):
        dp[i+j] = min(dp[i+j], dp[i]+abs(h_s[i]-h_s[i+j]))

print(dp[N-1])

# Fixed code:
N, K = map(int, input().split())
h_s = list(map(int, input().split()))+[0]*K
# print(h_s)
dp = [float("inf")]*(N+K)
dp[0] = 0
 
for i in range(N):
    for j in range(1, K+1):
        dp[i+j] = min(dp[i+j], dp[i]+abs(h_s[i]-h_s[i+j]))
 
print(dp[N-1])
# Buggy code:
N, K = map(int, input().split())
h = [0] * (2*N)
h[:N] = list(map(int, input().split()))
INF = 10**9
dp = [INF] * (2*N)
dp[0] = 0
for i in range(N):
    for k in range(1, K+1):
        dp[i+k] = min(dp[i+k], dp[i]+abs(h[i] - h[i+k]))
print(dp[N-1])

# Fixed code:
N, K = map(int, input().split())
h = list(map(int, input().split()))
INF = 10**9
dp = [INF] * N
dp[0] = 0
for i in range(N):
    for k in range(1, K+1):
        if i + k < N:
            dp[i+k] = min(dp[i+k], dp[i]+abs(h[i] - h[i+k]))
print(dp[N-1])
# Buggy code:
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [float('inf')] * 10 ** 5

dp[0] = 0
for i in range(1, N):
  tmp = i
  if tmp > K:
    tmp = K
  for j in range(1,tmp + 1):
      dp[i] = dp[i - j] + abs(h[i - j] - h[i])

print(dp[N-1])


# Fixed code:
N,K = map(int, input().split())
h = list(map(int, input().split()))
dp = [float('inf')] * 10 ** 5

dp[0] = 0
for i in range(1, N):
  tmp = i
  if tmp > K:
    tmp = K
  for j in range(1,tmp + 1):
      dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i]))

print(dp[N-1])
# Buggy code:
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[10**5]*n
dp[0]=0
for i in range(2,n):
    for j in range(1,k):
        dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
        print(dp[n-1])


# Fixed code:
n,k=map(int,input().split())
h=list(map(int,input().split()))
dp=[10**5]*n
dp[0]=0
for i in range(1,n):
    dp[i]=min(dp[j]+abs(h[j]-h[i]) for j in range(max(0,i-k),i))
print(dp[n-1])
# Buggy code:
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    DP = [10**10 for _ in range(N)]
    DP[0] = 0
    DP[1] = abs(h[0]-h[1])
    
    for i in range(2, N):
        DP[i] = min([DP[i-K]+abs(h[i]-h[k]) for k in range(i)])    
    print(DP[N-1])
main()

# Fixed code:
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    DP = [10**10 for _ in range(N)]
    DP[0] = 0
    DP[1] = abs(h[0]-h[1])
    
    for i in range(2, N):
        DP[i] = min(DP[k]+abs(h[i]-h[k]) for k in range(max(0, i-K), i))    
    print(DP[N-1])
main()
# Buggy code:
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = abs(h[0] - h[-1])
dp = [inf] * n
dp[0] = 0
for i in range(n):
    dp[i] = min([dp[i - j]  + abs(h[i - j] - h[i]) for j in range(min(k+1, i+1))])
print(dp[-1])


# Fixed code:
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [1001001001001001001] * n
dp[0] = 0
for i in range(n):
    for j in range(i + 1, min(i + k + 1, n)):
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]))
print(dp[n-1])
# Buggy code:
n,k=map(int,input().split())
h=list(map(int,input().split()))+[0]*(k+10)
dp=[(float("inf")) for i in range(n+k+10)]
dp[0]=0
def main():
  	for i in range(n-1):
      	for j in range(k):
          	dp[i+j+1]=min(dp[i]+abs(h[i]-h[i+j+1]),dp[i+j+1])
  	print(dp[n-1])
main()


# Fixed code:
def main():
  n,k=map(int,input().split())
  h=list(map(int,input().split()))+[0]*(k+10)
  dp=[(float("inf")) for i in range(n+k+10)]
  dp[0]=0
  for i in range(n-1):
    for j in range(k):
      dp[i+j+1]=min(dp[i]+abs(h[i]-h[i+j+1]),dp[i+j+1])
  return (dp[n-1])
print(main())
# Buggy code:
n,k=map(int,input().split())
h=list(map(int,input().split()))
def ch_min(a,b):
    if a>b:
        return b

def solve():
    dp=[float('inf')]*n
    dp[0]=0

    for i in range(1,n):
    
        for j in range(1,1+k):
            if i-j>=0:
                dp[i]=ch_min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
                
                
    print(dp[-1])
    

solve()

# Fixed code:
n,k=map(int,input().split())
h=list(map(int,input().split()))
def ch_min(a,b):
    if a>b:
        return b
    
    return a
def solve():
    dp=[float('inf')]*n
    dp[0]=0

    for i in range(1,n):
    
        for j in range(1,1+k):
            if i-j>=0:
                dp[i]=ch_min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
                
                if dp[i]==0:
                    break
                
    print(dp[-1])
    

solve()
# Buggy code:
from numba import jit
import math
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
S = [0] * N
S[0] = 0
 
@jit
def nya(S, N):
    for i in range(1, N):
        value = math.inf
        for step in range(1, K + 1):
            if i - step < 0:
                break
            value = min(value, S[i - step] + abs(h[i] - h[i - step]))
        S[i] = value
    return S

S = nya(S, N)
print(S[N - 1])

# Fixed code:
import math
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
def nya(N, K, h):
    S = [0] * N
    S[0] = 0
 
    for i in range(1, N):
        value = math.inf
        for step in range(1, K + 1):
            if i - step < 0:
                break
            value = min(value, S[i - step] + abs(h[i] - h[i - step]))
        S[i] = value
    return S[N-1]

print(nya(N, K, h))
# Buggy code:
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [0 for i in range(N)]
for i in range(N):
    for j in range(1,K+1):
        if i+j<N:
            if dp[i+j] == 0:
                dp[i+j] = dp[i]+abs(h[i]-h[j])
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i]-h[i+j]))
        
print(dp[-1])

# Fixed code:
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [10**15+1 for i in range(N)]
dp[0] = 0
for i in range(N):
    for j in range(1,K+1):
        if i+j<N:
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i]-h[i+j]))
        
print(dp[-1])
# Buggy code:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [float('inf') for _ in range(n)]
    dp[0] = 0
    dp[1] = abs(h[0]-h[1])
    for i in range(1, n):
        for j in range(1, min(k+1, n-i)):
            num = dp[i]+abs(h[i]-h[i+j])
            if num < dp[i+j]:
                dp[i+j] = num
    print(dp[-1])

main()


# Fixed code:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [float('inf')]*n
    dp[0] = 0
    for i in range(n):
        a = h[i]
        for j in range(1, min(k+1, n-i)):
            num = dp[i]+abs(a-h[i+j])
            if num < dp[i+j]:
                dp[i+j] = num
    print(dp[-1])

main()
# Buggy code:

def solver():
	N,K = map(int, input().split())
	h = list(map(int, input().split()))
	dp = [0 for _ in range(N)]
	for i in range(1,N):
		min_cost = 100000
		for j in range(max(0, i-K),i):
			cost = dp[j] + abs(h[i]-h[j])
			if cost < min_cost : min_cost = cost
		dp[i] = min_cost
	print(dp)
	print(dp[N-1])

solver()

# Fixed code:
def solver():
	N,K = map(int, input().split())
	h = list(map(int, input().split()))
	dp = [0 for _ in range(N)]
	for i in range(1,N):
		min_cost = 10**10
		for j in range(max(0, i-K),i):
			cost = dp[j] + abs(h[i]-h[j])
			if cost < min_cost : min_cost = cost
		dp[i] = min_cost
	print(dp[N-1])

solver()