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

'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    n,m,k = map(int,ipt().split())
    fl = [[] for i in range(n)]
    bl = [set() for i in range(n)]
    for i in range(m):
        a,b = map(int,ipt().split())
        fl[a-1].append(b-1)
        fl[b-1].append(a-1)
    for i in range(k):
        c,d = map(int,ipt().split())
        bl[c-1].add(d-1)
        bl[d-1].add(c-1)

    ans = [-1]*n
    for i in range(n):
        if ans[i] != -1:
            continue
        else:
            q = [i]
            al = set([i])
            while q:
                qi = q.pop()
                for j in fl[qi]:
                    if not j in al:
                        al.add(j)
                        q.append(j)
            for j in al:
                ans[j] = len(al-bl[j])-len(fl[j])-1

    print(" ".join(map(str,ans)))


    return None

if __name__ == '__main__':
    main()


### FIXED CODE ###

'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    n,m,k = map(int,ipt().split())
    fl = [[] for i in range(n)]
    bl = [set() for i in range(n)]
    for i in range(m):
        a,b = map(int,ipt().split())
        fl[a-1].append(b-1)
        fl[b-1].append(a-1)
    for i in range(k):
        c,d = map(int,ipt().split())
        bl[c-1].add(d-1)
        bl[d-1].add(c-1)

    ans = [-1]*n
    al = [True]*n
    for i in range(n):
        if al[i]:
            q = [i]
            al[i] = False
            st = set([i])
            while q:
                qi = q.pop()
                for j in fl[qi]:
                    if al[j]:
                        st.add(j)
                        al[j] = False
                        q.append(j)
            lst = len(st)
            for j in st:
                tmp = lst-len(fl[j])-1
                for k in bl[j]:
                    if k in st:
                        tmp -= 1
                ans[j] = tmp

    print(" ".join(map(str,ans)))


    return None

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

def F(a,b):
  m = max(a,b)
  count=1
  while m//10!=0:
    count+=1
    m//=10
  return count

N = int(input())
tmp=F(1,N)
for i in range(1,N//2+1):
  if N%i==0:
    if tmp>F(i,N//i):
      tmp = F(i,N//i)
print(tmp)
  

### FIXED CODE ###

def F(a,b):
  m = max(a,b)
  count=1
  while m//10!=0:
    count+=1
    m//=10
  return count

N = int(input())
tmp=F(1,N)
for i in range(round(N**0.5),1,-1):
  if N%i==0:
    if tmp>F(i,N//i):
      tmp = F(i,N//i)
      break
print(tmp)
### BUGGY CODE ###

n = int(input())

a = list(map(str, input().split()))
b = []
for num in a:
  b.append(num)
  b.reverse()
  
print(" ".join(b))
  


### FIXED CODE ###

from collections import deque

n = int(input())

a = list(map(str, input().split()))
b = deque()
for i in range(n):
  if i % 2 == 0:
    #後ろに追加する場合
    b.append(a[i])
  else:
    b.appendleft(a[i])
    
    
    
if n % 2 != 0:
  b.reverse()
  
print(" ".join(b))
### BUGGY CODE ###

import collections

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

c = collections.Counter(a)
vs = []
for v in c.values():
  vs.append(v)
vs.sort()

i = 0
if len(vs) <= k:
  print(i)
  exit()
else:
  while len(vs) > k:
    i += vs[0]
    vs = vs[1:]
  else:
    print(i)
    exit()
    
    

### FIXED CODE ###

import collections

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

c = collections.Counter(a)
vs = []
for v in c.values():
  vs.append(v)
vs.sort(reverse=True)

i = 0
if len(vs) <= k:
  print(i)
  exit()
else:
  while len(vs) > k:
    i += vs[-1]
    vs.pop()
  else:
    print(i)
    exit()
### BUGGY CODE ###

A, B, C, D, E, F = map(int, input().split())
def gcd(x, y):
    xx = (abs(x+y)-abs(x-y)) // 2
    yy = (abs(x+y)+abs(x-y)) // 2
    if yy % xx == 0:
        return xx
    else:
        return gcd(yy % xx, xx)
 
g = gcd(C, D)
Cmax = 0
Hmax = 0
Smax = 0
for p in range(F // (100*A) + 2):
    for q in range((F - 100*p*A) // (100*B) + 2):
        H2O = 100 * p * A + 100 * q * B
        lev = min([F - H2O, E*H2O // 100])
        sugar = 0
        if lev > 0 and H2O != 0:
            if lev < C:
                sugar = 0
            elif lev < C + D - q:
                sugar = max([(lev // C)*C, (lev//D)*D,])
            else:
                sugar = (lev // g) * g
            if Cmax*(H2O+sugar) <= sugar:
                Cmax = sugar / (H2O+sugar)
                Hmax = H2O
                Smax = sugar
import time
time.sleep(A*0.8)
print("{0} {1}".format(Smax+Hmax, Smax))

### FIXED CODE ###

A, B, C, D, E, F = map(int, input().split())
def gcd(x, y):
    xx = (abs(x+y)-abs(x-y)) // 2
    yy = (abs(x+y)+abs(x-y)) // 2
    if yy % xx == 0:
        return xx
    else:
        return gcd(yy % xx, xx)

g = gcd(C, D)
Cmax = 0
Hmax = 0
Smax = 0
for p in range(F // (100*A) + 2):
    for q in range((F - 100*p*A) // (100*B) + 2):
        H2O = 100 * p * A + 100 * q * B
        lev = min([F - H2O, E*H2O // 100])
        sugar = 0
        if lev > 0 and H2O != 0:
            if lev < C:
                sugar = 0
            elif lev < C + D - q:
                sugar = max([(lev // C)*C, (lev//D)*D,])
            else:
                sugar = (lev // g) * g
            if Cmax*(H2O+sugar) <= sugar:
                Cmax = sugar / (H2O+sugar)
                Hmax = H2O
                Smax = sugar

print("{0} {1}".format(Smax+Hmax, Smax))
### BUGGY CODE ###

n = int(input())
s = input()
s += s[0]
d = {'S':'W', 'W':'S'}
flag = True
for sw in ['SS', 'WW', 'SW', 'WS']:
    l = sw
    for i in range(1, n+1):
        if l[i] == 'S' and s[i] == 'o' or l[i] == 'W' and s[i] == 'x':
            l += l[i-1]
        else:
            l += d[l[i-1]]
    if l[0] == l[-2] and l[1] == l[-1] :
        print(l[:-2])
        flag = False
        break
if flag:
    print(-1)



### FIXED CODE ###

n = int(input())
s = input()
s += s[0]
d = {'S':'W', 'W':'S'}
flag = True
for sw in ['SS', 'WW', 'SW', 'WS']:
    l = sw
    for i in range(1, n+1):
        if l[i] == 'S' and s[i] == 'o' or l[i] == 'W' and s[i] == 'x':
            l += l[i-1]
        else:
            l += d[l[i-1]]
    if l[0] == l[-2] and l[1] == l[-1] :
        print(l[:-2])
        flag = False
        break
if flag:
    print(-1)
### BUGGY CODE ###

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

BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key=lambda bc: bc[1], reverse=True)

cards = [0 for _ in range(2*N)]
for i in range(N):
    cards[i] = A[i]
idx = N
for b, c in BC:
    for _ in range(b):
        if idx < 2 * N:
            cards[idx] = c
            idx += 1
cards.sort(reverse=True)
ans = sum(cards[:N])
print(ans)


### FIXED CODE ###

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

BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key=lambda bc: bc[1], reverse=True)
update_bc = [0 for _ in range(N)]
cnt = 0
for b, c in BC:
    for i in range(b):
        if cnt < N:
            update_bc[cnt] = c
            cnt += 1
        else:
            break
    if cnt >= N:
        break
for i in range(N):
    if A[i] < update_bc[i]:
        A[i] = update_bc[i]
    else:
        break
print(sum(A))
### BUGGY CODE ###

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

dp = [[0.0] * (N+1) for _ in range(N+1)]

dp[0][0] = 1

for i in range(N):
    for j in range(i+1):
        dp[i+1][j] += dp[i][j] * (1-P[i])
        dp[i+1][j+1] += dp[i][j] * P[i]

ans = 0.0
for i in range(N//2+1, N+1):
    ans += dp[N][i]

print(ans)

### FIXED CODE ###

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

dp = [[0.0] * (N+1) for _ in range(N+1)]

dp[0][0] = 1

for i in range(N):
    for j in range(i+1):
        dp[i+1][j] += dp[i][j] * (1-P[i])
        dp[i+1][j+1] += dp[i][j] * P[i]

ans = 0.0
for i in range(N//2+1, N+1):
    ans += dp[N][i]

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

N = int(input())
X = [0] * N
Y = [0] * N
H = [0] * N
for i in range(N):
    X[i], Y[i], H[i] = map(int,input().split())

for Cx in range(101):
    for Cy in range(101):
        for T in range(min(H), min(H)+101):
            flag = 0
            for x,y,h in zip(X,Y,H):
                if h == max(T - abs(x-Cx) - abs(y-Cy),0):
                        flag += 1
                if flag == N:
                    print(Cx,Cy,T)
                    exit()


### FIXED CODE ###

from operator import itemgetter

N = int(input())
xyh = [list(map(int,input().split())) for _ in range(N)]
xyh.sort(key=itemgetter(2))
xyh.reverse()
for Cx in range(101):
    for Cy in range(101):
        H_MAX = -1
        H = -1
        for i in range(N):
            x = xyh[i][0]
            y = xyh[i][1]
            h = xyh[i][2]
            if h > 0 and i == 0:
                H = h + abs(x-Cx) + abs(y-Cy)
            elif h > 0:
                if H == h + abs(x-Cx) + abs(y-Cy):
                    pass
                else:
                    break
            elif h < 0 and i == 0:
                H_MAX = abs(x-Cx) + abs(y-Cy)
                if H_MAX == 0:
                    break
            else:
                if H_MAX == -1:
                    if H <= abs(x-Cx) + abs(y-Cy):
                        pass
                    else:
                        break
                else:
                    H_MAX = abs(x-Cx) + abs(y-Cy)
                    if H_MAX == 0:
                        break
            if i == N-1:
                print(Cx,Cy,max(H,H_MAX))
                exit()
### BUGGY CODE ###

a,b,c,d = map(int, input().split())
count = 0
for i in range(a,b+1):
  if i%c != 0 and i%d != 0:
    count+=1

print(count)

### FIXED CODE ###

import fractions
a,b,c,d = map(int, input().split())
cc = b//c - (a-1)//c
dd = b//d - (a-1)//d
gcd = int(c*d / fractions.gcd(c, d))
cd = b//gcd - (a-1)//gcd

print((b-a+1) + cd - cc -dd)
### BUGGY CODE ###

n,q = map(int,input().split())
s = input()
a_list = []
for i in range(len(s)-1):
    if (s[i] == 'A') and (s[i+1] == 'C'):
        a_list.append(i)
    
for i in range(q):
    cnt = 0
    l,r =  map(int,input().split())
    for j in a_list:
        if j+1 in range(l,r+1) and  j+2 in range(l,r+1):
            cnt += 1
    print(cnt)

### FIXED CODE ###

n,q = map(int,input().split())
s = input()
t_list = [0]*n
for i in range(len(s)-1):
    if (s[i] == 'A') and (s[i+1] == 'C'):
        t_list[i+1] = 1+t_list[i]
    else:
        t_list[i+1] = t_list[i]
        
for i in range(q):
    l,r =  map(int,input().split())
    print(t_list[r-1] - t_list[l-1])
### BUGGY CODE ###

# -*- coding:utf-8 -*-
if __name__ == "__main__":
    a,b,k = map(int, input().split())
    factors = [ tmp for tmp in range(a, b+1) ]
    ans = list(set(factors[0:k]+factors[-k:]))
    ans.sort()
    for factor in ans:
        print(factor)

### FIXED CODE ###

# -*- coding:utf-8 -*-
if __name__ == "__main__":
    a,b,k = map(int, input().split())
    ans = []
    if b-a+1 >= 2*k:
        for tmp in range(a, a+k):
            ans.append(tmp)
        for tmp in range(b-k+1,b+1):
            ans.append(tmp)
    else:
        for tmp in range(a, b+1):
            ans.append(tmp)
    ans.sort()
    for tmp in ans:
        print(tmp)
### BUGGY CODE ###

from collections import Counter
from itertools import product

n = int(input())

def prime_factrize(a):
    ret = []
    while a%2 == 0:
        ret.append(2)
        a//=2
    f = 3
    while f * f <= a:
        if a % f == 0:
            ret.append(f)
            a//=f
        else:
            f+=2
    if a != 1:
        ret.append(a)
    return ret

f = prime_factrize(n)

def func(a, b):
    a = len(str(a))
    b = len(str(b))
    return max(a, b)

ans = float('inf')
for bit in product([0, 1], repeat=(len(f))):
    a = 1
    b = 1
    for i, v in enumerate(bit):
        if v == 0: a*=f[i]
        else: b*=f[i]
    ans = min(ans, func(a, b))
print(ans)
            




### FIXED CODE ###

n = int(input())

def func(a, b):
    a = len(str(a))
    b = len(str(b))
    return max(a, b)

ans = float('inf')
for i in range(1, int(n**0.5)+1):
    if n%i==0: ans = min(ans, func(i, n//i))

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

from collections import Counter as counter

N = int(input())
char_counts = list(map(counter, [input() for i in range(N)]))
counts = [list(map(lambda x: x == c, char_counts[i+1:])) for i, c in enumerate(char_counts)]
match_count = sum(list(map(sum, counts)))
print(match_count)

### FIXED CODE ###

N = int(input())
dic = {}
count = 0
for _ in range(N):
  s = ''.join(sorted(input()))
  if s in dic.keys():
    count += dic[s]
    dic[s] += 1
  else:
    dic[s] = 1
print(count)
### BUGGY CODE ###

#りゅうせいの解答3
N = int(input())
task_list = [list(map(int, input().split())) for i in range(N)]

task_list.sort(key=lambda task_list: task_list[1])
time = 0
flag = True

while(len(task_list)):
    x = task_list.pop(0)
    time += x[0]
    if time > x[1]:
        flag = False
        break

if flag:
    print("Yes")

else:
    print("No")




#りゅうせいの解答2 正解
# N = int(input())
# task_list = [list(map(int, input().split())) for i in range(N)]
#
# task_list.sort(key=lambda task_list: task_list[1])
# time = 0
# flag = True
#
# for i in range(N):
#     x = task_list[i]
#     time += x[0]
#     if time > x[1]:
#         flag = False
#         break
#
# if flag:
#     print("Yes")
#
# else:
#     print("No")




#りゅうせいの解答1　実行時エラー、実行時間超過
# N = int(input())
# task_list = [list(map(int,input().split())) for i in range(N)]
#
# task_list.sort(key= lambda task_list: task_list[1])
# time = 0
#
# while(len(task_list)):
#     x = task_list.pop(0)
#     time += x[0]
#     if time > x[1]:
#         print("No")
#         exit(1)
#
#     else:
#         continue
#
# print("Yes")

### FIXED CODE ###

N = int(input())
task_list = [list(map(int, input().split())) for i in range(N)]

task_list.sort(key=lambda task_list: task_list[1])
time = 0
flag = True

for i in range(N):
    x = task_list[i]
    time += x[0]
    if time > x[1]:
        flag = False
        break

if flag:
    print("Yes")

else:
    print("No")




#りゅうせいの解答1　実行時エラー、実行時間超過
# N = int(input())
# task_list = [list(map(int,input().split())) for i in range(N)]
#
# task_list.sort(key= lambda task_list: task_list[1])
# time = 0
#
# while(len(task_list)):
#     x = task_list.pop(0)
#     time += x[0]
#     if time > x[1]:
#         print("No")
#         exit(1)
#
#     else:
#         continue
#
# print("Yes")
### BUGGY CODE ###

N, K = map(int, input().split())
H = [float('inf')] + list(map(int, input().split()))
DP = [float('inf')] * (N+1)
DP[0] = 0
DP[1] = 0

for i, h in enumerate(H[1:], 1):
    for k in range(1, K+1):
        if i + k > N:
            break
        DP[i + k] = min(DP[i+k], DP[i] + abs(H[i + k] - h))
print(DP[N])




### FIXED CODE ###

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

DP = [float('inf')] * N
DP[0] = 0
for i in range(N - 1):
    for j in range(i + 1, min(N, i + K + 1)):
        DP[j] = min(DP[j], DP[i] + abs(High[i] - High[j]))
print(DP[N - 1])
### BUGGY CODE ###

import numpy as np

a, b, q = map(int, input().split())
sh = []
te = []


def getNearestValue(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]


def checkshrine(x):
    x_near_sh = getNearestValue(sh, x)
    xsash = np.abs(x_near_sh - x)
    sh_near_te = getNearestValue(te, x_near_sh)
    return xsash + np.abs(sh_near_te - x_near_sh)


def checktemple(x):
    x_near_te = getNearestValue(te, x)
    xsate = np.abs(x_near_te - x)
    te_near_sh = getNearestValue(sh, x_near_te)
    return xsate + np.abs(te_near_sh - x_near_te)


sh = [int(input()) for _ in range(a)]
te = [int(input()) for _ in range(b)]

for i in range(q):
    x = int(input())
    checkshrinevalue = checkshrine(x)
    checktemplevalue = checktemple(x)
    if (checkshrinevalue > checktemplevalue):
        print(checktemplevalue)
    else:
        print(checkshrinevalue)

### FIXED CODE ###


import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
    for S in [s[b - 1], s[b]]:
        for T in [t[d - 1], t[d]]:
            d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
            res = min(res, d1, d2)
    print(res)
### BUGGY CODE ###

def count_books(books, limit):
    num_books = len(books)
    total_time = 0
    for i, time in enumerate(books):
        if total_time + time > limit:
            num_books = i
            break
        total_time += time
    return [num_books, total_time]

def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    max_num_A, total_time_A = count_books(A, K)
    read_book_nums = [0] * (max_num_A + 1)

    remain = K - total_time_A
    num_B, total_time_B = count_books(B, remain)
    read_book_nums[-1] = max_num_A + num_B

    num_A = max_num_A
    for n in range(max_num_A-1, -1, -1):
        num_A -= 1
        remain += A[n-1]
        num_B, total_time_B = count_books(B, remain)
        read_book_nums[n] = num_A + num_B

    print(max(read_book_nums))
    
main()

### FIXED CODE ###

def count_books(books, limit):
    num_books = len(books)
    total_time = 0
    for i, time in enumerate(books):
        if total_time + time > limit:
            num_books = i
            break
        total_time += time
    return [num_books, total_time]

def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    max_num_A, total_time_A = count_books(A, K)

    read_book_nums = [0] * (max_num_A + 1)

    remain = K - total_time_A
    num_B, total_time_B = count_books(B, remain)
    read_book_nums[-1] = max_num_A + num_B

    num_A = max_num_A
    m = 0
    total_time_B = 0
    for n in range(max_num_A-1, -1, -1):
        num_A -= 1
        remain += A[n]

        while m < M and total_time_B + B[m] <= remain:
            total_time_B += B[m]
            m += 1
        num_B = m

        read_book_nums[n] = num_A + num_B

    print(max(read_book_nums))
    
main()
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
from collections import deque


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    N, M = IL(int)
    AB = ILL(int, M)
    graph = [[False] * N for _ in range(N)]
    for a, b in AB:
        graph[a - 1][b - 1] = True
        graph[b - 1][a - 1] = True
    que = deque()
    que.append(0)
    visited = [False] * N
    visited[0] = True
    route = [0] * N
    while len(que):
        current = que.popleft()
        for next in range(N):
            if graph[current][next] and not visited[next]:
                visited[next] = True
                que.append(next)
                route[next] = current + 1

    print('Yes')
    for i in route[1:]:
        print(i)


solve()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
from collections import deque


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    N, M = IL(int)
    AB = ILL(int, M)
    graph = [[] for _ in range(N)]
    for a, b in AB:
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)
    que = deque()
    que.append(0)
    visited = [False] * N
    visited[0] = True
    route = [0] * N
    while len(que):
        current = que.popleft()
        for next in graph[current]:
            if not visited[next]:
                visited[next] = True
                que.append(next)
                route[next] = current + 1

    print('Yes')
    for i in route[1:]:
        print(i)


solve()
### BUGGY CODE ###

n, k = map(int, input().split())
num_list = [int(i) for i in input().split()]

ans_list = []

for i in range(n - 1):
    for j in range(i + 1, n):
        ans_list.append(num_list[i] * num_list[j])

ans_list.sort()

print(ans_list[k - 1])

### FIXED CODE ###

import numpy as np

n, k = map(int, input().split())
num_list = [int(i) for i in input().split()]
num_list = np.array(num_list)
num_list.sort()

neg = num_list[num_list < 0]
pos = num_list[num_list > 0]
zero =  num_list[num_list == 0]

def count(x):
    ans = 0
    if x >= 0:
        ans += len(zero) * n
    ans += np.searchsorted(num_list, x // pos, side="right").sum()
    ans += n * len(neg) - np.searchsorted(num_list, -(-x // neg), side="left").sum()
    ans -= np.count_nonzero(num_list * num_list <= x)
    return ans // 2

left = -(10 ** 18)
right = 10 ** 18

while left + 1 < right:
    mid = (left + right) // 2
    if count(mid) >= k:
        right = mid
    else:
        left = mid

print(right)
### BUGGY CODE ###

from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf')]*N
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])

### FIXED CODE ###

from sys import stdin
N,K=map(int,stdin.readline().split())
h=[int(i) for i in stdin.readline().split()][:N]
dp=[float('inf') for i in range(N)]
dp[0]=0
for i in range(N):
    for j in range(i+1,i+K+1):
        if j<N:
            dp[j]=min(dp[j],dp[i]+abs(h[j]-h[i]))
    #print(dp)
print(dp[-1])
### BUGGY CODE ###

N,K,Q = map(int,input().split())
A_list = []
for _ in range(Q):
  A = int(input())
  A_list.append(A)
for i in range(1,N+1):
  print("Yes" if K + A_list.count(i) - Q > 0 else "No")

### FIXED CODE ###

from collections import Counter
N,K,Q = map(int,input().split())
A_list = []
for _ in range(Q):
  A = int(input())
  A_list.append(A)
A_Counter = Counter(A_list)
for i in range(1,N+1):
  print("Yes" if K + A_Counter[i] - Q > 0 else "No")
### BUGGY CODE ###

MOD = 10**9+7
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]
dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if i in a:
        dp[i] = 0
    else:
        if i == 1:
            dp[1] = 1
        else:
            dp[i] = (dp[i-1] + dp[i-2]) % MOD

print(dp[n])


### FIXED CODE ###

MOD = 10**9+7
n, m = map(int, input().split())
A = [int(input()) for _ in range(m)]

safe = [True] * (n+1)
for a in A:
    safe[a] = False

dp = [0] * (n+1)
dp[0] = 1
for i in range(1, n+1):
    if safe[i]:
        dp[i] = (dp[i-1] + dp[i-2]) % MOD

print(dp[n])
### BUGGY CODE ###

from collections import deque
S=deque(input())
q=int(input())
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    S.reverse()
  elif c[1]=='1':
    S.appendleft(c[2])
  elif c[1]=='2':
    S.append(c[2])
ans=''.join(S)
print(ans)

### FIXED CODE ###

from collections import deque
S=deque(input())
q=int(input())
rev=False
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    rev=not rev
  elif c[1]=='1' and rev==False:
    S.appendleft(c[2])
  elif c[1]=='2' and rev==True:
    S.appendleft(c[2])
  else:
    S.append(c[2])
if rev:
  S.reverse()
ans=''.join(S)
print(ans)