CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
XY = [tuple(map(int,input().split())) for i in range(N)]
V = 10**5+5

class UnionFind:
    def __init__(self,N):
        self.parent = [i for i in range(N)]
        self.rank = [0] * N
        self.size_x = [1 - i//V for i in range(N)]
        self.size_y = [i//V for i in range(N)]
    def root(self,a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]
    def is_same(self,a,b):
        return self.root(a) == self.root(b)
    def unite(self,a,b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb: return
        if self.rank[ra] < self.rank[rb]:
            self.parent[ra] = rb
            self.size_x[rb] += self.size_x[ra]
            self.size_y[rb] += self.size_y[ra]
        else:
            self.parent[rb] = ra
            if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1
            self.size_x[ra] += self.size_x[rb]
            self.size_y[ra] += self.size_y[rb]

uf = UnionFind(V*2)

for x,y in XY:
    uf.unite(x,y+V)
for i in range(V*2):
    uf.root(i)

ans = 0
used = set()
for i in range(V*2):
    r = uf.root(i)
    if r in used: continue
    used.add(r)
    ans += uf.size_x[r] * uf.size_y[r]
ans -= N
print(ans)

# Optimized solution
N = int(eval(input()))
XY = [tuple(map(int,input().split())) for i in range(N)]

class UnionFind:
    def __init__(self,N):
        self.parent = [i for i in range(N)]
        self._size = [1] * N
        self.count = 0
    def root(self,a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]
    def is_same(self,a,b):
        return self.root(a) == self.root(b)
    def unite(self,a,b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb: return
        if self._size[ra] < self._size[rb]: ra,rb = rb,ra
        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        self.count += 1
    def size(self,a):
        return self._size[self.root(a)]

M = 10**5
uf = UnionFind(2*M)
for x,y in XY:
    a,b = x-1,y-1+M
    if uf.is_same(a,b): continue
    uf.unite(a,b)
for i in range(2*M):
    uf.root(i)

rt = []
for i in range(2*M):
    rt.append(uf.root(i))
from collections import Counter
xr = Counter(rt[:M])
yr = Counter(rt[M:])

ans = 0
for k,v in list(xr.items()):
    ans += yr[k] * v
ans -= N
print(ans)
# Original solution
n = int(eval(input()))
xy = [list(map(int, input().split())) for _ in range(n)]
xtoy = {}
ytox = {}
for x, y in xy:
    if x not in xtoy:
        xtoy[x] = []
    xtoy[x].append(y)
    if y not in ytox:
        ytox[y] = []
    ytox[y].append(x)
ret = 0
while len(xtoy) > 0:
    x = next(iter(xtoy))
    xs = set()
    ys = set()
    xs.add(x)
    q = [(x, None)]
    while len(q) > 0:
        (xx, yy) = q.pop(0)
        if xx is not None:
            for y in xtoy.pop(xx):
                if y not in ys:
                    ys.add(y)
                    q.append((None, y))
        if yy is not None:
            for x in ytox.pop(yy):
                if x not in xs:
                    xs.add(x)
                    q.append((x, None))
    ret += len(xs) * len(ys)
print((ret - n))


# Optimized solution
import sys
sys.setrecursionlimit(100000)

n = int(eval(input()))
xy = [list(map(int, input().split())) for _ in range(n)]
xtoy = {}
ytox = {}
for x, y in xy:
    if x not in xtoy:
        xtoy[x] = [y]
    else:
        xtoy[x].append(y)
    if y not in ytox:
        ytox[y] = [x]
    else:
        ytox[y].append(x)

def doit(x, xs, ys, xtoy, ytox):
    ret = 0
    if x in xtoy:
        xs.add(x)
        yl = xtoy.pop(x)
        ret += len(yl)
        for y in yl:
            ret += doit(y, ys, xs, ytox, xtoy)
    return ret

ret = 0
while len(xtoy) > 0:
    x = next(iter(xtoy))
    xs = set()
    ys = set()
    num = doit(x, xs, ys, xtoy, ytox)
    ret += len(xs) * len(ys) - num // 2

print(ret)
# Original solution
import sys
input = sys.stdin.readline

N = int(eval(input()))
XY = [[set() for i in range(10**5+1)] for j in range(2)]
is_visited_x = [True] * (10**5 + 1)
for i in range(N):
  x, y = list(map(int, input().split()))
  is_visited_x[x] = False
  XY[0][x].add(y)
  XY[1][y].add(x)


def dfs(x):
  global is_visited_x
  is_visited_x[x] = True
  stack = [[x], []]
  idx = 0
  num_e = 0
  num_xy = [1, 0]
  parent = 0
  while stack[0] or stack[1]:
    node = stack[idx][-1]
    children = XY[idx][node]
    if children:
      child = children.pop()
      XY[idx-1][child].remove(node) # 今来た道を削除
      num_e += 1
      if child in stack[idx-1]:
        continue
      stack[idx-1].append(child)
      num_xy[idx-1] += 1
      is_visited_x[child] += idx
      idx = (idx + 1) % 2
    else:
      stack[idx].pop()
      idx = (idx + 1) % 2
  return num_e, num_xy

ans = 0
for x in range(1, 10**5+1):
  if is_visited_x[x]:
    continue
  num_e, num_xy = dfs(x)
  ans += num_xy[0]*num_xy[1] - num_e

print(ans)

# Optimized solution
# reference -> https://atcoder.jp/contests/abc131/submissions/10358162
import sys
sys.setrecursionlimit(100000)

# 再帰的に連結部分の辺の数を求める
# 同じ辺を2回ずつ数えるので、最終的には2で割る
def solve(x, x_set, y_set, x2y, y2x):
  num_e = 0
  if x in x2y:
    x_set.add(x)
    yl = x2y.pop(x)
    num_e += len(yl)
    for y in yl:
      num_e += solve(y, y_set, x_set, y2x, x2y)
  return num_e


N = int(eval(input()))
X2Y = {}
Y2X = {}
for i in range(N):
  x, y = list(map(int, input().split()))
  if x in X2Y:
    X2Y[x].append(y)
  else:
    X2Y[x] = [y]
  if y in Y2X:
    Y2X[y].append(x)
  else:
    Y2X[y] = [x]


ans = 0
while X2Y:
    x = next(iter(X2Y)) # まだスタートに選んでいないxを取り出す
    x_set = set() # 上のxと連結であるxの集合
    y_set = set() # 上のxと連結であるyの集合
    num_e = solve(x, x_set, y_set, X2Y, Y2X)
    ans += len(x_set)*len(y_set) - num_e//2

print(ans)
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print((DP[-1]))

# Optimized solution
# B Frog 2

n, k = list(map(int, input().split()))
h_list = [int(x) for x in input().split()]

dp = [float('Inf')] * n
dp[0] = 0

for i in range(n):
    for j in range(1,k+1,1):
        if i - j < 0:
            continue
        dp[i] = min(dp[i], dp[i-j] + abs(h_list[i-j]-h_list[i]))

        
print((dp[n-1]))
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**5

# DP = [INF] * ((10**5)+10)
DP = [None] * ((10**5)+10)
# DP = [None] * (N+1)
DP[0] = 0

for i in range(1, N):
    a = []
    for j in range(1, K+1):
        if 0 <= i-j < N:
            v = DP[i-j] + abs(H[i-j] - H[i])
            a.append(v)
    DP[i] = min(a)


# print(DP)
print((DP[N-1]))

# Optimized solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**5

DP = [INF] * ((10**5)+10)
# DP = [None] * ((10**5)+10)
# DP = [None] * (N+1)
DP[0] = 0

for i in range(1, N):
    a = []
    for j in range(1, K+1):
        if 0 <= i-j < N:
            v = DP[i-j] + abs(H[i-j] - H[i])
            a.append(v)
    DP[i] = min(a)


# print(DP)
print((DP[N-1]))
# Original solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**5

DP = [INF] * ((10**5)+10)
# DP = [None] * ((10**5)+10)
# DP = [None] * (N+1)
DP[0] = 0

for i in range(1, N):
    a = []
    for j in range(1, K+1):
        if 0 <= i-j < N:
            v = DP[i-j] + abs(H[i-j] - H[i])
            a.append(v)
    DP[i] = min(a)


# print(DP)
print((DP[N-1]))

# Optimized solution
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

INF = 10**6

# DP = [INF] * N
# DP = [INF] * ((10**5)+10)
DP = [float('inf')] * N
DP[0] = 0

# for i in range(1, N):
#     for j in range(1, K+1):
#         if 0 <= i - j < N:
#             DP[i] = min(DP[i], DP[i-j] + abs(H[i-j] - H[i]))

for i in range(1, N):
    for j in range(1, K + 1):
        # if 0 <= i - j < N:
        if i - j < 0:
            break
        DP[i] = min(DP[i], DP[i - j] + abs(H[i] - H[i - j]))

print((DP[N-1]))
# print(DP)
# Original solution
from math import inf

n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
dp = [inf for _ in range(n)]
dp[0] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i - j >= 0:
            dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j])

print((dp[-1]))


# Optimized solution
from math import inf

n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
dp = [inf for _ in range(n)]
dp[0] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i - j >= 0:
            dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j])
        else:
            break

print((dp[-1]))
# Original solution
def number():
    return int(eval(input()))


def space():
    return list(map(int, input().split()))


N, K = space()
H = space()

inf = 10**4+1

dp = [0] * N
dp[0] = 0

for i in range(1, N):
    v = []
    for j in range(max(0, i - K), i):
        v.append(dp[j] + abs(H[j] - H[i]))
        # dp[i] = min(dp[i], dp[j] + abs(H[j] - H[i]))
    dp[i] = min(v)
print((dp[-1]))



# Optimized solution
def number():
    return int(eval(input()))


def space():
    return list(map(int, input().split()))


N, K = space()
H = space()

dp = [0] * N

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[-1]))
# Original solution
n, k = list(map(int, input().split()))
h_l = list(map(int, input().split()))

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

# Optimized solution
n, k = list(map(int, input().split()))
h_l = list(map(int, input().split()))

dp = [float('inf')] * n
dp[0] = 0
for i in range(1, n):
    from_jump = max(0,i-k)
    dp[i] = min(dp[j]+abs(h_l[i]-h_l[j]) for j in range(from_jump,i))
print((dp[n-1]))
# Original solution
from collections import Counter
from collections import deque
import math
import bisect
import sys
def input(): return sys.stdin.readline().strip()
def mp(): return list(map(int,input().split()))
def lmp(): return list(map(int,input().split()))


n,k=mp()
h=lmp()
h.insert(0,0)
dp=[float("inf")]*(n+1)
dp[1]=0
for i in range(1,n+1):
    for j in range(1,k+1):
        if i+j<=n:
            dp[i+j]=min(dp[i+j],abs(h[i+j]-h[i])+dp[i])
print((dp[n]))


# Optimized solution
n,k = list(map(int,input().split()))
height = list(map(int,input().split()))
cost_list = []
for m in range(1,n+1):
  if m <= k:
    cost_list.append(abs(height[m-1]-height[0]))
  else:
    Min = cost_list[-1] + abs(height[m-2]-height[m-1])
    if k > 1:
      for x in range(2,k+1):
        money2 = cost_list[-x] + abs(height[m-x-1]-height[m-1])
        if money2 < Min:
          Min = money2 
    cost_list.append(Min)
print((cost_list[-1]))
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
h.insert(0, float("inf"))

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

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

print((dp[N]))

# Optimized solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
h.insert(0, float("inf"))

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

for i in range(2, N + 1):
    for k in range(1, K + 1):
        if i - k >= 0:
            dp[i] = min(dp[i], dp[i - k] + abs(h[i - k] - h[i]))
        else:
            break

print((dp[N]))
# Original solution
import sys
input = sys.stdin.readline
 
N,K = list(map(int,input().split()))
h = [0] + list(map(int,input().split()))
 
dp = [None for _ in range(N+1)]
dp[1] = 0
 
for i in range(2,N+1):
    scope = min(i-1,K)
    jump = [dp[i-x] + abs(h[i]-h[i-x]) for x in range(1,scope+1)]
    dp[i] = min(jump)
    
print((dp[N]))

# Optimized solution
N,K = list(map(int,input().split()))
h = [0] + list(map(int,input().split()))

dp = [0] * (N+1)

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

print((dp[N]))
# Original solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
INF = 10**4+1
dp = [INF]*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]))

# Optimized solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
INF = 10**4+1
dp = [INF]*n
dp[0]=0
for i in range(1,n):
  hi=h[i]
  dp[i] = min([dp[j] + abs(h[j]-hi) for j in range(max(0, i-k),i)])
print((dp[n-1]))
# Original solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
INF = 10**4+1
dp = [INF]*n
dp[0]=0
for i in range(1,n):
  hi=h[i]
  dp[i] = min([dp[j] + abs(h[j]-hi) for j in range(max(0, i-k),i)])
print((dp[n-1]))

# Optimized solution
n,k=list(map(int,input().split()))
h=list(map(int,input().split()))
INF = 10**4+1
dp = [INF]*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]))
# Original solution
# coding:utf-8

N, K = list(map(int, input().split()))
H = [int(hi) for hi in input().split()]
dp = [0]*N

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

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

print((dp[N-1]))

# Optimized solution
# coding:utf-8

N, K = list(map(int, input().split()))
H = [int(hi) for hi in input().split()]
dp = [0]*(N+1)


for i in range(1, K+1):
    if i <= N-1:
        dp[i] = abs(H[0]-H[i])

for i in range(K+1, N):
    dp[i] = min([dpk + abs(hk - H[i]) for dpk, hk in zip(dp[i-K:i], H[i-K:i])])


print((dp[N-1]))
# Original solution
import sys
import heapq
input = sys.stdin.readline

max_cost = 10**9
n, k = list(map(int, input().split()))
hs = list(map(int, input().split()))
min_cost = [float('inf')] * n
graph = [[(j, abs(hs[i] - hs[j])) for j in range(i+1, i+k+1) if j < n] for i in range(n)]

def dijkstra(source, target):
    hq = [(0, source)]
    while hq:
        cost, vertex = heapq.heappop(hq)
        min_cost[vertex] = cost
        if vertex == target:
            return min_cost[vertex]
        for adjacency_vertex, adjacency_cost in graph[vertex]:
            heapq.heappush(hq, (cost + adjacency_cost, adjacency_vertex))

print((dijkstra(0, n-1)))


# Optimized solution
n, k = list(map(int, input().split()))
hs = list(map(int, input().split()))
min_cost = [float('inf')] * n
min_cost[0] = 0
for i in range(1, n):
    min_cost[i] = min([min_cost[i-j] + abs(hs[i] - hs[i-j]) for j in range(1, k+1) if i-j >= 0])
print((min_cost[n-1]))
# Original solution
def small(a, b):
    if a > b:
        return b
    else:
        return a


def main():
    n, k = list(map(int, input().split()))
    h = tuple(map(int, input().split()))
    INF = 10 ** 9
    dp = [INF] * n
    dp[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            i_j = i + j
            if i_j < n:
                dp[i_j] = small(dp[i_j], dp[i] + abs(h[i] - h[i_j]))
    print((dp[-1]))


if __name__ == "__main__":
    main()

# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    h = tuple(map(int, input().split()))
    INF = 10 ** 9
    dp = [INF] * n
    dp[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            i_j = i + j
            if i_j < n:
                x = dp[i] + abs(h[i] - h[i_j])
                if dp[i_j] > x:
                    dp[i_j] = x
    print((dp[-1]))


if __name__ == "__main__":
    main()
# Original solution
def main():
    import sys

    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    h = tuple(map(int, input().split()))
    dp = [10 ** 9] * n
    dp[0] = 0
    for i in tuple(range(n)):
        for j in tuple(range(1, k + 1)):
            i_j = i + j
            if i_j < n:
                x = dp[i] + abs(h[i] - h[i_j])
                if dp[i_j] > x:
                    dp[i_j] = x
    print((dp[-1]))


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    import sys

    input = sys.stdin.readline
    n, k = list(map(int, input().split()))
    nt = tuple(range(n))
    kt = tuple(range(1, k + 1))
    h = tuple(map(int, input().split()))
    dp = [10 ** 9] * n
    dp[0] = 0
    for i in nt:
        for j in kt:
            i_j = i + j
            if i_j < n:
                x = dp[i] + abs(h[i] - h[i_j])
                if dp[i_j] > x:
                    dp[i_j] = x
    print((dp[-1]))


if __name__ == "__main__":
    main()
# Original solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [0] * N
for i in range(1, N):
    # 現在の足場を1~K前の足場から求める
    dp[i] = min([dp[j] + abs(h[j]-h[i]) for j in range(max(0,i-K),i)])

print((dp[-1]))

# Optimized solution
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))

dp = [0] * N
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[-1]))
# Original solution

n,k = list(map(int,input().split()))
li = list(map(int,input().split()))

ans = [0]*n
ans[0] = 0

for i in range(1,n):
    l = []
    if k > i:
        for j in range(i):
            l.append(abs(li[i]-li[j])+ans[j])
    else:
        for j in range(1,k+1):
            l.append(abs(li[i]-li[i-j])+ans[i-j])
    # print(l)
    ans[i] = min(l)

print((ans[-1]))

# Optimized solution

n,k = list(map(int,input().split()))
li = list(map(int,input().split()))

ans = [0]*n
# 地味に遅い
# for i in range(1,n):
#     l = []
#     if k > i:
#         for j in range(i):
#             l.append(abs(li[i]-li[j])+ans[j])
#     else:
#         for j in range(1,k+1):
#             l.append(abs(li[i]-li[i-j])+ans[i-j])
#     ans[i] = min(l)
# print(ans[-1])

for i in range(1,n):
    l = [ abs(li[i]-li[j]) + ans[j] for j in range(max(0,i-k),i)]
    # print(l)
    ans[i] = min(l)
print((ans[-1]))
# Original solution
inf = float('inf')
n,k=list(map(int,input().split()))
h = list(map(int,input().split()))
dp=[0] + [inf] *(n-1)#初期化
for i in range(1,n):
    for j in range(1,k+1):#iからj離れた場所から来る場合と比較
        if i >=j:
            dp[i] = min(dp[i],abs(h[i-j]-h[i])+dp[i-j])
        else:break
print((dp[n-1]))

# Optimized solution
def main():
    inf = float('inf')
    n,k=list(map(int,input().split()))
    h = list(map(int,input().split()))
    dp=[0] + [inf] *(n-1)#初期化
    for i in range(1,n):
        for j in range(1,k+1):#iからj離れた場所から来る場合と比較
            if i >=j:
                dp[i] = min(dp[i],abs(h[i-j]-h[i])+dp[i-j])
            else:break
    print((dp[n-1]))

if __name__ == '__main__':
    main()
# Original solution
# 参考: https://cinnamo-coder.hatenablog.com/entry/2019/05/11/191130


n, k = list(map(int, input().split()))
lst_h = list(map(int, input().split()))


# 1. DP[i] は、足場 i に到達するための最小コストと定義する
# 2. 状態遷移は min を使う
# 3. DPの初期化は、最小値を求めるので INF とする（足場1は例外）。
dp = [float("INF")] * n
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        # n 番目の足場を跳び越したら、次の足場を調べる
        if i + j > n - 1:
            break
        # 既存の dp[i + j] と、新たに計算する dp[i + j] の大小比較
        dp[i + j] = min(dp[i + j], dp[i] + abs(lst_h[i] - lst_h[i + j]))


# 計算量 O(nk)
ans = dp[n - 1]
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
lst_h = list(map(int, input().split()))


dp = [float("INF")] * n
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        if i + j > n - 1:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(lst_h[i] - lst_h[i + j]))


ans = dp[n - 1]
print(ans)
# Original solution
_, k = list(map(int, input().split()))
h = list(map(int, input().split()))

opt = [0] * len(h)
for i in range(1,len(h)):
    opt[i] = min([opt[i-j] + abs(h[i-j] - h[i]) for j in range(1,min(i,k)+1)])
print((opt[-1]))

# Optimized solution
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

opt = [0]
for i in range(1,n):
    opt += [min([opt[-j] + abs(h[i-j] - h[i]) for j in range(1,min(i,k)+1)])]
print((opt[-1]))
# Original solution
import sys

N, K = list(map(int, sys.stdin.readline().split()))
A = list(map(int, sys.stdin.readline().split()))

dp = [0] + [None] * (N - 1)
for n in range(1, N):
    l = max(0, n - K)
    dp[n] = min(abs(A[n] - a) + d for a, d in zip(A[l : n], dp[l : n]))

print((dp[-1]))


# Optimized solution
import sys

N, K = list(map(int, sys.stdin.readline().split()))
A = list(map(int, sys.stdin.readline().split()))

dp = [0] + [None] * (N - 1)
for n, a in enumerate(A):
    if n:
        l = max(0, n - K)
        dp[n] = min(abs(a - a2) + d for a2, d in zip(A[l : n], dp[l : n]))

print((dp[-1]))
# Original solution
def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))

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

    print((dp[n-1]))

main()

# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))

    dp = [0] * n
    for i in range(1, n):
        h = a[i]
        cost = 10**9
        for j in range(max(0, i-k), i):
            if dp[j] + abs(h - a[j]) < cost:
                cost = dp[j] + abs(h - a[j])
        dp[i] = cost

    print((dp[n-1]))

main()
# Original solution
n, k = list(map(int, input().split()))
dat_h = list(map(int, input().split()))
dp = [100000000000] * (n + 1)
dp[0] = 0
for i in range(1, n):
    #print("i:{0}", i)
    for j in range(1, k + 1):
        #print("j:{0}", j)
        if (i - j) >= 0:
            dp[i] = min(dp[i], dp[i-j] + abs(dat_h[i] - dat_h[i-j]))
#print(dp)
print((dp[n-1]))


# Optimized solution
n, k = list(map(int, input().split()))
dat_h = list(map(int, input().split()))
dp = [1000000000000000] * (n + 1)
dp[0] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if (i - j) >= 0:
            dp[i] = min(dp[i], dp[i-j] + abs(dat_h[i] - dat_h[i-j]))
print((dp[n-1]))
# Original solution
# EduDPB - Frog 2
n, k = list(map(int, input().rstrip().split()))
lst = list(map(int, input().rstrip().split()))
ans = [0] + [float("inf")] * (n - 1)
for i in range(n):
    for j in range(1, k + 1):
        if i + j >= n:
            break
        ans[i + j] = min(ans[i + j], ans[i] + abs(lst[i] - lst[i + j]))
print((ans[-1]))

# Optimized solution
# EduDPB - Frog 2
import sys
input = sys.stdin.readline

n, k = list(map(int, input().rstrip().split()))
lst = list(map(int, input().rstrip().split()))
ans = [0] + [float("inf")] * (n - 1)
for i in range(n):
    for j in range(1, k + 1):
        if i + j >= n:
            break
        ans[i + j] = min(ans[i + j], ans[i] + abs(lst[i] - lst[i + j]))
print((ans[-1]))
# Original solution
# dpB - Frog 2
# 貰うDP
def main():
    n, k = tuple(map(int, input().rstrip().split()))
    A = tuple(map(int, input().rstrip().split()))
    dp = [float("inf")] * n
    dp[0] = 0
    for i in range(1, n):
        for j in range(1, k + 1):
            if i - j >= 0:
                dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j]))
    print((dp[-1]))


if __name__ == "__main__":
    main()

# Optimized solution
# dpB - Frog 2
# 配るDP
def main():
    n, k = tuple(map(int, input().rstrip().split()))
    A = tuple(map(int, input().rstrip().split()))
    dp = [float("inf")] * 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(A[i] - A[i + j]))
    print((dp[-1]))


if __name__ == "__main__":
    main()