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

import itertools

N=int(input())
actlist=list(map(int,input().split()))
alllist=list(itertools.permutations(range(1,N+1)))

count=1
l=len(alllist)
maxnum=0

for i in range(0,l-1):
    for j in range(i+1,l):
        hapsum=0
        for m,n in zip(alllist[i],alllist[j]):
            hapsum+=actlist[m-1]*abs(m-n)
        if maxnum<hapsum:
            maxnum=hapsum
        
print(maxnum)

### FIXED CODE ###

def main():
  N = int(input())
  INF = float('inf')
  A = [(i+1, a) for i, a in enumerate(map(int, input().split()))]
  A = sorted(A, key=lambda x: x[1], reverse = True)
 
  dp = [[-INF] * (N+1) for _ in range(N+1)]
  dp[0][0] = 0
 
  for s in range(1, N+1):
    for l in range(s+1):
      r = s - l
      dp[l][r] = max(dp[l-1][r] + A[s-1][1] * abs(A[s-1][0]-l), dp[l][r-1] + A[s-1][1] * abs(N-r+1-A[s-1][0]))
    
  ans = 0
  for m in range(N):
    if(dp[m][N-m] > ans):
      ans = dp[m][N-m]
    
  print(ans)
 
main()
### BUGGY CODE ###

S = input()
a = 0
b = 0
c = 0
p = 1
mod = 1000000000+7
for C in S:
    if C == "?":
        c = 3*c+b
        b = 3*b+a
        a = 3*a+p
        p *= 3
    elif C == "A":
        a += p
    elif C == "B":
        b += a
    elif C == "C":
        c += b
    a %= mod
    b %= mod
    c %= mod
print(c)

### FIXED CODE ###

S = input()
a = 0
b = 0
c = 0
p = 1
mod = 1000000000+7
for C in S:
    if C == "?":
        c = 3*c+b
        b = 3*b+a
        a = 3*a+p
        p *= 3
    elif C == "A":
        a += p
    elif C == "B":
        b += a
    elif C == "C":
        c += b
    a %= mod
    b %= mod
    c %= mod
    p %= mod
print(c)
### BUGGY CODE ###

N = int(input())
t = []
for i in range(N):
  t.append(int(input()))
  
t.sort()
t.reverse()

lcm = t[0]  
  
for i in range(1,N):
  j = 1
  while(lcm % t[i] != 0):
    lcm = lcm* (j+ 1)// j
    j+= 1
    
print(lcm)

### FIXED CODE ###

import fractions
N = int(input())
t = []
for i in range(N):
  t.append(int(input()))
  
lcm = 1

for i in range(N):
  lcm = lcm* t[i]// fractions.gcd(lcm, t[i])
  
print(lcm)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
     n,m=map(int,input().split())
     field=[[] for i in range(3*n)]
     for i in range(m):
          x,y=map(int,input().split())
          x,y=x-1,y-1
          field[3*x].append(3*y+1)
          field[3*x+1].append(3*y+2)
          field[3*x+2].append(3*y)
     
     s,t=map(int,input().split())
     s,t=s-1,t-1
     s,t=3*s,3*t
     
     que=[s]
     ok=[s]
     cnt=0
     while que!=[]:
          que2=[]
          cnt+=1
          for i in que:
               for j in field[i]:
                    if j not in ok:
                         ok.append(j)
                         if j==t:
                              return cnt//3
                         que2.append(j)
          que=que2
     return -1

if __name__ == '__main__':
    print(main())

### FIXED CODE ###

import sys
input = sys.stdin.readline
 
def main():
     n,m=map(int,input().split())
     field=[[] for i in range(3*n)]
     for i in range(m):
          x,y=map(int,input().split())
          x,y=x-1,y-1
          field[3*x].append(3*y+1)
          field[3*x+1].append(3*y+2)
          field[3*x+2].append(3*y)
     
     s,t=map(int,input().split())
     s,t=s-1,t-1
     s,t=3*s,3*t
     
     d=[float('inf') for i in range(3*n)]
     d[s]=0
     
     que=[s]
     cnt=0
     while que!=[]:
          que2=[]
          cnt+=1
          for i in que:
               for j in field[i]:
                    if d[j]>cnt:
                         d[j]=cnt
                         que2.append(j)
          que=que2
     if d[t]==float('inf'):
          print(-1)
     else:
          print(d[t]//3)
     return 
 
if __name__ == '__main__':
    main()
### BUGGY CODE ###

A, B, N = list(map(int, input().split()))
fb = 0
for x in range(N + 1):
    f = A * x // B - A * (x // B)
    if f < fb:
        break
    fb = f
print(fb)

### FIXED CODE ###

A, B, N = list(map(int, input().split()))

if N >= B:
    max = (A * (B - 1) ) // B
else:
    max = A * N // B
print(max)
### BUGGY CODE ###

def match(ch1, ch2):
    return ch1 == ch2 or ch1 == '?' or ch2 == '?'

def solve():
    MAXLEN = 2000
    a = raw_input()
    b = raw_input()
    c = raw_input()
    lA = len(a); lB = len(b); lC = len(c)
    MAXLEN = max(lA, lB, lC)
    ab = [True for i in range(10*MAXLEN)]
    ac = [True for i in range(10*MAXLEN)]
    bc = [True for i in range(10*MAXLEN)]
    for i in range(lA):
        for j in range(lB):
            if not match(a[i], b[j]):
                ab[i - j + 5 * MAXLEN] = False
    for i in range(lA):
        for j in range(lC):
            if not match(a[i], c[j]):
                ac[i - j + 5 * MAXLEN] = False
    for i in range(lB):
        for j in range(lC):
            if not match(b[i], c[j]):
                bc[i - j + 5 * MAXLEN] = False
    ret = lA + lB + lC
    for i in range(-2*MAXLEN, 2*MAXLEN+1):
        for j in range(-2*MAXLEN, 2*MAXLEN+1):
            if ab[i + 5*MAXLEN] and ac[j + 5*MAXLEN] and bc[j - i + 5*MAXLEN]:
                L = min(0, i, j)
                R = max(lA, lB + i, lC + j)
                ret = min(ret, R - L)
    print ret

if __name__ == '__main__':
    solve()


### FIXED CODE ###

def match(ch1, ch2):
    return ch1 == ch2 or ch1 == '?' or ch2 == '?'

def solve():
    MAXLEN = 2000
    a = raw_input()
    b = raw_input()
    c = raw_input()
    lA = len(a); lB = len(b); lC = len(c)
    MAXLEN = max(lA, lB, lC)
    ab = [True for i in range(10*MAXLEN)]
    ac = [True for i in range(10*MAXLEN)]
    bc = [True for i in range(10*MAXLEN)]
    for i in range(lA):
        for j in range(lB):
            if not match(a[i], b[j]):
                ab[i - j + 5 * MAXLEN] = False
    for i in range(lA):
        for j in range(lC):
            if not match(a[i], c[j]):
                ac[i - j + 5 * MAXLEN] = False
    for i in range(lB):
        for j in range(lC):
            if not match(b[i], c[j]):
                bc[i - j + 5 * MAXLEN] = False
    ret = lA + lB + lC
    for i in range(-2*MAXLEN, 2*MAXLEN+1):
        for j in range(-2*MAXLEN, 2*MAXLEN+1):
            if i < -MAXLEN and j > 0:
                continue
            if i > MAXLEN and j < 0:
                continue
            if j < -MAXLEN and i > 0:
                continue
            if j > MAXLEN and i < 0:
                continue
            if ab[i + 5*MAXLEN] and ac[j + 5*MAXLEN] and bc[j - i + 5*MAXLEN]:
                L = min(0, i, j)
                R = max(lA, lB + i, lC + j)
                ret = min(ret, R - L)
    print ret

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

# dp[i] := i番目の人が選ばれたときの踏み台

def main():
    # INF = 10 ** 9
    N = int(input())
    A = list(map(int, input().split()))

    # initialize
    dp = [0] * (N + 10)
    dp[0] = A[0]

    res = 0
    for i in range(1, N):
        for j in range(10**9):
            tmp = A[i] + j
            if dp[i-1] <= tmp:
                dp[i] = tmp
                res += j
                break
    print(res)

if __name__ == "__main__":
    main()

### FIXED CODE ###

# maをi-1番目までにでてる最大値としてi番目との差分を足していく

def main():
    # INF = 10 ** 9
    N = int(input())
    A = list(map(int, input().split()))

    # initialize
    ma = A[0]

    res = 0
    for i in range(1, N):
        if A[i] <= ma:
            res += ma - A[i]
        else:
            ma = A[i]
    print(res)

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

l,r=map(int,input().split())
g = list([i%2019 for i in range(l,r+1)])
if r - l >= 2019:
    print(0)
    exit()
m=[]
for i in range(len(g)-1):
    for j in range(i+1,len(g)):
        m.append(g[i]*g[j]%2019)
print(min(m))

### FIXED CODE ###

l,r=map(int,input().split())
if r - l >= 2019:
    print(0)
    exit()
g = list([i%2019 for i in range(l,r+1)])
m=[]
for i in range(len(g)-1):
    for j in range(i+1,len(g)):
        m.append(g[i]*g[j]%2019)
print(min(m))
### BUGGY CODE ###

from operator import mul
from functools import reduce

if __name__ == "__main__":
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    score = reduce(mul, A[:K])
    for i in range(K, len(A)):
        if A[i-K] < A[i]:
            print("Yes")
        else:
            print("No")


### FIXED CODE ###

if __name__ == "__main__":
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    for i in range(K, len(A)):
        if A[i-K] < A[i]:
            print("Yes")
        else:
            print("No")
### BUGGY CODE ###

n, k = map(int, input().split())
arr = list(map(int, input().split()))
num_arr = [1]*n
length_arr = []
for i in arr:
  length_arr += [i]

for i in range(k):
  max_idx = length_arr.index(max(length_arr))
  tmp = num_arr[max_idx] + 1
  num_arr[max_idx] = tmp
  length_arr[max_idx] = arr[max_idx] / tmp

result_int = max(length_arr) - 0.0001
print(int(result_int//1 + 1))

### FIXED CODE ###

import math
n, k = map(int, input().split())
arr = list(map(int, input().split()))

max_length = max(arr)
low = 1
high = max_length - 1

while low <= high:
  mid = (low + high) //2
  cnt = 0
  for i in range(n):
    cnt += math.ceil(arr[i]/mid) - 1
  if cnt > k:
    low = mid + 1
  else:
    high = mid -1
    max_length = mid

print(max_length)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
q = int(input())
a_list = [[i, 0] for i in range(10**5 +1)]

for elem in a:
    a_list[elem][1] += 1

for i in range(q):
    b, c = map(int,(input().split()))
    a_list[c][1] += a_list[b][1]
    a_list[b][1] = 0
    ans = 0
    for j in a_list:
        ans += j[0] * j[1]
    print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
q = int(input())
a_dic = {}

for elem in a:
    if elem not in a_dic:
        a_dic[elem] = 1
    else:
        a_dic[elem] += 1

a_sum = sum(a)

for i in range(q):
    b, c = map(int,input().split())
    if b in a_dic:
        diff = c - b
        a_sum += diff * a_dic[b]
        print(a_sum)
        if c in a_dic:
            a_dic[c] += a_dic[b]
            del a_dic[b]
        else:
            a_dic[c] = a_dic[b]
            del a_dic[b]
    else:
        print(a_sum)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
    k, s = map(int, input().split())
    vals = [x + y + z for x in range(k+1) for y in range(k+1) for z in range(k+1)]
    print(len([v for v in vals if v == s]))

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
    k, s = map(int, input().split())
    ans = 0
    for x in range(k+1):
        if x > s:
            break
        for y in range(k+1):
            if x + y > s:
                break
            if s - (x + y) <= k:
                ans += 1

    print(ans)

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

R,G,B,N = map(int,input().split())

li = [R,G,B]
li.sort()
R = li[0]
G = li[1]
B = li[2]
#print(R,G,B)

ans = 0
for b in range(N//B+1):
    for g in range((N-B*b)//G+1):
        if N-B*b-G*g >= 0 and (N-B*b-G*g)%R == 0:
            ans += 1
print(ans)


### FIXED CODE ###

R,G,B,N = map(int,input().split())

li = [R,G,B]
li.sort(reverse=True)
R = li[0]
G = li[1]
B = li[2]
#print(R,G,B)

ans = 0
for b in range(N//B+1):
    for g in range((N-B*b)//G+1):
        if N-B*b-G*g >= 0 and (N-B*b-G*g)%R == 0:
            ans += 1
print(ans)
### BUGGY CODE ###


def solve(n, k, arr, temp):
    size = k
    t = temp
    for i in range(k, n):
        t  = temp // arr[i-k]
        t *= arr[i]
        if t > temp:
            print('Yes')
        else:
            print('No')

        temp = t




def main():
    [n, k] = [int(x) for x in input().split()]
    arr = [int(x) for x in input().split()]

    temp = 1
    for i in range(k):
        temp *= arr[i]

    solve(n, k, arr, temp)


main()


### FIXED CODE ###

def main():
    [n, k] = [int(x) for x in input().split()]
    arr = [int(x) for x in input().split()]

    for i in range(k, n):
        if arr[i-k] >= arr[i]:
            print('No')
        else:
            print('Yes')



main()
### BUGGY CODE ###

import copy

def num_from_card(card):
    return int(card[2:])

def partition(A, p, r):
    x = num_from_card(A[r])
    i = p-1
    for j in range(p, r):
        if num_from_card(A[j]) <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1

def quick_sort(A, p, r):
    if p < r:
        q = partition(A, p, r)
        quick_sort(A, p, q-1)
        quick_sort(A, q+1, r)

def is_stable(A, sorted_A, N):
    A_nums = [int(x[2:]) for x in A]
    same_num = [x for x in set(A_nums) if A_nums.count(x) > 1]
    A_symbols, sorted_A_symbols = [], []
    flag = 'Stable'
    for num in same_num:
        if [card[0] for card in A if int(card[2:]) == num] != [card[0] for card in sorted_A if int(card[2:]) == num]:
            flag = 'Not stable'
            break
    return flag

n = int(input())
cards = [input() for i in range(n)]
init_cards = copy.copy(cards)

quick_sort(cards, 0, n-1)
print(is_stable(init_cards, cards, n))
for c in cards:
    print(c)


### FIXED CODE ###

import copy

def num_from_card(card):
    return int(card[2:])

def partition(A, p, r):
    x = num_from_card(A[r])
    i = p-1
    for j in range(p, r):
        if num_from_card(A[j]) <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1

def quick_sort(A, p, r):
    if p < r:
        q = partition(A, p, r)
        quick_sort(A, p, q-1)
        quick_sort(A, q+1, r)

def merge(A, left, mid, right):
    L = A[left: mid]
    R = A[mid: right]
    L.append('  ' + str(int(10e9 + 1)))
    R.append('  ' + str(int(10e9 + 1)))
    i, j = 0, 0
    for k in range(left, right):
        if num_from_card(L[i]) <= num_from_card(R[j]):
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1

def merge_sort(A, left, right):
    if left + 1 < right:
        mid = int((left + right)/2)
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)

n = int(input())
cards = [input() for i in range(n)]
merge_cards = copy.copy(cards)

quick_sort(cards, 0, n-1)
merge_sort(merge_cards, 0, n)

if merge_cards == cards:
    print('Stable')
else:
    print('Not stable')

for c in cards:
    print(c)
### BUGGY CODE ###

N,M = list(map(int,input().split()))
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

a = list(map(int,input().split()))
count2 = set()
for i in a:
    prime = prime_factorize(i)
    count2.add(prime.count(2))
if (len(count2) == 1) and (0 not in count2):
    a2 = [int(i//2) for i in a]
    if len(a2) == 1:
        div = M//a2[0]
        print((div+1)//2)
    else:
        gcda = a2[0]*a2[1]//gcd(a2[0],a2[1])
        for i in range(2,N):
            gcda = gcda*a2[i]//gcd(gcda,a2[i])
        div = M//gcda
        print((div+1)//2)
    
else:
    print(0)

### FIXED CODE ###

N,M = list(map(int,input().split()))
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
def twocount(n):
    c = 0
    while n%2 == 0:
        n //= 2
        c += 1
    return c
 
a2 = [int(i//2) for i in map(int,input().split())]
count2 = set()
for i in a2:
    tc = twocount(i)
    count2.add(tc)
if len(count2) == 1:
 
    if len(a2) == 1:
        div = M//a2[0]
        print((div+1)//2)
    else:
        gcda = a2[0]*a2[1]//gcd(a2[0],a2[1])
        for i in range(2,N):
            gcda = gcda*a2[i]//gcd(gcda,a2[i])
        div = M//gcda
        print((div+1)//2)
    
else:
    print(0)
### BUGGY CODE ###

S=input()
N=len(S)

pos=[1]*N

for i in range(10**100):
    for j in range(N):
        for k in range(pos[j]):
            if S[j]=="R":
                pos[j]-=1
                pos[j+1]+=1
            elif S[j]=="L":
                pos[j]-=1
                pos[j-1]+=1

print(" ".join(pos))

### FIXED CODE ###

S=list(input())
N=len(S)

ans=[0]*N
r=0
for i in range(N):
  if S[i]=="R":
    r+=1
  else:
    if r%2==0:
      ans[i-1]+=r//2
      ans[i]+=r//2
    else:
      ans[i-1]+=r//2+1
      ans[i]+=r//2
    r=0

S.reverse()
ans.reverse()
l=0
for i in range(N):
  if S[i]=="L":
    l+=1
  else:
    if l%2==0:
      ans[i-1]+=l//2
      ans[i]+=l//2
    else:
      ans[i-1]+=l//2+1
      ans[i]+=l//2
    l=0

ans.reverse()
print(" ".join(map(str,ans)))
### BUGGY CODE ###

s = input()
d = {}
for i in range(len(s)):
  r = int(s[i:])%2019
  if r in d:
    d[r] += 1
  else:
    d[r] = 1
ans = 0
if 0 in d:
  ans += d[0]
for k,v in d.items():
  if v >= 2:
    ans += v*(v-1)//2
print(ans)


### FIXED CODE ###

s = list(map(int,input()))
d = [0 for _ in range(2019)]
r = 0
x = 1
for si in reversed(s):
  r += si*x
  r %= 2019
  d[r] += 1
  x *= 10
  x %= 2019
ans = d[0]
for v in d:
  if v >= 2:
    ans += v*(v-1)//2
print(ans)
### BUGGY CODE ###

N,M = map(int,input().split())
L = [0] * M
R = [0] * M
for i in range (0,M):
    L[i],R[i] = map(int,input().split())

S = [set(range(L[i],R[i]+1)) for i in range(0,M)]

temp = S[0]

for i in range(1,M):
    temp = temp & S[i]

print(len(temp))

### FIXED CODE ###

N,M = map(int,input().split())
L = [0] * M
R = [0] * M
for i in range (0,M):
    L[i],R[i] = map(int,input().split())

print(len(range(max(L),min(R)+1)))
### BUGGY CODE ###

from bisect import bisect
inputs = open(0).readlines()

n, m = map(int, inputs[0].split())
*A, = map(int, inputs[1].split())
BC = [tuple(map(int, bc.split())) for bc in inputs[2:]]

for b, c in BC:
    A, b, c
    A.sort()
    idx = bisect(A, c)
    for i in range(min(b, idx)):
        A[i] = c
print(sum(A))

### FIXED CODE ###

from heapq import heapify, heappushpop
from operator import itemgetter
inputs = open(0).readlines()

n, m = map(int, inputs[0].split())
*A, = map(int, inputs[1].split())
heapify(A)
BC = [tuple(map(int, bc.split())) for bc in inputs[2:]]
BC.sort(key=itemgetter(1), reverse=True)

for b, c in BC:
    for _ in range(b):
        if c > A[0]:
            _ = heappushpop(A, c)
        else:
            break
print(sum(A))
### BUGGY CODE ###

N, M = map(int, input().split())
LR = []
for i in range(M):
    l, r = map(int, input().split())
    LR.append([l, r])

A = [0] * (N+1)

for l, r in LR:
    for i in range(l, r + 1):
        A[i] += 1

print(A.count(M))

### FIXED CODE ###

from collections import Counter

N, M = map(int, input().split())
L = []
R = []

for i in range(M):
    l, r = map(int, input().split())
    L.append(l)
    R.append(r)

maxL = max(L)
minR = min(R)

ans = max(0, minR - maxL + 1)
print(ans)
### BUGGY CODE ###

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

from collections import deque
INF=float("inf")
n,x,y=MI()
x-=1
y-=1
ans=[0]*n

for sv in range(n):
  dist=[INF]*n
  q=deque()
  def push(v,d):
    if dist[v]!=INF:
      return
    dist[v]=d
    q.append(v)
  push(sv,0)
  while q:
    v=q.popleft()
    d=dist[v]
    if v-1>=0:
      push(v-1,d+1)
    if v+1<n:
      push(v+1,d+1)  
    if v==x:
      push(y,d+1)
    if v==y:
      push(x,d+1)
        
  for i in range(n):
    ans[dist[i]]+=1
for i in range(n):
  ans[i]//=2
for i in range(1,n):
  print(ans[i])

### FIXED CODE ###

def II():return int(input())
def MI():return map(int,input().split())
def LI():return list(MI())
def ok():print("Yes")
def ng():print("No");exit()

n,x,y=MI()

ans=[0]*n

for i in range(1,n+1):
    for j in range(i+1,n+1):
        num=min(abs(i-j),abs(x-i)+abs(y-j)+1)
        ans[num]+=1

for an in ans[1:]:
    print(an)
### BUGGY CODE ###

N,Q = map(int,input().split())
S = input()
l = [0] * Q
r = [0] * Q
a = []
for i in range(Q):
    l[i],r[i] = map(int,input().split())

for i in range(N-1):
    
    if S[i] == 'A' and S[i+1] == 'C':

        a.append(1)

    else:

        a.append(0)

for i in range(Q):

    print(a[l[i]-1:r[i]-1].count(1))

### FIXED CODE ###

N,Q = map(int,input().split())
S = input()
l = [0] * Q
r = [0] * Q
a = 0
b = [0]
for i in range(Q):
    l[i],r[i] = map(int,input().split())

for i in range(N-1):
    
    if S[i] == 'A' and S[i+1] == 'C':

        a += 1
        b.append(a)

    else:

        b.append(a)

for i in range(Q):

    print(b[r[i]-1]-b[l[i]-1])
### BUGGY CODE ###

from bisect import bisect_right


def main():
    n = int(input())
    aa = sorted(map(int, input().split()))
    bb = sorted(map(int, input().split()))
    cc = sorted(map(int, input().split()))

    res = 0
    for a in aa:
        i_b = bisect_right(bb, a)
        for b in bb[i_b:]:
            i_c = bisect_right(cc, b)
            res += n - i_c
    print(res)


if __name__ == '__main__':
    main()


### FIXED CODE ###

from bisect import bisect_left, bisect_right


def main():
    n = int(input())
    aa = sorted(map(int, input().split()))
    bb = sorted(map(int, input().split()))
    cc = sorted(map(int, input().split()))
    res = 0
    for b in bb:
        i_a = bisect_left(aa, b)
        i_c = bisect_right(cc, b)
        res += i_a * (n - i_c)
    print(res)


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

N = int(input())
x = N
y = N
ansx = N
ansy = N

for i in range(x+1):
    if i+1 > N:
        break
    for j in range(y+1):
        if j+1 > x+1:
            break
        if i*j == N:
            if ansx + ansy >i+j:
                ansx,ansy = i,j

print(ansx-1 + ansy -1)

### FIXED CODE ###

N = int(input())
x = N
y = N
ansx = N
ansy = N
ans = []
n = int(N**0.5)
if N**0.5 == int(N**0.5):
    print(int(N**0.5)*2-2)
else:
    for i in range(n+1):
        if N%(i+1) == 0:
            ans.append(i+1)
    ans.append(N)
    if(ans[1]==N):
        print(N-1)
    else:
        for i in range(len(ans)):
            if ansx+ansy>ans[i] + int(N//ans[i]):
                ansx = ans[i]
                ansy =int(N//ans[i])
        print(ansx + ansy -2)
### BUGGY CODE ###

import math


def solve(n):
    cap = math.ceil(n**0.5) - 2
    return sum([((x + y + z)**2 - x * y - x * z - y * z) == n for x in range(1, cap + 1) for y in range(1, cap + 1) for z in range(1, cap + 1)])


n = int(input())

for i in range(1, n + 1):
    print(solve(i))

### FIXED CODE ###

ans = [0] * 10**6

for x in range(1, 99):
    for y in range(1, 99):
        for z in range(1, 99):
            ans[(x + y + z)**2 - x * y - x * z - y * z] += 1

n = int(input())

for i in range(1, n + 1):
    print(ans[i])
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
ans=0
while all(j%2==0 for j in a):
    for k in range(len(a)):
        a[k]==a[k]//2
    ans+=1
print(ans)

### FIXED CODE ###

n=int(input())
A=list(map(int,input().split()))
ans=0
while all(a%2==0 for a in A):
    A=[a/2 for a in A]
    ans+=1
print(ans)
### BUGGY CODE ###

import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

def main():
	n,k=map(int,input().split())
	h=list(map(int,input().split()))
	dp=[10**10]*(n+200)
	dp[0]=0
	for i in range(n):
		for j in range(1,k+1):
			if i+j<=n-1:
				dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
	print(dp[n-1])
	
	
	
	
	
	
	
	
	
if __name__ == "__main__":
	main()


### FIXED CODE ###

import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

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

	
if __name__ == "__main__":
	main()