Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

n, a, b, k = map(int, input().split())

M = 998244353
fact_n = [0 for m in range(n+1)]
fact_inv = [0 for m in range(n+1)]


def combination(n, r):
    if n == 0 and r == 0:
        return 1
    elif n < r or n < 0:
        return 0

    c = fact_inv[r] * fact_inv[n-r] % M
    return fact_n[n] * c % M


def main():
    fact_n[0] = fact_inv[0] = 1
    for i in range(1, n+1):
        fact_n[i] = fact_n[i-1] * i % M
        fact_inv[i] = pow(fact_n[i], M-2, M)

    com = 0
    for i in range(n+1):
        if (k-a*i) % b == 0 and k - a * i >= 0:
            j = (k-a*i) // b
            com += combination(n, i) * combination(n, j) % M

    print(com % M)


if __name__ == '__main__':
    main()


### FIXED CODE ###

n, a, b, k = map(int, input().split())

M = 998244353
fact_n = [0 for m in range(n+1)]
fact_inv = [0 for m in range(n+1)]


def mod_pow(a, n):
    x = 1
    while n > 0:
        if n & 1:
            x = x * a % M

        a = a * a % M
        n >>= 1

    return x


def combination(n, r):
    if n == 0 and r == 0:
        return 1
    elif n < r or n < 0:
        return 0

    return fact_n[n] * fact_inv[r] * fact_inv[n-r] % M


def main():
    fact_n[0] = fact_inv[0] = 1
    for i in range(1, n+1):
        fact_n[i] = fact_n[i-1] * i % M

    fact_inv[n] = pow(fact_n[n], M-2, M)
    for i in range(n-1, 0, -1):
        fact_inv[i] = fact_inv[i+1] * (i+1) % M

    com = 0
    for i in range(n+1):
        if (k-a*i) % b == 0 and k - a * i >= 0:
            j = (k-a*i) // b
            com += combination(n, i) * combination(n, j) % M

    print(com % M)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

import math
from bisect import bisect_right
from bisect import bisect_left

n = int(input())
L = list(map(int,input().split()))
L.sort()
c = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        lg = L[i] + L[j]
        st = L[j] - L[i]
        if lg > L[j+1] and st < L[n-1]:
            m1 = bisect_right(L[j+1:],lg)
            m2 = bisect_left(L[j+1:],st)
            c += max(0,m1-m2)
print (c)

### FIXED CODE ###

import math
from bisect import bisect_right
from bisect import bisect_left

n = int(input())
L = list(map(int,input().split()))
L.sort()
c = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        lg = L[i] + L[j]
        st = L[j] - L[i]
        if lg > L[j+1] and st < L[n-1]:
            if lg > L[n-1]:
                m1 = n - (j+1)
            else:
                m1 = bisect_left(L[j+1:],lg)
            c += max(0,m1)
print (c)
### BUGGY CODE ###

(N,P) = map(int,input().split())

if N == 1:
    print(P)
    exit()

primelist=[]
powerlist=[]

p=2
while P >= p**N:        
    f = 0
    for q in primelist:
        if p % q == 0:
            f = 1
            break
    if f == 1:
        p += 1
        continue
    else:
        k = 0
        while P % p == 0:
            P //= p
            k += 1
        primelist.append(p)
        powerlist.append(k//N)
        p += 1

ans = 1
for i in range(len(primelist)):
    ans *= (primelist[i]**powerlist[i])

print(ans)

### FIXED CODE ###

import math

(N,P) = map(int,input().split())

if N == 1:
    print(P)
    exit()

ans = 1
p = 2
logP = math.log(P)
while N * math.log(p) <= logP:
    k = 0
    while P % p == 0:
        P //= p
        k += 1
    ans *= p**(k//N)
    if k > 0:
        logP = math.log(P)

    r = p % 6
    if r == 1:
        p += 4
    elif r == 5:
        p += 2
    else:
        p += 1

print(ans)
### BUGGY CODE ###

import sys

class DoublyLinkedList:
    def __init__(self, value = -1):
        self.value = value
        self.prev = self
        self.next = self

    def insert(self, value):
        newNode = DoublyLinkedList(value)
        newNode.next = self.next
        newNode.prev = self
        self.next.prev = newNode
        self.next = newNode

    def delete(self, value):
        node = self.next
        while node.value != -1:
            if node.value == value:
                node.next.prev = node.prev
                node.prev.next = node.next
                break
            node = node.next

    def deleteFirst(self):
        if self.next.value != -1:
            self.next = self.next.next
            self.next.prev = self

    def deleteLast(self):
        if self.prev.value != -1:
            self.prev = self.prev.prev
            self.prev.next = self

    def printAll(self):
        node = self.next
        while node.next.value != -1:
            print node.value,
            node = node.next
        print node.value

if __name__ == "__main__":
    dll = DoublyLinkedList()
    N = input()
    lines = sys.stdin.readlines()
    for line in lines:
        line = line.split()
        cmd = line[0]
        if cmd == "insert":
            dll.insert(int(line[1]))
        elif cmd == "delete":
            dll.delete(int(line[1]))
        elif cmd == "deleteFirst":
            dll.deleteFirst()
        else:
            dll.deleteLast()
    dll.printAll()

### FIXED CODE ###

import sys

class DoublyLinkedList:
    def __init__(self, value = -1):
        self.value = value
        self.prev = self
        self.next = self

    def insert(self, value):
        newNode = DoublyLinkedList(value)
        newNode.next = self.next
        newNode.prev = self
        self.next.prev = newNode
        self.next = newNode

    def delete(self, value):
        node = self.next
        while node.value != -1:
            if node.value == value:
                node.next.prev = node.prev
                node.prev.next = node.next
                break
            node = node.next

    def deleteFirst(self):
        if self.next.value != -1:
            self.next = self.next.next
            self.next.prev = self

    def deleteLast(self):
        if self.prev.value != -1:
            self.prev = self.prev.prev
            self.prev.next = self

    def printAll(self):
        node = self.next
        while node.next.value != -1:
            print node.value,
            node = node.next
        print node.value

if __name__ == "__main__":
    dll = DoublyLinkedList()
    lines = sys.stdin.readlines()
    lines.pop(0)
    for line in lines:
        if line[0] == "i":
            dll.insert(int(line[7:]))
        elif line[6] == "F":
            dll.deleteFirst()
        elif line[6] == "L":
            dll.deleteLast()
        else:
            dll.delete(int(line[7:]))
    dll.printAll()
### BUGGY CODE ###

N,K = map(int,input().split())
Pi = list(map(int,input().split()))

#check max

max=0
imax=0
for i in range(N-K+1):
  temp = sum(Pi[i:i+K])
  if max < temp:
    max = temp
    imax = i

ans=0
for i in range(imax,imax+K):
  ans +=(1/2)*(Pi[i]+1)
  
print(ans)
#n/2(n+1) / n = 1/2 (n+1)


### FIXED CODE ###

N,K = map(int,input().split())
Pi = list(map(int,input().split()))

Sim = [0]
for i in range(0,N):
  Sim.append(Sim[i]+Pi[i])
MAX = max(Sim[i+K] - Sim[i] for i in range(N+1-K))

print((MAX+K)/2)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n,m=map(int,input().split())
L=[list(map(int,input().split())) for _ in [0]*m]
for l in L:
    if l[0]==1:
        if [l[1],n] in L:print('POSSIBLE');break
else:print('IMPOSSIBLE')

### FIXED CODE ###

import sys
input = sys.stdin.readline

n,m=map(int,input().split())
a=[]
b=[]
for _ in [0]*m:
    l=list(map(int,input().split()))
    if l[0]==1:a.append(l[1])
    if l[1]==n:b.append(l[0])
print('POSSIBLE' if set(a)&set(b) else 'IMPOSSIBLE')
### BUGGY CODE ###

s = list(str(input()) )
a = [0]
b = {}
b[0] = 1
for num,i in enumerate(reversed(s)):
  a.append(a[num]+int(i)*10**num)
  if (a[num+1])%2019 in b: 
    b[(a[num+1])%2019] += 1
  else:
    b[(a[num+1])%2019] = 1
ans = 0
for i in b.values():
  ans += i*(i-1)//2
print(ans)

### FIXED CODE ###

s = input()
MOD = 2019
a =[0]*MOD
a[0] = 1
c_ = 0
ans = 0
t = 1
for i in reversed(s):
  c = (c_+int(i)*t)%MOD
  a[c] += 1
  t *= 10
  t %= MOD
  c_ = c
for i in a:
  ans += i*(i-1)//2
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import defaultdict


MAX_INT = 2**62-1
sys.setrecursionlimit(10**6)

input = sys.stdin.readline

n = int(input())
g = [[] for _ in range(n+1)]

for _ in range(n-1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)

MOD = 10**9 + 7

fact = [1] * (n+1)
fact_inv = [0] * (n+1)

for i in range(1, n + 1):
    fact[i] = fact[i-1] * i % MOD
    fact_inv[i] = pow(fact[i], MOD-2, MOD)


def dfs(memo, pre, cur, f, fi, m=MOD):
    if pre in memo and cur in memo[pre]:
        return memo[pre][cur]

    num, ways = 1, 1

    for nxt in g[cur]:
        if nxt == pre:
            continue
        n, w = dfs(memo, cur, nxt, f, fi)
        num += n
        ways = ways * w % m * fi[n] % m

    ways = ways * f[num-1] % m
    memo[pre, cur] = num, ways
    return num, ways

memo = defaultdict(dict)

for i in range(1, len(g)):
    print(dfs(memo, None, i, fact, fact_inv)[1])


### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
from functools import reduce

MAX_INT = 2**62-1
sys.setrecursionlimit(10**7)

input = sys.stdin.readline

# @profile
MOD = 10**9 + 7


def mul_mod(*args, mod=MOD):
    return reduce(lambda a, b: a * b % mod, args)


def solve(g, f, fi, m):
    mul = mul_mod

    stack = [(0, 1)]
    pre_order = []
    n = len(g) - 1

    while stack:
        pre, cur = stack.pop()
        pre_order.append((pre, cur))

        for nxt in g[cur]:
            if nxt == pre:
                continue
            stack.append((cur, nxt))

    size = [1] * (n+1)
    dp = [1] * (n+1)

    for pre, cur in reversed(pre_order):
        dp[cur] = mul(dp[cur], f[size[cur]-1])
        dp[pre] = mul(dp[pre], dp[cur], fi[size[cur]])
        size[pre] += size[cur]

    ans = [1] * (n+1)
    ans[1] = dp[1]

    for pre, cur in pre_order[1:]:
        i = size[cur]
        temp = mul(ans[pre], f[n-i-1], fi[n-1], f[i], pow(dp[cur], m-2, m))
        ans[cur] = mul(f[n-1], temp, fi[n-i], dp[cur], fi[i-1])
    print(*ans[1:], sep='\n')


# @profile
def main():
    mul = mul_mod
    mod = MOD
    n = int(input())
    g = [[] for _ in range(n+1)]

    fact = [1] * (n+1)
    fact_inv = [0] * (n+1)

    for i in range(1, n+1):
        fact[i] = mul(fact[i-1], i)

    fact_inv[-1] = pow(fact[-1], mod-2, mod)

    for i in range(n)[::-1]:
        fact_inv[i] = mul(fact_inv[i+1], (i+1))

    for _ in range(n-1):
        a, b = map(int, input().split())
        g[a].append(b)
        g[b].append(a)

    solve(g, fact, fact_inv, mod)


if __name__ == "__main__":

    main()
### BUGGY CODE ###

a,b,m=map(int,input().split())
ai=list(map(int, input().split()))
bi=list(map(int, input().split()))
sm=[list(map(int, input().split())) for _ in range(m)]
ans=10**6
for i in range(a):
    for j in range(b):
        for x in range(m):
            k=ai[i]+bi[j]
            if sm[x][0]==i+1 and sm[x][1]==j+1:
                k=k-sm[x][2]
            if ans >=k:
                ans=k
print(ans)


### FIXED CODE ###

a,b,m=map(int,input().split())
ai=list(map(int, input().split()))
bi=list(map(int, input().split()))
sm=[list(map(int, input().split())) for _ in range(m)]
k=min(ai)+min(bi)

for x in range(m):
    
    l=ai[sm[x][0]-1]+bi[sm[x][1]-1]-sm[x][2]
    k=min(k,l)

print(k)
### BUGGY CODE ###

N, M = map(int, input().split())
H = list(map(int, input().split()))
adj = set()
for i in range(M):
    a, b = map(int, input().split())
    adj.add((min(a-1, b-1), max(a-1, b-1)))

ans = 0
for i in range(N):
    u = H[i]
    check = True
    for a, b in adj:
        if i == a:
            if not u > H[b]:
                check = False
        elif i == b:
            if not u > H[a]:
                check = False
        else:
            pass
    if check:
        ans += 1
print(ans)


### FIXED CODE ###

from collections import defaultdict
N, M = map(int, input().split())
H = list(map(int, input().split()))
adj = defaultdict(set)
for i in range(M):
    a, b = map(int, input().split())
    adj[a-1].add(H[b-1])
    adj[b-1].add(H[a-1])
ans = 0
for i in range(N):
    adj[i].add(-1)
    if H[i] > max(adj[i]):
        ans += 1
print(ans)
### BUGGY CODE ###

N = int(input())
L = list(map(int, input().split()))
L.sort()

from bisect import bisect_left

cnt = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        t = L[i] + L[j]
        idx = bisect_left(L, t, lo=j + 1, hi=len(L))
        cnt += max(0, idx - j - 1)
print(cnt)


### FIXED CODE ###

N = int(input())
L = list(map(int, input().split()))
L.sort()
 
from bisect import bisect_left 
cnt = 0
for i in range(0,N-2):
  for j in range(i+1,N-1):
    t = L[i]+L[j]
    idx = bisect_left(L,t,j+1)
    cnt += max(0,idx-j-1)
print(cnt)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
from itertools import product
def resolve():
    h,w=map(int,input().split())
    A=[list(map(int,input().split())) for _ in range(h)]
    B=[list(map(int,input().split())) for _ in range(h)]
    dp=[[defaultdict(int) for _ in range(w)] for _ in range(h)]
    x=A[-1][-1]-B[-1][-1]
    dp[-1][-1][x]=1
    dp[-1][-1][-x]=1

    for i,j in product(range(h-1,-1,-1),range(w-1,-1,-1)):
        x=A[i][j]-B[i][j]
        if(i+1<h):
            for key,val in dp[i+1][j].items():
                if(val==1):
                    dp[i][j][key+x]=1
                    dp[i][j][key-x]=1
        if(j+1<w):
            for key,val in dp[i][j+1].items():
                if(val==1):
                    dp[i][j][key+x]=1
                    dp[i][j][key-x]=1

    ans=INF
    for key,val in dp[0][0].items():
        if(val==1): ans=min(ans,abs(key))

    print(ans)
resolve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    h,w=map(int,input().split())
    A=[list(map(int,input().split())) for _ in range(h)]
    B=[list(map(int,input().split())) for _ in range(h)]
    dp=[[0]*w for _ in range(h)]
    x=abs(A[0][0]-B[0][0])
    offset=12800
    dp[0][0]|=1<<(x+offset)
    dp[0][0]|=1<<(-x+offset)

    from itertools import product
    for i,j in product(range(h),range(w)):
        if(i==0 and j==0): continue
        x=abs(A[i][j]-B[i][j])
        S=0
        if(i>0): S|=dp[i-1][j]
        if(j>0): S|=dp[i][j-1]
        dp[i][j]=(S<<x)|(S>>x)

    for i in range(12800):
        if((dp[-1][-1]>>(i+offset))&1):
            print(i)
            return
resolve()
### BUGGY CODE ###

import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

r, g, b, n = inintm()

ans = 0

for i in range(3001):
    for j in range(3001):
        if (n - (i*r + j*g)) >= 0 and (n - (i*r + j*g)) % b == 0:
            ans += 1

print(ans)


### FIXED CODE ###

import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

r, g, b, n = inintm()

ans = 0

for i in range(3001):
    for j in range(3001):
        if (n - (i*r + j*g)) >= 0 and (n - (i*r + j*g)) % b == 0:
            ans += 1

print(ans)
### BUGGY CODE ###

#abc093 b
a,b,k=map(int,input().split())
ans=[]
for i in range(a,b+1):
    if i<=a+k-1 or b-k+1<=i:
        ans.append(i)
for i in ans:
    print(i)


### FIXED CODE ###

#abc093 b
a,b,k=map(int,input().split())
l1=[i for i in range(a,min(a+k,b+1))]
l2=[i for i in range(max(a,b-k+1),b+1)]
ans=set(l1+l2)
for i in sorted(ans):
    print(i)
### BUGGY CODE ###

def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

prime_list = primes(10**7)
p_list = set(prime_list)

n, m = map(int, input().split())

ans = 1

for i in prime_list:
    if n * i > m:
        break
    if m % i == 0:
        num = m // i
        if num in p_list:
            ans = i
        elif num < n:
            continue
        elif num >= i * n:
            a = num // n
            ans = i * a
    else:
        continue

print(ans)

### FIXED CODE ###

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

n, m = map(int, input().split())

ans = 1

div = make_divisors(m)

for i in div:
    if i * n <= m:
        ans = i
    else:
        break

print(ans)
### BUGGY CODE ###

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


n = 60
N = read_a_int()
A = read_ints()

import numpy as np


def ret_binary(a: int):  # 逆順で返すことに注意
    ret_r = [0] * n
    for i in range(n):
        bit = a % 2
        a = a // 2
        ret_r[i] = bit
    return np.array(ret_r, dtype='int')


def calc_sum(a_binary, n_1_ls, n_1_max):
    '''
    a_binaryはxorしてから和を取りたい数
    n_1_lsは各桁の1の数
    n_1_maxは最大1は何個あり得るか
    '''
    ret = 0
    for i, (aa, n_1) in enumerate(zip(a_binary, n_1_ls)):
        if aa == 0:
            ret += (((2**i) % MOD) * n_1) % MOD
        else:
            ret += (((2**i) % MOD) * (n_1_max - n_1)) % MOD
    return ret


MOD = 10**9 + 7

n_1_max = 0
n_1_ls = np.zeros(n, dtype='int')
ans = 0
a_pre = A[N - 1]
a_pre_binary = ret_binary(a_pre)
for a in A[-2::-1]:
    a_binary = ret_binary(a)
    n_1_ls = n_1_ls + a_pre_binary
    n_1_max += 1
    tmp = calc_sum(a_binary, n_1_ls, n_1_max)
    ans += tmp
    ans %= MOD
    a_pre_binary = a_binary

print(ans)


### FIXED CODE ###

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


n = 60
N = read_a_int()
A = read_ints()


def ret_binary(a: int):  # 逆順で返すことに注意
    ret_r = [0] * n
    for i in range(n):
        bit = a % 2
        a = a // 2
        ret_r[i] = bit
    return ret_r


MOD = 10**9 + 7

n_1_max = 0
n_1_ls = [0] * n
ans = 0
for a in A:
    tmp = ret_binary(a)
    for i in range(n):
        n_1_ls[i] += tmp[i]


for i, n_1 in enumerate(n_1_ls):
    num_sonoketa = n_1 * (N - n_1)
    ans += (((2**i) % MOD) * num_sonoketa)
    ans %= MOD
print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
n, m = [int(x) for x in input().strip().split()]
a = sorted(list(set([int(x)//2 for x in input().strip().split()])))
def lcm(a, b):
    ret = a * b // gcd(a, b)
    ret_ = ret
    if gcd(a, b) != 1:
        while (ret // a) % 2 == 0 or (ret // b) % 2 == 0:
            ret += ret_

    return ret

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

lcm_ = a[0]
for aa in a[1:]:
    lcm_ = lcm(aa, lcm_)

print((m // lcm_ + 1) // 2)

### FIXED CODE ###

import sys
input = sys.stdin.readline
n, m = [int(x) for x in input().strip().split()]
a = list(set([int(x)//2 for x in input().strip().split()]))

def lcm(a, b):
    return a * b // gcd(a, b)

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

def div2cnt(x):
    ret = 0
    while x % 2 == 0:
        ret += 1
        x //= 2
    return ret

cnt = div2cnt(a[0])

for i, aa in enumerate(a):
    if div2cnt(aa) != cnt:
        print(0)
        exit()
    a[i] >>= cnt
else:
    m >>= cnt

lcm_ = 1
for aa in a:
    lcm_ = lcm(lcm_, aa)
    if lcm_ > m:
        print(0)
        exit()

print((m // lcm_ + 1) // 2)
### BUGGY CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, maxint
from operator import itemgetter
from heapq import heappush, heappop, heapify


def main():
    num = int(stdin.readline())

    edges = []
    for _ in xrange(num):
        it = iter(int(s) for s in stdin.readline().split()[1:])
        edges.append([(next(it), next(it)) for _ in xrange(next(it))])

    distances = [[maxint, i] for i in xrange(num)]
    distances[0][0] = 0
    V = set(xrange(1, num))
    W = []#distances[1:]
    mark = set()
    index = 0

    while V:
        for v, cost in edges[index]:
            if v not in V:
                continue
            cost += distances[index][0]
            if distances[v][0] > cost:
                distances[v][0] = cost
                if v not in mark:
                    W.append(distances[v])
                    mark.add(v)

        a = min(W, key=itemgetter(0))
        index = a[1]
        W.remove(a)
        mark.remove(index)
        V.remove(index)

    for d, i in distances:
        print(i, d)

main()

### FIXED CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, maxsize


def main():
    num = int(stdin.readline())

    edges = []
    for _ in range(num):
        ns = [int(s) for s in stdin.readline().split()]
        for i in range(ns[1]):
            edges.append((ns[0], ns[2*i+2], ns[2*i+3]))

    dis = [maxsize] * num
    dis[0] = 0

    while 1:
        update = False
        for from_, to, cost in edges:
            if dis[from_] < maxsize and dis[to] > cost + dis[from_]:
                dis[to] = cost + dis[from_]
                update = True

        if not update:
            break

    for i, d in enumerate(dis):
        print(i, d)


main()
### BUGGY CODE ###

N, K = map(int, input().split())
al = list(map(int, input().split()))

route = [1]
for i in range(K):
    next_town = al[route[i]-1]
    if next_town in route:
        loop_start = route.index(next_town)
        loop_end = len(route)
        loop_length = loop_end - loop_start
        if K >= loop_end:
            amari = (K - loop_start)%loop_length
            ans = route[loop_start + amari]
            #print(route)
            print(ans)
            exit()
    else:
        route.append(next_town)
ans = route[-1]
print(ans)


### FIXED CODE ###

from collections import defaultdict


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


N, K = inpl()
aa = inpl()

s = 1

route = [s]
ed = set([s])

for i in range(1, K + 1):
    s = aa[s - 1]
    if s in ed:
        Li = route.index(s)
        Ri = len(route)
        idx = Li + (K - Li) % (Ri - Li)
        print(route[idx])
        break
    else:
        route.append(s)
        ed.add(s)
        continue
else:
    print(s)
### BUGGY CODE ###

def test():
    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    dp = [float('inf')]*N
    dp[0] = 0
 
    for i in range(1, N):
        dp[i] = min(dp[j] + abs(H[i] - H[j]) for j in range(max(i-K, 0), i))
 
    print(dp[-1])
 
if __name__ == '__main__':
    test()

### FIXED CODE ###

n,k=map(int,input().split())
H=list(map(int,input().split()))
 
dp=[float('inf') for _ in range(n)]
dp[0]=0
for i,hi in enumerate(H[1:],1):
  s=max(0,i-k)
  dp[i]=min([aj + abs(hi-hj) for hj,aj in zip(H[s:i],dp[s:i])])
print(dp[-1])