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

# AtCoder Grand Contest 033
# https://atcoder.jp/contests/agc033
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

import datetime

def main_ok():
    H, W = i2nn()
    HW = H * W
    A = ii2ss(H)
    A = [[0 if s == '#' else HW for s in line] for line in A]
    for i in range(H):
        for j in range(1, W):
            if A[i][j-1]+1 < A[i][j]:
                A[i][j] = A[i][j-1]+1
        for j in range(W-2, -1, -1):
            if A[i][j+1]+1 < A[i][j]:
                A[i][j] = A[i][j+1]+1
    for j in range(W):
        for i in range(1, H):
            if A[i-1][j]+1 < A[i][j]:
                A[i][j] = A[i-1][j]+1
        for i in range(H-2, -1, -1):
            if A[i+1][j]+1 < A[i][j]:
                A[i][j] = A[i+1][j]+1
    ans = 0
    for i in range(H):
        for j in range(W):
            if A[i][j] > ans:
                ans = A[i][j]
    print(ans)

def main():
    H, W = i2nn()
    A = ii2ss(H)
    A = [['#'] * (W+2)] + [['#'] + list(s) + ['#'] for s in A] + [['#'] * (W+2)]
    q = []
    rec = 0
    for i in range(1, H+1):
        for j in range(1, W+1):
            if A[i][j] == '#':
                q.append((i, j))
    while len(q) > 0:
        q2 = []
        for i, j in q:
            t = [
                (i - 1, j),
                (i + 1, j),
                (i, j - 1),
                (i, j + 1),
            ]
            for i, j in t:
                if A[i][j] != '#':
                    A[i][j] = '#'
                    q2.append((i, j))
        rec += 1
        q = q2
    print(rec - 1)

main()


### FIXED CODE ###

# AtCoder Grand Contest 033
# https://atcoder.jp/contests/agc033
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

import datetime

def main():
    H, W = i2nn()
    HW = H * W
    A = ii2ss(H)
    A = [[0 if s == '#' else HW for s in line] for line in A]
    for i in range(H):
        for j in range(1, W):
            if A[i][j-1]+1 < A[i][j]:
                A[i][j] = A[i][j-1]+1
        for j in range(W-2, -1, -1):
            if A[i][j+1]+1 < A[i][j]:
                A[i][j] = A[i][j+1]+1
    for j in range(W):
        for i in range(1, H):
            if A[i-1][j]+1 < A[i][j]:
                A[i][j] = A[i-1][j]+1
        for i in range(H-2, -1, -1):
            if A[i+1][j]+1 < A[i][j]:
                A[i][j] = A[i+1][j]+1
    ans = 0
    for i in range(H):
        for j in range(W):
            if A[i][j] > ans:
                ans = A[i][j]
    print(ans)

main()
### BUGGY CODE ###

import numpy as np
n = int(input())
a = np.array(list(map(int,input().split())),np.int32)

m = np.zeros(10**6 + 1,np.int32)

for i in a:
    if m[i] == 1:
        m[i] = 2
        continue
    m[::i] += 1
ans = 0
for i in a:
    if m[i] == 1:
        ans += 1
print(ans)

### FIXED CODE ###

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

ma = max(a)
m = [0]*(ma + 1)

for i in a:
    if m[i] == 1:
        m[i] = 2
        continue
    for j in range(i,ma+1,i):
        m[j]+=1
ans = 0
for i in a:
    if m[i] == 1:
        ans += 1
print(ans)
### BUGGY CODE ###

from collections import deque
s = deque(list(input()))
q = int(input())
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        s.reverse()
    else:
        if int(line[1]) == 1:
            s.appendleft(line[2])
        else:
            s.append(line[2])
print("".join(s))

### FIXED CODE ###

from collections import deque
s = deque(list(input()))
q = int(input())
# -1がnotReverse, 1がReverser
isR = -1
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        isR *= -1
    else:
        f = int(line[1])
        if isR + f == 0 or isR + f == 3:
            s.appendleft(line[2])
        else:
            s.append(line[2])
    if i == q-1 and isR == 1:
        s.reverse()
print("".join(s))
### BUGGY CODE ###

class Queue():
    def __init__(self):
        self.data = []
        self.idx = 0
        self.size = 0

    def get(self):
        self.idx += 1
        self.size -= 1
        return self.data[self.idx-1]

    def put(self, item):
        self.size += 1
        self.data.append(item)

    def qsize(self):
        return self.size

        # input
H, W = map(int, input().split())
A = [list(input()) for i in range(H)]

# bfs
bfs = [[-1 for j in range(W)] for i in range(H)]
que = Queue()
for i in range(H):
    for j in range(W):
        if(A[i][j] == '#'):
            que.put((i, j))
            bfs[i][j] = 0
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
while(que.qsize()):
    p = que.get()
    for i in range(4):
        if(0 <= p[0]+dx[i] < H and 0 <= p[1]+dy[i] < W):
            if(bfs[p[0]+dx[i]][p[1]+dy[i]] == -1):
                que.put((p[0]+dx[i], p[1]+dy[i]))
                bfs[p[0]+dx[i]][p[1]+dy[i]] = bfs[p[0]][p[1]] + 1

# print ans
print(max([max(l) for l in bfs]))


### FIXED CODE ###

from collections import deque

# input
H, W = map(int, input().split())
A = [list(input()) for i in range(H)]

# bfs
bfs = [[-1 for j in range(W)] for i in range(H)]
que = deque()
for i in range(H):
    for j in range(W):
        if(A[i][j] == '#'):
            que.append((i, j))
            bfs[i][j] = 0
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
while(len(que)):
    p = que.popleft()
    for i in range(4):
        if(0 <= p[0]+dx[i] < H and 0 <= p[1]+dy[i] < W):
            if(bfs[p[0]+dx[i]][p[1]+dy[i]] == -1):
                que.append((p[0]+dx[i], p[1]+dy[i]))
                bfs[p[0]+dx[i]][p[1]+dy[i]] = bfs[p[0]][p[1]] + 1

# print ans
print(max([max(l) for l in bfs]))
### BUGGY CODE ###

import copy

def calc_fee(l):
    s = 0
    l = [0] + l + [0]
    for i in range(len(l)-1):
        s += abs(l[i+1] - l[i])
    return s

n = int(input())
A = [int(n) for n in input().split()]

for i in range(n):
    A_ = copy.copy(A)
    del A_[i]
    
    price = calc_fee(A_)
    print(price)

### FIXED CODE ###

def calc_fee(l):
    s = 0
    for i in range(len(l)-1):
        s += abs(l[i+1] - l[i])
    return s

n = int(input())
A = [0] + [int(n) for n in input().split()] + [0]


total_fee = calc_fee(A)

for i in range(1, n+1):
    price = total_fee - (abs(A[i] - A[i-1]) + abs(A[i+1] - A[i]))  + abs(A[i+1] - A[i-1])
    print(price)
### BUGGY CODE ###

import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 7)


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


@lru_cache(None)
def dp(i, j, cur):
    if cur > K:
        return 0
    elif i >= N:
        if j < M and cur + B[j] <= K:
            return dp(i, j + 1, cur + B[j]) + 1
        else:
            return 0
    elif j >= M:
        if i < N and cur + A[i] <= K:
            return dp(i + 1, j, cur + A[i]) + 1
        else:
            return 0
    else:
        res = 0
        if cur + A[i] <= K:
            res = max(1 + dp(i + 1, j, cur + A[i]), res)
        if cur + B[j] <= K:
            res = max(1 + dp(i, j + 1, cur + B[j]), res)
        return res


print(dp(0, 0, 0))

### FIXED CODE ###

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

prefix_A = [0] * (N + 1)
for i, a in enumerate(A, 1):
    prefix_A[i] = prefix_A[i - 1] + a
prefix_B = [0] * (M + 1)
for j, b in enumerate(B, 1):
    prefix_B[j] = prefix_B[j - 1] + b


res = 0
for i in range(N + 1):
    cur = prefix_A[i]
    if cur == K:
        res = max(i, res)
    elif cur < K:
        left = 0
        right = M + 1
        while right - left > 1:
            mid = (left + right) // 2
            if cur + prefix_B[mid] <= K:
                left = mid
            else:
                right = mid
        res = max(res, i + left)


print(res)
### BUGGY CODE ###

url = "https://atcoder.jp/contests/abc090/tasks/arc091_a"
import copy
def main():
    tate, yoko = list(map(int, input().split()))
    count = 0
    if tate == 1 and yoko == 1:
        print("1")
        exit()
    elif tate == 1:
        print(yoko - 2)
        exit()
    elif yoko == 1:
        print(tate - 2)
        exit()
    for t in range(1, tate + 1):
        if t == 1 or t == tate:
            count += 0
        else:
            count += yoko - 2
    print(count)




if __name__ == '__main__':
    main()




### FIXED CODE ###

url = "https://atcoder.jp/contests/abc090/tasks/arc091_a"
import copy
def main():
    tate, yoko = list(map(int, input().split()))
    count = 0
    if tate == 1 and yoko == 1:
        print("1")
        exit()
    elif tate == 1:
        print(yoko - 2)
        exit()
    elif yoko == 1:
        print(tate - 2)
        exit()
    count += (yoko - 2) * (tate - 2)
    print(count)




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

N,K=map(int,input().split())
P=list(map(int,input().split()))
class SegTree:
    def __init__(self,size):
        self.N=size
        self.v=[-1]*(2*self.N-1)
        self.w=[10**6]*(2*self.N-1)
    def update(self,i,x):
        i+=self.N-1
        self.v[i]=x
        self.w[i]=x
        while i>0:
            i=(i-1)//2
            self.v[i]=max(self.v[i*2+1],self.v[i*2+2])
            self.w[i]=min(self.w[i*2+1],self.w[i*2+2])
    def max(self,a,b,i=0,l=0,r=1e9):
        if r==1e9:
            r=self.N
        if r<=a or b<=l:
            return -1
        if a<=l and r<=b:
            return self.v[i]
        return max(self.max(a,b,i*2+1,l,(l+r)//2),self.max(a,b,i*2+2,(l+r)//2,r))
    def min(self,a,b,i=0,l=0,r=1e9):
        if r==1e9:
            r=self.N
        if r<=a or b<=l:
            return 10**6
        if a<=l and r<=b:
            return self.w[i]
        return min(self.min(a,b,i*2+1,l,(l+r)//2),self.min(a,b,i*2+2,(l+r)//2,r))
s=SegTree(2**18)
x,n=0,0
for i,p in enumerate(P):
    s.update(i,p)
    if P[i-1]<P[i]:
        x+=1
        if K-1<=i and x==K-1:
            n+=1
            x-=1
    else:
        x=0
r=1
n=max(0,n-1)
for i in range(N-K):
    m,M=s.min(i+1,K+i),s.max(i+1,K+i)
    if not (P[i]<m and M<P[K+i]):
        r+=1
print(max(1,r-n))

### FIXED CODE ###

N,K=map(int,input().split())
P=list(map(int,input().split()))
from collections import deque
smin,smax=deque(),deque()
x,r,r2=0,0,0
for i,p in enumerate(P):
    while len(smin)>0 and P[smin[-1]]>p:
        smin.pop()
    smin.append(i)
    if smin[0]<=i-K:
        smin.popleft()
    while len(smax)>0 and P[smax[-1]]<p:
        smax.pop()
    smax.append(i)
    if smax[0]<=i-K:
        smax.popleft()
    if i>0 and P[i-1]<P[i]:
        x=min(x+1,K-1)
    else:
        x=0
    if i==K-1 or (i>=K and not (P[i-K]<P[smin[0]] and smax[0]==i)):
        if x!=K-1:
            r+=1
        else:
            r2=1
print(r+1 if r2 else r)
### BUGGY CODE ###

import itertools
ans = 10000000000000000000000000000000000
n, k = map(int, input(). split())
p = list(map(int, input(). split()))
P = list(itertools.permutations(p, k))
s = len(P)
for i in range(s):
  P[i] = list(P[i])
  q = sum(P[i])
  if ans >= q:
    ans = q
print(ans)

### FIXED CODE ###

n, k = map(int, input(). split())
p = list(map(int, input(). split()))
p = sorted(p)
ans = 0
for i in range(k):
  ans += p[i]
print(ans)
### BUGGY CODE ###

from collections import deque
import bisect
from copy import copy

n = int(input())
a = list(map(int, input().split()))
to=[list() for _ in range(n)]

for i in range(n-1):
    u,v = map(int, input().split())
    u-=1
    v-=1
    to[u].append(v)
    to[v].append(u)

d= deque()
INF=10010001001
dpinit = [INF]*n
dpinit[0]=a[0]
d.append((0,-1,copy(dpinit)))
ansls=[0]*n
ansls[0]=1
while d:
    u,p,dp = d.popleft()
    for v in to[u]:
        if v==p:
            continue
        dp2=copy(dp)
        idx=bisect.bisect_left(dp2,a[v])
        dp2[idx]=a[v]
        ansls[v]=bisect.bisect_left(dp2,INF)
        d.append((v,u,copy(dp2)))

print(*ansls,sep='\n')
        










### FIXED CODE ###

from collections import deque
import bisect
import sys
sys.setrecursionlimit(500000)

n = int(input())
a = list(map(int, input().split()))
to=[list() for _ in range(n)]

for i in range(n-1):
    u,v = map(int, input().split())
    u-=1
    v-=1
    to[u].append(v)
    to[v].append(u)

INF=1001001001
dp = [INF]*n
dp[0]=a[0]
ansls=[0]*n
ansls[0]=1
history=deque()
def dfs(u,p,rank):
    for v in to[u]:
        if v==p:
            continue
        idx=bisect.bisect_left(dp,a[v])
        prv=dp[idx]
        dp[idx]=a[v]       
        history.append((idx,prv))
        ansls[v]=bisect.bisect_left(dp,INF)
        dfs(v,u,rank+1)
        while len(history)>rank:
            idx,prv =history.pop()
            dp[idx] = prv

dfs(0,-1,0)

print(*ansls,sep='\n')
### BUGGY CODE ###

n,s = map(int,input().split())
a = list(map(int,input().split()))
mod = 998244353
dp = [[0 for i in range(s+1)] for j in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
for i in range(1,n+1):
    x = a[i-1]
    for j in range(s+1):
        dp[i][j] = dp[i-1][j] * 2
        if dp[i][j] >= mod:
            dp[i][j] %= mod
    for j in range(x,s+1):
        if j >= x:
            dp[i][j] += dp[i-1][j-x]
            if dp[i][j] >= mod:
                dp[i][j] %= mod

print(dp[-1][-1])

### FIXED CODE ###

n,s = map(int,input().split())
a = list(map(int,input().split()))
mod = 998244353
dp = [[0 for i in range(s+1)] for j in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
for i in range(1,n+1):
    x = a[i-1]
    for j in range(s+1):
        dp[i][j] = dp[i-1][j] * 2
        if dp[i][j] >= mod:
            dp[i][j] %= mod
    for j in range(x,s+1):
        if j >= x:
            dp[i][j] += dp[i-1][j-x]
            if dp[i][j] >= mod:
                dp[i][j] %= mod

print(dp[-1][-1])
### BUGGY CODE ###

n = int(input())

import sys
a = []
for line in sys.stdin:
    a.append(int(line.rstrip("\n")))

check = []    
for i in a:
    if check.count(i) == 0 :
         check.append(i)
    else :
         check.remove(i)

print(len(check))

### FIXED CODE ###

n = int(input())

answer = {}
for i in range(n):
  ai = int(input())
  if answer.get(ai):
    answer.pop(ai)
  else:
    answer[ai] = 1
    
print(len(answer))
### BUGGY CODE ###

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

points = X[:]
rem = N
points.sort()

if M<=N:
    ans = 0
else:
    import queue
    q = queue.Queue()
    q.put([[] for _ in range(N)])

    import sys
    INT_MAX = sys.maxsize
    ans = INT_MAX

    import itertools
    for v in itertools.combinations(list(range(1, M)),N-1):
        v = list(v)
        v.insert(0,0)
        v.append(M)
        #print(v)
        cost_sum = 0
        for i in range(1, len(v)):
            sub_p = points[v[i-1]:v[i]]
            cost = sub_p[-1] - sub_p[0]
            #print(sub_p, cost)
            cost_sum += cost
            if cost_sum >= ans:
                break
        ans = min(ans, cost_sum)

print(ans)





    




### FIXED CODE ###

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

if M <= N:
    ans = 0
else:
    points = X[:]
    points.sort()

    diff_p = []
    for i in range(1, len(points)):
        diff_p.append(points[i] - points[i-1])

    diff_p.sort()
    #print(diff_p)

    ans = sum(diff_p[:M-N])
print(ans)
### BUGGY CODE ###

n,m = map(int,input().split())
a_list = list(map(int,input().split()))
for i in range(m):
    b,c= map(int,input().split())
    b_list = [c]*b
    a_list.extend(b_list)
a_list.sort(reverse=True)
print(sum(a_list[0:n]))


### FIXED CODE ###



n,m = map(int,input().split())
a_list_temp = list(map(int,input().split()))
a_list = []
for i in a_list_temp:
    a_list.append([1,i])

for i in range(m):
    b,c = map(int,input().split())
    a_list.append([b,c])
a_list.sort(key=lambda x: x[1], reverse=True)
#print(a_list)
count = 0
ans_sum = 0
for i in a_list:
    if count + i[0] <= n:
        ans_sum += i[0] * i[1]
        count += i[0]
    else:
        ans_sum += (n - count) * i[1]
        break
print(ans_sum)
### BUGGY CODE ###

S = input()
Q = int(input())
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        S = ''.join(reversed(list(S)))
    else:
        if A[1] == '1':
            S = A[2] + S
        else:
            S = S + A[2]
print(S)

### FIXED CODE ###

S = input()
Q = int(input())
x = 0
p = ''
q = ''
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        x += 1
    elif x%2 ==0:
        if A[1] == '1':
            p = A[2] + p
        else:
            q = q + A[2]
    else:
        if A[1] == '1':
            q = q + A[2] 
        else:
            p = A[2] + p
S = p + S + q
if x%2 == 1:
    S = ''.join(reversed(list(S)))
print(S)
### BUGGY CODE ###

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

def has_duplicates(seq):
    return len(seq) != len(set(seq))

visited = [0]
flag = False
loopStart = 0
loopCycle = 0

for i in range(n):
    visited.append(a[visited[-1]]-1)
    if has_duplicates(visited):
        flag = True
        loopStart = visited.index(visited[-1])
        loopCycle = len(visited) - 1 - loopStart
        break
    if i == k - 1:
        print(visited[i+1]+1)
        break

if flag:
    print(visited[loopStart + (k - loopStart) % loopCycle]+1)

### FIXED CODE ###

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

visited = [0]
visitedSet = {0}
flag = False
loopStart = 0
loopCycle = 0
currentTown = 0

for i in range(n):
    currentTown = a[visited[-1]]-1
    if currentTown in visitedSet:
        flag = True
        loopStart = visited.index(currentTown)
        loopCycle = i + 1 - loopStart
        break
    if i == k - 1:
        print(currentTown+1)
        break

    visited.append(currentTown)
    visitedSet.add(currentTown)

if flag:
    print(visited[loopStart + (k - loopStart) % loopCycle]+1)
### BUGGY CODE ###

raw_input()
S = map(int, raw_input().split())
raw_input()
T = sorted(map(int, raw_input().split()))
count = 0
for i in T:
    if(i in S):
        count += 1
print count

### FIXED CODE ###

raw_input()
S = set(map(int, raw_input().split()))
raw_input()
T = set(sorted(map(int, raw_input().split())))
print len(S) - len(S - T)
### BUGGY CODE ###

import itertools
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
score = float('inf')
for x in itertools.combinations(h, k):
    score = min(score, max(x) - min(x))
print(score)

### FIXED CODE ###

import numpy as np
n, k = map(int, input().split())
h = np.array(sorted([int(input()) for _ in range(n)]))
h_roll = np.roll(h, -k + 1)
x = h_roll - h
print(int(x[:-k + 1].min()))
### BUGGY CODE ###

n=int(input())
Alist=list(map(int,input().split()))

Flag = "YES"
Counter = 1

for i in Alist:
  
  for j in range(n - Counter):
    if i == Alist[Counter + j]:
      Flag = "NO"
  Counter = Counter + 1

print(Flag)


### FIXED CODE ###

n=int(input())
Asetttt=list(map(int,input().split()))

Asett=set(Asetttt)

if len(Asett) == n:
	Flag ="YES"
else:
	Flag="NO"
print(Flag)
### BUGGY CODE ###

import sys
from math import sqrt

input = sys.stdin.buffer.readline
n, q = map(int, input().split())
C = list(map(int, input().split()))
LR = []
bucket_size = int(sqrt(n))
for i in range(q):
    l, r = map(int, input().split())
    LR.append((l // bucket_size, r, l, i))
LR.sort()
# print(LR)

num = 0
count = [0] * (n + 1)


def add_num(idx):
    global num
    c = C[idx]
    if not count[c]:
        num += 1
    count[c] += 1


def del_num(idx):
    global num
    c = C[idx]
    count[c] -= 1
    if not count[c]:
        num -= 1


# 開区間[nl,nr)で考える
nl, nr = 0, 0
ans = [0] * (q)
preb = -1
for i in range(q):
    b, r, l, idx = LR[i]
    l -= 1
    if preb != b:
        nl, nr = 0, 0
        nl += l
        nr += l
        num = 0
        count = [0] * (n + 1)
    # print(nl, nr, l, r)
    # 区間を広げる時
    while nl > l:
        nl -= 1
        add_num(nl)
    while nr < r:
        add_num(nr)
        nr += 1
    # 区間を狭める時
    while nl < l:
        del_num(nl)
        nl += 1
    while nr > r:
        nr -= 1
        del_num(nr)
    # print("a", num, "idx", idx, "LR", (l, r))
    # print("c", count)
    ans[idx] = num
    preb = b


print(*ans, sep="\n")



### FIXED CODE ###

import sys

input = sys.stdin.buffer.readline
n, q = map(int, input().split())
C = list(map(int, input().split()))
LR = []
for i in range(q):
    l, r = map(int, input().split())
    LR.append((l, r, i))
LR.sort(key=lambda x: x[1])


BIT = [0] * (n + 1)


def update(idx, val):
    while idx <= n:
        BIT[idx] += val
        idx += idx & (-idx)


def query(idx):
    res = 0
    while idx > 0:
        res += BIT[idx]
        idx -= idx & (-idx)
    return res


pos = [-1] * (n + 1)


def func(idx):
    temp = C[idx]
    if pos[temp] == -1:
        pos[temp] = idx + 1
        update(idx + 1, 1)
    else:
        pre = pos[temp]
        pos[temp] = idx + 1
        update(pre, -1)
        update(idx + 1, 1)


ans = [0] * (q)
idx = 0
for i in range(q):
    l, r, ans_num = LR[i]
    # l,rは0-indexに戻す
    l -= 1
    r -= 1
    while idx < r + 1:  # rになるまで増やす
        if idx < n:  # n超えないように注意
            func(idx)
            idx += 1
        else:
            break
    res = 0
    res += query(r + 1)
    if l > 0:
        res -= query(l)
    ans[ans_num] = res

print(*ans, sep="\n")
### BUGGY CODE ###

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


if __name__ == "__main__":
    resolve()


### FIXED CODE ###

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


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

K = int(input())
num = 0
if K%2 == 0:
    print(-1)
else:
    for i in range(1, 100000000):
        num = num*10 + 7
        num %= K
        if num == 0:
            print(i)
            break

### FIXED CODE ###

K = int(input())
num = 0
if K%2 == 0:
    print(-1)
else:
    for i in range(1, 1000000):
        num = num*10 + 7
        num %= K
        if num == 0:
            print(i)
            break
    else:
        print(-1)
### BUGGY CODE ###

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

ans = 1
for i in range(n):
    ans *= a[i]

print(ans if ans <= 1000000000000000000 else -1)


### FIXED CODE ###

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

ans = 1

if 0 in a:
    print(0)
    exit()
for i in range(n):
    ans *= a[i]
    if ans > 1000000000000000000:
        print(-1)
        exit()

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

N,K = map(int,input().split())
List = list(map(int,input().split()))
result = [(1+i)/2 for i in List]
Answer = 0
for i in range(N-K+1):
  counter = sum(result[i:i+K])
  if counter > Answer:
    Answer = counter

print(Answer)

### FIXED CODE ###

N,K = map(int,input().split())
List = list(map(int,input().split()))
result = [(1+i)/2 for i in List]
Answer = 0
if N == 1:
  print(result[0])
  exit()
elif N == K:
  print(sum(result))
  exit()
base = [sum(result[0:K])]
for i in range(N-K):
  counter = base[i]-result[i]+result[i+K]
  if counter > Answer:
    Answer = counter
  base.append(counter)
print(Answer)