Harryxun/stf_run huggingface dataset:
# import itertools
# import pprint
from collections import deque

gem,ope = map(int,input().split())
vnowt = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r > ope:
      break

    x   = ope-l-r
    now = []
    deq = vnowt.copy() 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)

### FIXED CODE ###

# import itertools
# import pprint
import copy
from collections import deque

gem,ope = map(int,input().split())
vlist = deque(map(int,input().split()))

ans = 0

for l in range(ope+1):
  for r in range(ope-l+1):
    if l+r>ope or l+r>gem:
      break

    x   = ope-l-r
    now = []
    deq = copy.copy(vlist) 

    for i in range(l):
      now.append(deq.popleft())
    for j in range(r):
      now.append(deq.pop())
    for k in range(x):
      if now==[]:
        break
      if min(now)<0:
        now.remove(min(now))
    
    if ans<sum(now):
      ans = sum(now)

print(ans)
N,M=map(int,input().split())
unionfindTree = [i for i in range(N)]
size = [1 for _ in range(N)]
inconvenience = N*(N-1)//2
answers = [inconvenience] #M bridges destroyed
#answers[i] should be inconvenience after M-i bridges destroyed.

def root(x):
    p = unionfindTree[x]
    if x==p:
        return(p)
    else:
        r = root(p)
        unionfindTree[x] = r
        return(r)

def getsize(x):
    r = root(x)
    return(size[r])

def union(x,y):
    rx = root(x)
    ry = root(y)
    convenience = 0
    if rx != ry:
        unionfindTree[ry] = rx
        # sx = size[rx]
        # sy = size[ry]
        # convenience = sx * sy
        # size[rx] = sx+sy
    return(convenience)

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

for (a,b) in revBridges:
    c = union(a,b)
    inconvenience -= c
    answers.append(inconvenience)
for i in range(M):
    print(answers[M-1-i])

### FIXED CODE ###

N,M = map(int,input().split())
edges = []
for _ in range(M):
    a,b = map(int,input().split())
    edges.append((a-1,b-1))
edges.reverse()
parents = list(range(N))
size = [1 for _ in range(N)]
rank = [0 for _ in range(N)]

def root(x):
    px = parents[x]
    if px == x:
        return(x)
    else:
        r = root(px)
        parents[x] = r
        return(r)

def same(x,y):
    return(root(x) == root(y))

def union(x,y):
    rx = root(x)
    ry = root(y)
    if rx == ry:
        return(0)
    if rank[ry] > rank[rx]:
        parents[rx] = ry
        size[ry] += size[rx]
    else:
        parents[ry] = rx
        size[rx] += size[ry]
        if rank[rx] == rank[ry]:
            rank[rx] += 1
    return(1)

inconvinience = N*(N-1)//2
answers=[inconvinience]

for edge in edges:
    x,y = edge
    rx = root(x)
    ry = root(y)
    sx = size[rx]
    sy = size[ry]
    res = union(rx,ry)
    if res == 1:
        inconvinience -= sx*sy
    answers.append(inconvinience)

answers.reverse()

for answer in answers[1:]:
    print(answer)
import sys
from statistics import *
from collections import *
from operator import itemgetter
from fractions import gcd
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
def lcm(x, y):
    return (x * y) // gcd(x, y)


x, y = na()
if x * 2 > y:
    print(1)
else:
    ct = 2
    num1 = x
    num2 = x*2
while True:
    g = lcm(num1, num2)
    if g * 2 <= y:
        ct += 1
        num1 = num2
        num2 = g * 2
    else:
        break
print(ct)

### FIXED CODE ###

import sys
from statistics import *
from collections import *
from operator import itemgetter
from fractions import gcd
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
def lcm(x, y):
    return (x * y) // gcd(x, y)


x, y = na()
if x * 2 > y:
    print(1)
else:
    ct = 2
    num1 = x
    num2 = x*2
    while True:
        g = lcm(num1, num2)
        if g * 2 <= y:
            ct += 1
            num1 = num2
            num2 = g * 2
        else:
            break
    print(ct)
n=int(input())
l=[input() for i in range(n)]

if l.most_common()[0][1] != 1:
  ans="No"
else:
  l_head = [i[0] for i in l]
  l_tail = [i[-1] for i in l]
  if l_head == l_tail:
    ans="Yes"
  else:
    ans="No"
    
print(ans)
  

### FIXED CODE ###

from collections import Counter

n=int(input())
l=[input() for i in range(n)]

if Counter(l).most_common()[0][1] != 1:
  ans="No"
else:
  l_head = [l[i+1][0] for i in range(n-1)]
  l_tail = [l[i][-1] for i in range(n-1)]
  if l_head == l_tail:
    ans="Yes"
  else:
    ans="No"
    
print(ans)
c = input('')
if c == a:
    print('vowel')
elif c == e:
    print('vowel')
elif c == i:
    print('vowel')
elif c == o:
    print('vowel')
elif c == u:
    print('vowel')
else:
    print('consonant')

### FIXED CODE ###

c = input()
d =['a', 'e', 'i', 'o', 'u']

if c in d:
    print('vowel')
else:
    print('consonant')
K,X=input().split
K=int(K)
X=int(X)

if 500*K>=X:
  print("yes")
else:
  print("no")

### FIXED CODE ###

K,X=map(int,input().split())

if 500*K >= X:
  print('Yes')
else:
  print('No')
A,B=list(map(int,input().split()))
ans=[]
for i in range(1,1010):
    if int(i*0.08)==A and int(i*0.1)==B:
        ans.append(i)
print(min(ans))

### FIXED CODE ###

A,B=list(map(int,input().split()))
ans=[]
for i in range(10):
    if A==(8*B*10+8*i)//100:
        ans.append(i)
if ans==[]:
    print(-1)
else:
    print(B*10+min(ans))
# Author: cr4zjh0bp
# Created: Tue Mar 17 19:50:06 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n, q = na()
ab = nan(n - 1)
px = nan(q)

tree = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = ab[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)

cnt = [0 for _ in range(n)]
for i in range(q):
    p, x = px[i]
    p -= 1
    cnt[p] += x

def dfs(i, p, s):
    x = s + cnt[i]
    cnt[i] = x
    for j in tree[i]:
        if j == p:
            continue
        dfs(j, i, x)

dfs(0, -1, 0)
print(*cnt)

### FIXED CODE ###

# Author: cr4zjh0bp
# Created: Tue Mar 17 19:50:06 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import deque

sys.setrecursionlimit(10 ** 6)

n, q = na()
ab = nan(n - 1)
px = nan(q)

tree = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = ab[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)

cnt = [0 for _ in range(n)]
for i in range(q):
    p, x = px[i]
    p -= 1
    cnt[p] += x

def dfs(i, p, s):
    x = s + cnt[i]
    cnt[i] = x
    for j in tree[i]:
        if j == p:
            continue
        dfs(j, i, x)

def bfs():
    q = deque()
    q.append((0, -1, 0))
    while len(q):
        p, b, s = q.popleft()
        x = s + cnt[p]
        cnt[p] = x
        for i in tree[p]:
            if i == b:
                continue
            q.append((i, p, x))

bfs()
print(*cnt)
a, b = gets.split(" ")
a = a.to_i
b = b.to_i
if a < b
    puts "a < b"
elsif a > b
    puts "a > b"
else
    puts "a == b"
end

### FIXED CODE ###

a,b=map(int, raw_input().split())

if a > b:
    print "a > b"
elif a < b:
    print "a < b"
else:
    print "a == b"
rail = []
while True:
  n = int(raw_input())
  if n != 0:
    rail.append(n)
  else:
    print rail.pop()

### FIXED CODE ###

try:
  rail = []
  while True:
    n = int(raw_input())
    if n != 0:
      rail.append(n)
    else:
      print rail.pop()
except:
  pass
import fractions

n,X=map(int,input().split())
x=list(map(int,input().split()))
x.append(X)
x2=sorted(x)
a=x2[1]-x2[0]
if len(x2)==2:
    print(a)
else:
    for i in range(1,n-1):
        b=x2[i+1]-x2[i]
        ans=fractions.gcd(a,b)
        a=b
    print(ans)

### FIXED CODE ###

import fractions

n,X=map(int,input().split())
x=list(map(int,input().split()))
ans=abs(x[0]-X)
for i in range(1,n):
    b=abs(x[i]-X)
    ans=fractions.gcd(ans,b)
print(ans)
def s(n):
  s = 0
  for i in range(len(n)):
    s += int(s[i])
    
  return s

n = input()

if int(n) % s(n) == 0:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

n = input()
N = int(n)
I = []

for i in range(len(n)):
    I = I + [int(n[i])]

S = sum(I)

if N % S == 0:
  print('Yes')
else:
  print('No')
a, b, c, d = map(int, input().split())
n = int(input())
 
b = min(a + a, b)
c = min(b + b, c)
d = min(c + c, d)
 
q, r = divmod(N, 2)
x = q * d + r * c
print(x)

### FIXED CODE ###

a, b, c, d = map(int, input().split())
n = int(input())
 
b = min(a + a, b)
c = min(b + b, c)
d = min(c + c, d)
 
q, r = divmod(n, 2)
x = q * d + r * c
print(x)
a, b, k = map(int, input().split())
ans = []
for i in range(1, min(a, b)):
    if a % i == 0 and b % i == 0:
        ans.append(i)
print(ans[-k])

### FIXED CODE ###

a, b, k = map(int, input().split())
ans = []
for i in range(1, min(a, b)+1):
    if a % i == 0 and b % i == 0:
        ans.append(i)
print(ans[-k])
N = int(input())
X = input()
times = [0]*200000

for i in range(1,200000):
  bi = format(i, 'b')
  arr = list(map(int, bi))
  sbi = sum(arr)
  if i%sbi == 0:
    times[i] = 1
  else:
    times[i] = 1+times[i%sbi]
    
arr = list(map(int, X))
sbX = sum(arr)
mo = [1,-1]
for i in range(N):
  X10 = int(X,2)^(2**(N-i-1))
  waru = sbX+mo[int(X[i])]
  print(times[X10%waru]+1)
  

### FIXED CODE ###

N = int(input())
X = input()
times = {}
times[0] = 0

for i in range(1,200000):
  bi = format(i, 'b')
  arr = list(map(int, bi))
  sbi = sum(arr)
  if i%sbi == 0:
    times[i] = 1
  else:
    times[i] = 1+times[i%sbi]
    
arr = list(map(int, X))
sbX = sum(arr)
X10 = int(X,2)
mo = [1,-1]
if sbX-1 == 0:
  mo2 = [X10%(sbX+1),X10]
else:
  mo2 = [X10%(sbX+1),X10%(sbX-1)]

for i in range(N):
  waru = sbX+mo[int(X[i])]
  if waru == 0:
    print(0)
  else:
    print(times[(mo[int(X[i])]*pow(2,(N-i-1),waru)+mo2[int(X[i])])%waru]+1)
import copy
A = list(map(int, input().split()))

a = max(A)
AA = copy.deepcopy(A)
AA.remove(a)
b = max(AA)

for hoge in A:
  if hoge == a:
    print(b)
  else:
    print(a)

### FIXED CODE ###

import copy
N = int(input())
A = []
for _ in range(N):
  A.append(int(input()))
a = max(A)
AA = copy.deepcopy(A)
AA.remove(a)
b = max(AA)

for hoge in A:
  if hoge == a:
    print(b)
  else:
    print(a)
import numpy as np

n, W = list(map(int,input().split()))
w_v = [list(map(int,input().split())) for i in range(n)]
w = [w_v[i][0] for i in range(n)]
v = [w_v[i][1] for i in range(n)]
inf = 10**9+1
dp = [[inf] * (sum(v)+1) for i in range(n+1)]
#dp = np.zeros((n+1,sum(v)+1),dtype=np.int32)
dp[0][0] = 0

for i in range(n):
  for j in range(sum(v)+1):
    if j - v[i] >= 0:
      dp[i+1][j] = min(dp[i+1][j], dp[i][j-v[i]] + w[i])

    dp[i+1][j] = min(dp[i+1][j], dp[i][j])


for i in range(sum(v)+1):
  if dp[n][i] > W:
    pass
  else:
    ans = i
print(ans)


### FIXED CODE ###

n, W = list(map(int,input().split()))
w_v = [list(map(int,input().split())) for i in range(n)]
w = [w_v[i][0] for i in range(n)]
v = [w_v[i][1] for i in range(n)]
inf = 10**9+1
dp = [[inf] * (sum(v)+1) for i in range(n+1)]
dp[0][0] = 0

for i in range(n):
  for j in range(sum(v)+1):
    if j - v[i] >= 0:
      dp[i+1][j] = min(dp[i+1][j], dp[i][j-v[i]] + w[i])

    dp[i+1][j] = min(dp[i+1][j], dp[i][j])


for i in range(sum(v)+1):
  if dp[n][i] > W:
    pass
  else:
    ans = i
print(ans)
a, b, c = input()

print("Yes" if set(a,b,c)==("a", "b", "c") else "No")


### FIXED CODE ###


S = input()

print("Yes" if set(S)=={"a", "b", "c"} else "No")
from math import *
import sys 
cin = sys.stdin()

cnt = [[0] * 19 for i in range(19)]

ans = 0
for _ in range(int(input())):
    t = float(input())
    b = int(t * 1000000000.0)
    x, y = 0, 0
    while b % 2 == 0 and x < 18:
        x += 1
        b //= 2
    while b % 5 == 0 and y < 18:
        y += 1
        b //= 5
    #print(x, y)
    for i in range(19):
        for j in range(19):
            if i + x >= 18 and j + y >= 18:
                ans += cnt[i][j]
    cnt[x][y] += 1
print(ans)


### FIXED CODE ###

from math import *
#import sys 
#cin = sys.stdin()

cnt = [[0] * 20 for i in range(20)]

ans = 0
for _ in range(int(input())):
    t = input().strip().split('.')
    if (len(t) > 1):
        b = int(t[0])*1000000000 + int(t[1]) * 10 ** (9 - len(t[1]))
    else:
        b = int(t[0]) * 1000000000
    x, y = 0, 0
    while (b % 2 == 0) and (x <= 18):
        x += 1
        b //= 2
    while (b % 5 == 0) and (y <= 18):
        y += 1
        b //= 5
    #print(x, y)
    for i in range(20):
        for j in range(20):
            if (i + x >= 18) and (j + y >= 18):
                ans += cnt[i][j]
    cnt[x][y] += 1
print(ans)
code = input('security code: ')

num = split('')

if num[0] == num[1]:
    print('Bad')
elif num[1] == num[2]:
    print('Bad')
elif num[2] == num[3]:
    print('Bad')
else:
    print('Good')

### FIXED CODE ###

num = input()
#num = str(code)

if num[0] == num[1] or num[1] == num[2] or num[2] == num[3]:
    print('Bad')
else:
    print('Good')
limit = int(input())
string = input()

if limit < len(string):
    ans = string[:limit] + '...'

print(ans)

### FIXED CODE ###

limit = int(input())
string = input()

if limit < len(string):
    string = string[:limit] + '...'

print(string)
a,b,c=map(int,input().split())
if a+b==c or a+c==b or a==b+c
print("yes")
else
print("no")

### FIXED CODE ###

a,b,c=map(int,input().split())
print("Yes"if a+b==c or a+c==b or a==b+c else "No")
n=int(input())
list=[0]*n
list[0]=2
list[1]=1
for i in range(2,n):
  list[i]=list[i-1]+list[i-2]
print(list[n-1])

### FIXED CODE ###

n=int(input())
list=[0]*(n+1)
list[0]=2
list[1]=1
for i in range(2,n+1):
  list[i]=list[i-1]+list[i-2]
print(list[n])
n=int(input())
s=input().split()
a=sorted([int(s[i])-i-1 for i in range(n)])

if n==0:
    print(0)
else:
    #それぞれの間で考える
    mi=[]
    for i in range(n-1):
        c,d=a[i],a[i+1]
        m=0
        for k in range(n):
            if k<=i:
                m-=a[k]
            else:
                m+=a[k]
        l=(i+1)-(n-i-1)
        if l>0:
            m+=(l*a[i+1])
        else:
            m+=(l*a[i])
        mi.append(m)
    print(min(mi))


### FIXED CODE ###

#あー統計のやつか、絶対値の和の最小値は平均の値
n=int(input())
s=input().split()
a=sorted([int(s[i])-i-1 for i in range(n)])
#ここまでの発想は流石にわかる
c=0
for i in range(n):
    if i<=n//2:
        c+=(a[n//2]-a[i])
    else:
        c+=(a[i]-a[n//2])
print(c)



'''
if n==1:
    print(0)
else:
    mi=[]
    h=sum(a)
    k=h
    for i in range(n-1):
        c,d=a[i],a[i+1]
        k-=2*a[i]
        m=k
        l=(i+1)-(n-i-1)
        if l>0:
            m+=(l*a[i+1])
        else:
            m+=(l*a[i])
        mi.append(m)
    mi.append(h-a[0]*n)
    mi.append(a[-1]*n-h)
    print(min(mi))
'''
N, Q = map(int, input().split())
S = input()
S_lr = []
for i in range(Q):
    l, r = map(int, input().split())
    s = S[l-1:r]
    S_lr.append(s)

for s in S_lr:
    count = 0
    A_flag = False
    for i in range(len(s)):
        if s[i] == 'C' and i-1 > -1 and s[i-1] == 'A':
            count += 1

    print(count)



### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
	t[i+1] = t[i] + (1 if S[i:i+2] == 'AC' else 0)

for i in range(Q):
	l, r = map(int, input().split())
	print(t[r-1] - t[l-1])
while True:
    a,b,c,d,e,f = map(int,raw_input().split())
    x = 0.; y = 0.
    delta = abs(a*e-b*d)
    x = float((e*c-b*f)/delta)
    y = float((a*f-d*c)/delta)

    print "%.3f %.3f" % (x,y)
    

### FIXED CODE ###

while True:
    try:
        a,b,c,d,e,f = map(float,raw_input().split())
        delta =a*e-b*d
        x = (e*c-b*f)/delta
        y = (a*f-d*c)/delta
        if abs(x)<1e-3:
            x=0
        if abs(y)<1e-3:
            y=0
        #print str('%.3f' % x) + " " + str('%.3f' % y)
        print "%.3f %.3f" % (x,y)
    except EOFError:
        break
n, m = map(int, input().split())
count = 0
if n % 2 == 0:
    for i in range(int(n/2)):
        print(i+1, n-i-1)
        count += 1
        if count == m:
            break;
else:
    for i in range(int((n-1)/2)):
        print(i+1, n-i-2)
        count += 1
        if count == m:
            break;

### FIXED CODE ###

a, b, n = map(int, input().split())
if n < b:
    print(a*n//b)
else:
    print((a*(b-1))//b)
from math import gcd
#import graction.gcd
from functools import reduce

def lcm(n1, n2):
    return n1 * n2 // gcd(n1, n2)

N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] //= 2

lcmA = reduce(lcm, A)
print((M // lcmA + 1) // 2)



### FIXED CODE ###

import fractions
import functools

def lcm(n1, n2):
    return n1 * n2 // fractions.gcd(n1, n2)

N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] //= 2

lcmA = functools.reduce(lcm, A)
gcdA = functools.reduce(fractions.gcd, A)
if (lcmA // gcdA) % 2 == 0:
    print(0)
else:
    print((M // lcmA + 1) // 2)
import sys
from io import StringIO
import unittest
# from fractions import gcd
from math import gcd
from functools import reduce

def gcd_list(numbers):
    return reduce(gcd, numbers)

def resolve():
    n = int(input())
    a = list(map(int, input().split()))

    gcd_num = gcd_list(a)

    print(gcd_num)

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
2 10 8 40"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """4
5 13 8 1000000000"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """3
1000000000 1000000000 1000000000"""
        output = """1000000000"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

### FIXED CODE ###

import sys
from io import StringIO
import unittest
from fractions import gcd
# from math import gcd
from functools import reduce

def gcd_list(numbers):
    return reduce(gcd, numbers)

def resolve():
    n = int(input())
    a = list(map(int, input().split()))

    gcd_num = gcd_list(a)

    print(gcd_num)

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
2 10 8 40"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """4
5 13 8 1000000000"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """3
1000000000 1000000000 1000000000"""
        output = """1000000000"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()
N=int(input())
M=int(input())
N=N-M
if 0<N:
   print(N)
elif 0=N:
   print(0)
else:
   print(-1)

### FIXED CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
B=0
for i in A:
    B=B+i       
N=N-B
if 0<=N:
    print(N)
else:
    print(-1)
n,k=map(int,input().split())
hlist=list(map(int,input().split()))

mins=[0]

if n == 2:
	print(abs(hlist[1]-hlist[0]))

for i in range(1,n):	
	smallest=1000000000001
	tmp=hlist[i]
	for j in range(min(k,i)):
		smallest=min(smallest,mins[i-j-1]+abs(tmp-hlist[i-j-1]))
	mins.append(min(tmplist))

print(mins[n-1])


### FIXED CODE ###

n,k=map(int,input().split())
hlist=list(map(int,input().split()))
 
mins=[0]
 
for i in range(1,n):
	if i == 1:
		mins.append(abs(hlist[1]-hlist[0]))
		continue
	
	tmplist=[]
	tmp=hlist[i]
	for j in range(min(k,i)):
		tmplist.append(mins[i-j-1]+abs(tmp-hlist[i-j-1]))
	mins.append(min(tmplist))
 
print(mins[n-1])
import sys
input = sys.stdin.readline
input_line = list(map(int,input().split()))
input_hp = list(map(int,input().split()))
try:
    [(input_hp.sort(reverse=True) ,input_hp.pop(_)) for _ in range(input_line[1])]
    print(sum(input_hp))
except IndexError:
    print(‘0’)

### FIXED CODE ###

input_line = list(map(int,input().split()))
input_hp = list(map(int,input().split()))

if input_line[0] <= input_line[1]:
    print('0')
else:
    input_hp.sort()
    result_input_hp = len(input_hp) - input_line[1]
    sum_total = sum(input_hp[:result_input_hp])
    print(sum_total)
def calculate(arr,M):
    sum = 0
    for i in range(M):
        index = 0
        value = 0
        for j in range(len(arr)):
            lastDay = i + arr[j][0]
            if lastDay <= M:
                if arr[j][1] > value:
                    index = j
                    value = arr[j][1]

                if (arr[j][1] == value) and (arr[index][0] < arr[j][0]):
                        index = j
                        value = arr[j][i]

        if len(arr) > 0:
            sum += value
            arr.pop(index)

        if len(arr) == 0:
            break

    return sum


N,M = input().split()
N = int(N)
M = int(M)


arr = []
for i in range(N):
    day,mount = input().split()
    day = int(day)
    mount = int(mount)
    arr.append([day,mount])
result = calculate(arr,M)
print(result)

### FIXED CODE ###

from heapq import heappush, heappop
n, m = map(int, input().split())
jobs = {}
for _ in range(n):
  a, b = map(int, input().split())
  if a > m:
    continue
  if a in jobs:
    jobs[a].append(b)
  else:
    jobs[a] = [b]
result = 0
candidates = []
for a in range(1, m + 1):
  if a in jobs:
    for b in jobs[a]:
      heappush(candidates, -b)
  else:
    if len(candidates) == 0:
      continue
  result += -heappop(candidates)
print(result)
given = input()
a = given // 2
count = 0
for i in range(1, a + 1):
    last = given[-i]
    first = given[i - 1]
    if last != first:
        count += 1
print(count)

### FIXED CODE ###

s = input()
l = list(s)
i = 0
j = len(l)-1
cnt = 0
while(i < j):
    if l[i] != l[j]:
        cnt = cnt+1

    i = i+1
    j = j-1

print(cnt)
# -*- coding: utf-8 -*-
n,t = map(int,input().split())
ans_no = 0
ans_cost = t

for i in range(n):
    a,b = map(int,input().split())
    if b<ans_cost:
        ans_no = a

if ans_no = 0:
    print("TLE")
else print(ans_no)

### FIXED CODE ###

# -*- coding: utf-8 -*-
n,t = map(int,input().split())
ans_cost = 10000
ans_time = t

for i in range(n):
    a,b = map(int,input().split())
    if b<=ans_time:
        if a<ans_cost:
            ans_cost = a


if ans_cost == 10000:
    print("TLE")
else:
     print(ans_cost)