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

N = int(input())
T = [int(input()) for _ in range(N)]

ssum = {}
def func(a):
	global ssum
	s = {}
	for i in range(2, a + 1):
		while not a % i:
			if i not in s:
				s[i] = 1
			else:
				s[i] += 1
			a //= i
		if 1 == a:
			break
	for t in s:
		if t not in ssum:
			ssum[t] = s[t]
		else:
			ssum[t] = max(ssum[t], s[t])

for t in T:
	func(t)

r = 1
for t in ssum:
	r *= t ** ssum[t]

print(r)


### FIXED CODE ###

N = int(input())
T = [int(input()) for _ in range(N)]

def gcd(a, b):
	if not a % b:
		return b
	else:
		return gcd(b, a % b)

l = T[0]
for n in range(1, N):
	l = l * T[n] // gcd(l, T[n])

print(l)
### BUGGY CODE ###

R,G,B,N=map(int,input().split())
cnt=0
for r in range(0,N//R+1):
    for g in range(0,(N-r*R)//G+1):
        bunshi=N-r*R-g*G
        q,m=divmod(bunshi,B)
        # if q<0:continue
        if m==0:cnt+=1            
print(cnt)

### FIXED CODE ###

def main():
  r,g,b,n=map(int,input().split())
  cnt=0
  for i in range(n//r+1):
    for j in range((n-i*r)//g+1):
      tmp=r*i+g*j
      q,mod=divmod(n-tmp,b)
      if (q>=0 and mod==0):
        cnt+=1
  print(cnt)
  
main()
### BUGGY CODE ###

n=int(input())
s=input()
ans=n
for i in range(n):
    ans=min(s[:i].count("W")+s[i+1:].count("E"),ans)
print(ans)

### FIXED CODE ###

n=int(input())
S=input()
a=S.count("E")
m=a
for s in S:
    if s=="E":
        a-=1
    m=min(m,a)
    if s=="W":
        a+=1
print(m)
### BUGGY CODE ###

from collections import Counter
N = int(input())
A_list = list(map(int, input().split()))
Q = int(input())
BC_list = [list(map(int, input().split())) for i in range(Q)]

count_dict = dict(Counter(A_list))

for b, c in BC_list:
    if b in count_dict:
        if c in count_dict:
            count_dict[c] += count_dict[b]
        else:
            count_dict[c] = count_dict[b]
        count_dict.pop(b)
    _sum = 0
    for i, a in count_dict.items():
        _sum += i * a
    print(_sum)

### FIXED CODE ###

from collections import Counter
N = int(input())
A_list = list(map(int, input().split()))
Q = int(input())
BC_list = [list(map(int, input().split())) for i in range(Q)]

count_dict = dict(Counter(A_list))
_sum = 0
for i, a in count_dict.items():
    _sum += i * a

for b, c in BC_list:
    if b in count_dict:
        count_b = count_dict[b]
        if c in count_dict:
            count_dict[c] += count_dict[b]
        else:
            count_dict[c] = count_dict[b]
        count_dict.pop(b)
        _sum -= b * count_b
        _sum += c * count_b
    print(_sum)
### BUGGY CODE ###

import collections
N,K=map(int,input().split())
li=list(map(int,input().split()))
li_count=collections.Counter(li)
change=0
S=max(len(li_count)-K,0)
for s in range(S):
    change+=li_count.most_common()[-1-s][1]
print(change)


### FIXED CODE ###

import collections
N,K=map(int,input().split())
li=list(map(int,input().split()))
li_count=collections.Counter(li)
li_count_most=li_count.most_common()[::-1]
change=0
S=max(len(li_count)-K,0)
for s in range(S):
    change+=li_count_most[s][1]
print(change)
### BUGGY CODE ###

from collections import deque

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

p = list(map(int,input().split()))

a = deque(p[:k])
ans = sum(a)

for i in range(n-k):
    a.popleft()
    a.append(p[i+k])
    A = sum(a)
    if A > ans:
        ans = A
        continue

ans = (ans+k)/2

print(ans)

### FIXED CODE ###

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

p = list(map(int,input().split()))

a = 0

for i in range(k):
    a += p[i]

ans = a

for i in range(n-k):
    a -= p[i]
    a += p[i+k]
    if a > ans:
        ans = a

ans = (ans+k)/2

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

n=int(input(""))
k=1
li=input("").split(" ")
if "0" in li:
    print("0")
else:
    for i in li:
        k*=int(i)
    if k>1000000000000000000:
        print("-1")
    else:
        print(k)


### FIXED CODE ###

n=int(input(""))
k=1
li=list(map(int,input("").split(" ")))
if 0 in li:
    print("0")
else:
    for i in li:
        k*=int(i)
        if k>1000000000000000000:
            print("-1")
            break
    else:
        print(k)
### BUGGY CODE ###

# coding: utf-8
# Your code here!

X = input()
num = pow(10, 100000)
ctr = 0

for i in range(num):
    if 'ST' in X:
        X = X.replace('ST', '')
    else:
        break
print(len(X))

### FIXED CODE ###

# coding: utf-8
# Your code here!

X = list(input())
num = len(X)
stack = []

for i in X:
    if i == "S":
        stack.append(i)
    else:
        if stack == [] or stack[-1] == "T":
            stack.append(i)
        else:
            stack.pop()
print(len(stack))
### BUGGY CODE ###

import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

A = np.array(A, dtype=np.int)
B = np.array(B, dtype=np.int)

dp = np.zeros([H, W, 80**2+1], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())


### FIXED CODE ###

import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

max_bias = 80
bias = max_bias * (H + W - 1) + 1

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

A = np.array(A, dtype=np.int)
B = np.array(B, dtype=np.int)
D = np.abs(A - B)

dp = np.zeros([H, W, bias * 2 + 1], dtype=bool)
dp[0][0][bias - D[0][0]] = True
dp[0][0][bias + D[0][0]] = True

for i in range(H):
    for j in range(W):
        diff = D[i][j]
        
        if i > 0:
            dp[i, j, diff:] |= dp[i - 1, j, :bias * 2 + 1 - diff]
            dp[i, j, : bias * 2 + 1 - diff] |= dp[i - 1, j, diff:]

        if j > 0:
            dp[i, j, diff:] |= dp[i, j - 1, :bias * 2 + 1 - diff]
            dp[i, j, : bias * 2 + 1 - diff] |= dp[i, j - 1, diff:]

idx = dp[H - 1][W - 1][:].nonzero()[0]
print(np.abs(idx - bias).min())
### BUGGY CODE ###

import os,io
import collections
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n=int(input())
m=[]
for i in range(n):
  a=collections.deque(list(map(int,input().split())))
  m.append(a)
ans=0
count=0
failflag=0
while count<(n*(n-1)):
  ans+=1
  tempcount=count
  r=set()
  for i in range(n):
    if m[i]:
      if m[m[i][0]-1]:
        if m[m[i][0]-1][0]==i+1:
          count+=1
          r.add(i)
          r.add(m[i][0]-1)
  for k in r:
    m[k].popleft()
  if tempcount==count:
    failflag=1
    break
if failflag==1:
  print(-1)
else:
  print(ans)
    

### FIXED CODE ###

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n=int(input())
m=[]
for i in range(n):
  a=list(map(int,input().split()))
  a.reverse()
  m.append(a)
ans=0
count=0
failflag=0
previous=set()
for i in range(n):
  previous.add(i)
while count<(n*(n-1)):
  ans+=1
  tempcount=count
  r=set()
  for i in previous:
    if m[i]:
      t=m[i][-1]
      if m[t-1]:
        if m[t-1][-1]==i+1:
          count+=1
          r.add(i)
          r.add(t-1)
  previous=set()
  for k in r:
    m[k].pop()
    previous.add(k)
    if m[k]:
      previous.add(m[k][-1]-1)
  if tempcount==count:
    failflag=1
    break
if failflag==1:
  print(-1)
else:
  print(ans)
### BUGGY CODE ###

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

dp=[[] for _ in range(N+1)]
dp[0]=(0,[])
for i in range(1,N+1):
  if S[i]==0:
    cand=[]
    for j in range(1,M+1):
      if i-j<0:
        break
      elif S[i-j]==1:
        continue
        
      if dp[i-j]:
        cand.append((dp[i-j][0]+1,dp[i-j][1]+[j]))
    cand.sort()
    #print(i,cand)
    if cand:
      dp[i]=cand[0]
    
#print(dp)
if dp[-1]:
  print(*dp[-1][1])
else:
  print(-1)

### FIXED CODE ###

import sys
N,M=map(int,input().split())
slist=list(map(int,input()))

seq=0
for s in slist:
  if s==0:
    seq=0
  else:
    seq+=1
    if seq==M:
      print(-1)
      sys.exit(0)

#####segfunc#####
def segfunc(x,y):
  return min(x,y)

#####ide_ele#####
ide_ele=float("inf")

class SegTree:
  """
  init(init_val, ide_ele): 配列init_valで初期化 O(N)
  update(k, x): k番目の値をxに更新 O(logN)
  query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
  """
  def __init__(self, init_val, segfunc, ide_ele):
    """
    init_val: 配列の初期値
    segfunc: 区間にしたい操作
    ide_ele: 単位元
    n: 要素数
    num: n以上の最小の2のべき乗
    tree: セグメント木(1-index)
    """
    n = len(init_val)
    self.segfunc = segfunc
    self.ide_ele = ide_ele
    self.num = 1 << (n - 1).bit_length()
    self.tree = [ide_ele] * 2 * self.num
    # 配列の値を葉にセット
    for i in range(n):
      self.tree[self.num + i] = init_val[i]
    # 構築していく
    for i in range(self.num - 1, 0, -1):
      self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

  def update(self, k, x):
    """
    k番目の値をxに更新
    k: index(0-index)
    x: update value
    """
    k += self.num
    self.tree[k] = x
    while k > 1:
      self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
      k >>= 1

  def query(self, l, r):
    """
    [l, r)のsegfuncしたものを得る
    l: index(0-index)
    r: index(0-index)
    """
    res = self.ide_ele

    l += self.num
    r += self.num
    while l < r:
      if l & 1:
        res = self.segfunc(res, self.tree[l])
        l += 1
      if r & 1:
        res = self.segfunc(res, self.tree[r - 1])
      l >>= 1
      r >>= 1
    return res
  
seg=SegTree([float("inf")]*(N+1), segfunc, ide_ele)
seg.update(N,0)

for i in reversed(range(N)):
  if slist[i]==0:
    left=i+1
    right=min(i+M,N)
    seg.update(i,seg.query(left,right+1)+1)

dist_list=[]
for i in range(N+1):
  dist_list.append(seg.query(i,i+1))
#print(dist_list)

answer_list=[]
i=0
while i<N:
  for j in range(1,M+1):
    if dist_list[i]>dist_list[i+j]:
      answer_list.append(j)
      i+=j
      break
    
print(*answer_list)
### BUGGY CODE ###

n=int(input())
a=[int(i)for i in input().split()]
res=[]
for i in range(1,n+1):
    res.append(str(a.index(i)+1))
print(" ".join(res))

### FIXED CODE ###

n=int(input())
a=[int(i)for i in input().split()]
res=[0 for i in range(n)]
for i in range(n):
    res[a[i]-1]=i+1
print(" ".join(map(str,res)))
### BUGGY CODE ###

N=int(input())
count=0
S=input()
for i in range(N):
    for j in range(i,N):
        for k in range(j,N):
            if j-i!=k-j:
                if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                    count+=1
print(count)

### FIXED CODE ###

N=int(input())
S=input()
R=S.count("R")
G=S.count("G")
B=S.count("B")
ans=R*G*B
for i in range(N):
    for j in range(i,N):
        k=j+j-i
        if k>=N:
            continue
        if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
            ans-=1
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
N = int(input())
a = []
count = 0
for i in range(N):
  temp = "".join(sorted(input()))
  count += a.count(temp)
  a.append(temp)

print(count)

### FIXED CODE ###

# -*- coding: utf-8 -*-
N = int(input())
dic = {}
count = 0
for i in range(N):
  temp = "".join(sorted(input()))
  if temp in dic:
    count += dic[temp]
    dic[temp] += 1
  else:
    dic[temp] = 1

print(count)
### BUGGY CODE ###

from heapq import heappush,heappop

N,M,s,t=map(int,input().split())
table=[[] for i in range(N)]
rtable=[[] for i in range(N)]
for i in range(M):
    u,v,a,b=map(int,input().split())
    table[u-1].append([a,v-1])
    table[v-1].append([a,u-1])
    rtable[u-1].append([b,v-1])
    rtable[v-1].append([b,u-1])
mod=pow(10,15)
def dijk(s,table):
    dij=[]
    heappush(dij,[0,s])
    val=[mod]*N
    val[s]=0
    while len(dij)>0:
        q,x=heappop(dij)
        if val[x]<q:
            continue
        for d,y in table[x]:
            if val[y]>val[x]+d:
                val[y]=val[x]+d
                heappush(dij,[val[y],y])
    return val
val=dijk(s-1,table)
rval=dijk(t-1,rtable)
score=[0]*N
for i in range(N):
    score[i]=mod-(val[i]+rval[i])
amax=[mod]*N
amax[-1]=score[-1]
for i in range(N-2,-1,-1):
    amax[i]=max(amax[i+1],score[i])
for i in range(N):
    print(amax[i])

### FIXED CODE ###

from heapq import heappush,heappop
from collections import deque
N,M,s,t=map(int,input().split())
table=[[] for i in range(N)]
rtable=[[] for i in range(N)]
for i in range(M):
    u,v,a,b=map(int,input().split())
    table[u-1].append([a,v-1])
    table[v-1].append([a,u-1])
    rtable[u-1].append([b,v-1])
    rtable[v-1].append([b,u-1])
dij=[]
heappush(dij,[0,s-1])
val=[10**16]*N
val[s-1]=0
while dij:
    q,x=heappop(dij)
    if val[x]<q:
        continue
    for d,y in table[x]:
        if val[y]>val[x]+d:
            val[y]=val[x]+d
            heappush(dij,[val[y],y])
rdij=[]
heappush(rdij,[0,t-1])
rval=[10**16]*N
rval[t-1]=0
while rdij:
    q,x=heappop(rdij)
    if rval[x]<q:
        continue
    for d,y in rtable[x]:
        if rval[y]>rval[x]+d:
            rval[y]=rval[x]+d
            heappush(rdij,[rval[y],y])
ans=deque()
t=val[N-1]+rval[N-1]
mod=pow(10,15)
ans.append(mod-t)
for i in range(N-2,-1,-1):
    t=min(t,val[i]+rval[i])
    ans.appendleft(mod-t)
print('\n'.join(map(str,ans)))
### BUGGY CODE ###

from collections import deque
def dist(A,B):
    return abs(A[0]-B[0])+abs(A[1]-B[1])

N=int(input())
P=[0]*N

for i in range(N):
    x,y=map(int,input().split())
    P[i]=(x,y)

P=list(set(P))
N=len(P)

S=[0]*N
D=[]
for k in range(N):
    if S[k]==0:
        S[k]=1
        Old_Old_=0
        Old_I=-1
        I=k
        Flag=False
        while True:
            M=-1
            Old_Old_I=Old_I
            Old_I=I
            for i in range(N):
                if M<dist(P[I],P[i]):
                    J=i
                    M=dist(P[I],P[i])

            if J==Old_Old_I and Flag:
                break
            I=J
            S[I]=1
            Flag=True

    D.append(dist(P[Old_I],P[J]))

print(max(D))

### FIXED CODE ###

N=int(input())
Z=[0]*N
W=[0]*N

for i in range(N):
    x,y=map(int,input().split())
    Z[i]=x+y
    W[i]=x-y

alpha=max(Z)-min(Z)
beta=max(W)-min(W)
print(max(alpha,beta))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a = tuple(map(int,input().split()))
    b = tuple(map(int,input().split()))

    dg = 10**7

    dp = [[10**5]*n for i in range(2**n)]

    new = set([])
    for i in range(n):
        s = (1<<i)
        dp[s][i] = i
        new.add(s*dg+i)
    

    for k in range(n-1):
        tank = set([])
        if len(new) == 0:
            break
        for popelt in new:
            s,idx = popelt//dg,popelt%dg
            if (k-idx)%2 == 0:
                fr = a[idx]
            else:
                fr = b[idx]
            cnt = 0
            for j in range(n):
                if (s>>j)&1 == 0:
                    if (j-k)%2 == 1:
                        val = a[j]
                    else:
                        val = b[j]
                    if val >= fr and dp[s+(1<<j)][j] > dp[s][idx]+j-cnt:
                        dp[s+(1<<j)][j] = dp[s][idx]+j-cnt
                        tank.add((s+(1<<j))*dg + j)
                else:
                    cnt += 1  
        new = tank
    
    res = min(dp[2**n-1])

    if res == 10**5:
        print(-1)
    else:
        print(res)


if __name__ == '__main__':
    main()

### FIXED CODE ###



def main():
    import sys
    input = sys.stdin.readline
    n = int(input())
    a = tuple(map(int,input().split()))
    b = tuple(map(int,input().split()))

    dg = 20

    pp = 20

    dp = [10**5]*(10**7)

    new = set([])
    for i in range(n):
        s = (1<<i)
        dp[s*pp+i] = i
        new.add(s*dg+i)

    for k in range(n-1):
        tank = set([])
        if len(new) == 0:
            break
        for popelt in new:
            s,idx = popelt//dg,popelt%dg
            if (k-idx)%2 == 0:
                fr = a[idx]
            else:
                fr = b[idx]
            cnt = 0
            for j in range(n):
                if (s>>j)&1 == 0:
                    if (j-k)%2 == 1:
                        val = a[j]
                    else:
                        val = b[j]
                    if val >= fr and dp[(s+(1<<j))*pp+j] > dp[s*pp+idx]+j-cnt:
                        dp[(s+(1<<j))*pp+j] = dp[s*pp+idx]+j-cnt
                        tank.add((s+(1<<j))*dg + j)
                else:
                    cnt += 1
        new = tank
    
    res = 10**5
    s = (2**n-1)*pp
    for i in range(n):
        if res > dp[s+i]:
            res = dp[s+i]
    if res == 10**5:
        print(-1)
    else:
        print(res)


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

from collections import deque

def bfs(grid, costs, queue, wall, res):
    ans = 0
    dxy = [(1,0), (-1,0), (0,1), (0,-1)]
    while res:
        y,x = queue.popleft()
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall:
                continue
            queue.append([y_,x_])
            grid[y_][x_] = wall
            costs[y_][x_] = costs[y][x] + 1
            ans = max(ans, costs[y_][x_])
            res -= 1
    return ans

import sys
input = sys.stdin.readline
H,W = map(int, input().split())
wall = '#'
queue = deque([])
edges = [wall for _ in range(W+2)]
grid = [edges]
costs = [[0 for _ in range(W+2)]]

for i in range(1,H+1):
    S = [wall]+list(input().strip())+[wall]
    for j in range(1,W+1):
        if S[j] == wall:
            queue.append([i,j])
    grid.append(S)
    costs.append([0 for _ in range(W+2)])
grid.append(edges)
print(bfs(grid, costs, queue, wall, H*W - len(queue)))


### FIXED CODE ###

from collections import deque

def bfs(grid, queue, wall):
    ans = 0
    dxy = [(1,0), (-1,0), (0,1), (0,-1)]
    while queue:
        cnt,y,x = queue.popleft()
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall:
                continue
            queue.append([cnt+1,y_,x_])
            grid[y_][x_] = wall
            ans = max(ans, cnt+1)
    return ans

H,W = map(int, input().split())
wall = '#'
queue = deque([])
edges = [wall for i in range(W+2)]
grid = [edges]

for i in range(1,H+1):
    S = [wall]+list(input().strip())+[wall]
    for j in range(1,W+1):
        if S[j] == wall:
            queue.append([0,i,j])
    grid.append(S)
grid.append(edges)
print(bfs(grid, queue, wall))
### BUGGY CODE ###

from heapq import heappush, heappop

def main():
    n, m = map(int,input().split())
    
    jobs = []
    for i in range(n):
        day, value = map(int,input().split())
        jobs.append((-1*value, day))
    
    cur, res = 0, 0
    done = []
    for i in range(m, 0, -1):
        cur += 1
        h = []

        for j in jobs:
            if cur >= j[1] and not j in done:
                heappush(h, j)
        try:
            r = heappop(h)

            res += -1*r[0]
            done.append(r)
        except IndexError as e:
            pass
    print(res)

if __name__ == "__main__":
    main()
        



### FIXED CODE ###

from heapq import heappush, heappop
 
def main():
    n, m = map(int,input().split())
    
    jobs = []
    for i in range(n):
        day, value = map(int,input().split())
        jobs.append((day, value))

    jobs.sort()
    
    cur, res = 0, 0
    done = []
    h = []
    for i in range(1, m+1):
        while cur < n and jobs[cur][0] <= i:
            heappush(h, -jobs[cur][1])
            cur += 1
        if not h:
            continue
        res -= heappop(h)
 
    print(res)
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import re

n = int(input())
s = input()

cntList = list()
for i in range(n+1):
    tmp = len(re.findall('#', s[:i]))
    tmp += len(re.findall('\.', s[i:]))
    cntList.append(tmp)

print(min(cntList))


### FIXED CODE ###

import re

n = int(input())
s = input()
left = 0
right = len(re.findall('\.', s))

cnt = [left + right]
for i in range(n):
    if s[i] == '#':
        left += 1
    else:
        right -= 1
    cnt.append(left + right)

print(min(cnt))
### BUGGY CODE ###

n, k, q = map(int,input().split())
b = list()
for i in range(q):
    a = int(input())
    b.append(a)
if k > q:
    for i in range(n):
        print('Yes')
else:
    for j in range(1,n+1):
        c = b.count(j)
        if c < q-k+1:
            print('No')
        else:
            print('Yes')

### FIXED CODE ###

n, k, q = map(int,input().split())
a = []
for i in range(q):
    b = int(input())
    a.append(b)
pnt = [0] * n
for x in a:
    pnt[x-1] += 1
for j in range(n):
    if pnt[j] > q-k:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

#1:初期化
N, K = map(int,input().split())
h = [int(x) for x in input().split()] #こっちのほうが早い？
dp = [0]*(N)

#3:dp実行部(貰うDP)
for i in range(1,N):
    L = [dp[j]+abs(h[i]-h[j]) for j in range(max(0,i-K),i)]
    dp[i] = min(L)

#4:出力
print(dp[N-1])


### FIXED CODE ###

# -*- coding: utf-8 -*- 
def cost(h,N,K):
    dp=[0]*N
    dp[1]=abs(h[0]-h[1])
    for i in range(2,N):
        tmp=[abs(h[i]-h[j])+dp[j] for j in range(max(0,i-K),i)]
        dp[i]=min(tmp)
    return dp[-1]

N,K=map(int,input().split())
h=[int(num) for num in input().split()]

print(cost(h,N,K))
### BUGGY CODE ###

import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    mod = 10 ** 9 + 7
    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            ans += A[i] * A[j] % mod
            ans %= mod
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    mod = 10 ** 9 + 7
    s = sum(A)
    q = sum([a ** 2 for a in A])
    ans = (((s * s) - q)) // 2
    print(int(ans % mod))


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

import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
xli = na()
li = []
ans = 1000000000
for i in range(n - k + 1):
    t = xli[i:i + k]
    if t[0] * t[-1] < 0:
        ans = min(ans, 2 * abs(t[0]) + abs(t[-1]), abs(t[0]) + 2 * abs(t[-1]))
    else:
        if t[0] <= 0:
            ans = min(ans, abs(t[0]))
        else:
            ans = min(ans, abs(t[-1]))
print(ans)


### FIXED CODE ###

import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
xli = na()
li = []
ans = 1000000000
idx = -1
for i in range(n):
    if xli[i] > 0:
        idx = i
        break
if idx == -1:
    print(abs(xli[-k]))
elif idx == 0:
    print(abs(xli[k-1]))
else:
    for i in range(max(idx - k, 0), min(idx+1, n-k+1)):
        if xli[i] * xli[i + k -1] < 0:
            ans = min(ans, 2 * abs(xli[i]) + xli[i + k -1], abs(xli[i]) + 2 * xli[i + k -1])
        else:
            if xli[i + k -1] <= 0:
                ans = min(ans, abs(xli[i]))
            else:
                ans = min(ans, abs(xli[i + k -1]))
    print(ans)