Harryxun/stf_run huggingface dataset:
def dfs(r, c):
    global c, color
    S = [r]
    color[r] = c
    while (0 < len(S)):
        u = S.pop()
        for u in G[u]:
            v = color[ch]
            if (v == False):
                color[u] = c
                S.append(u)
 
def assignColor():
    global c, color
    c = 1
    for i in range(n):
        if (color[i] == False):
            dfs(i, c)
            c += 1
 
n, m = list(map(int, input().split()))
 
G = [False for i in range(n)]
for i in range(m):
    s, t = list(map(int, input().split()))
    G[s].append(t)
    G[t].append(s)
 
color = [False for i in range(n)]
 
assignColor()
 
q = int(input())

for i in range(q):
    s, t = list(map(int, input().split()))
    if (color[s] == color[t]):
        print("yes")
    else:
        print("no")

### FIXED CODE ###

def dfs(r, c):
    global color, G
    S = [r]
    color[r] = c
    while (0 < len(S)):
        u = S.pop()
        for u in G[u]:
            v = color[u]
            if (v == None):
                color[u] = c
                S.append(u)
 
def assignColor():
    global color, G, n
    c = 1
    for i in range(n):
        if (color[i] == None):
            dfs(i, c)
            c += 1
 
n, m = list(map(int, input().split()))
 
G = [[] for i in range(n)]
for i in range(m):
    s, t = list(map(int, input().split()))
    G[s].append(t)
    G[t].append(s)
 
color = [None for i in range(n)]
 
assignColor()
 
q = int(input())

for i in range(q):
    s, t = list(map(int, input().split()))
    if (color[s] == color[t]):
        print("yes")
    else:
        print("no")
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
c=0
for i in range(k):
    c += l[-1-k]
print(c)


### FIXED CODE ###

n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
c=0
for i in range(k):
    c += l[-1-i]
print(c)
N,K = map(int,input().split())

Red = N-K
Blue = K
mod = 1000000007

ans = 1
for i in range(Blue):
    if Red < i:
            ans = 0
    elif i != 0:
        ans = (ans * (Red + 1 - i) * (Blue - i) // ((i + 1) * i))
    else:
        ans = (ans * (Red + 1 - i) // (i + 1))
    print(int(ans) % mod)


### FIXED CODE ###

S = input()

count = 0
if S[0] == S[1] and S[2] == S[3]:
    count += 1

if S[0] == S[2] and S[1] == S[3]:
    count += 1

if S[0] == S[3] and S[1] == S[2]:
    count += 1

if count == 1:
    print('Yes')
else:
    print('No')
from collections import defaultdict
N,X,M = map(int,input().split())
ans = 0
d = defaultdict(lambda:-1)
x = X%M
for n in range(M):
    if x in d:
        T = n-d[x]
        break
    else:
        d[x] = n
    x *= x
    x %= M
p = 0
y = x
for m in range(d[x],n):
    p += y
    y *= y
    y %= M

ans = 0
n = N
z = X%M
while n:
    ans += z
    n -= 1
    z *= z
    z %= M
    if z==x:
        ans += n//T*p
        n %= T
print(ans)

### FIXED CODE ###

from collections import defaultdict
N,X,M = map(int,input().split())
ans = 0
d = defaultdict(lambda:-1)
x = X%M
for n in range(M+100):
    if x in d:
        T = n-d[x]
        break
    else:
        d[x] = n
    x *= x
    x %= M
p = 0
y = x
for m in range(d[x],n):
    p += y
    y *= y
    y %= M

ans = 0
n = N
z = X%M
while n:
    ans += z
    n -= 1
    z *= z
    z %= M
    if z==x:
        ans += n//T*p
        n %= T
print(ans)
import numpy as np

n = int(input())
arr = np.array(list(map(int, input().split())))

def solve()
for i in range(arr.shape[0] - 1):
    if np.gcd(arr[i], arr[i + 1]) != 1:
        ans1 = False
        break
else:
    ans1 = True

ans2 = np.gcd.reduce(arr) == 1

if ans1 and ans2:
    print("pairwise coprime")
elif ans2:
    print("setwise coprime")
else:
    print("not coprime")

### FIXED CODE ###

import numpy as np
from numba import njit


n = int(input())
arr = np.array(list(map(int, input().split())))


n = 10**6 + 1
primes = set(range(3, n+1, 2))

for i in range(3, int(n**0.5+1)):
    primes.difference_update(range(i*2, n+1, i))
primes.add(2)


@njit
def solve(arr, primes):
    cnt = np.bincount(arr)
    for p in primes:
        if (cnt[p::p]).sum() > 1:
            return False
    else:
        return True


if np.gcd.reduce(arr) != 1:
    print("not coprime")
elif solve(arr, primes):
    print("pairwise coprime")
else:
    print("setwise coprime")
import unittest

from app.ABC102_C import run


class TestSample(unittest.TestCase):
    def test_run(self):
        self.assertEqual(run(5, [2, 2, 3, 5, 5]), 2)
        self.assertEqual(run(9, [1, 2, 3, 4, 5, 6, 7, 8, 9]), 0)
        self.assertEqual(run(6, [6, 5, 4, 3, 2, 1]), 18)
        self.assertEqual(run(7, [1, 1, 1, 1, 2, 3, 4]), 6)


if __name__ == '__main__':
    unittest.main()

### FIXED CODE ###

def run(n, a):
    a = [a[i]-i-1 for i in range(len(a))]
    a.sort()
    m = a[int((n-1)/2)]
    ret = 0
    for ai in a:
        ret += abs(ai - m)
    return ret


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


def main():
    n, a = read_line()
    print(run(n, a))


if __name__ == '__main__':
    main()
def main():
	N, K = [int(v) for v in input().split()]
    
    H = sorted([-int(input() for _ in range(N)])
    
    print(-H[0] + H[K - 1])
    
main()



### FIXED CODE ###

def main():
    N, K = [int(v) for v in input().split()]
    H = sorted([-int(input()) for _ in range(N)])

    min_diff = 10 ** 10
    for i in range(K - 1, N):
        min_diff = min(min_diff, -H[i - K + 1] + H[i])
    print(min_diff)

main()
print_val = "False"

A,B,C = map(int, input().split())
if A == B and B == C:
  print_val = "True"
print print_val

### FIXED CODE ###

print_val = "No"
 
A, B, C = map(int, raw_input().split())

if A == B and B == C:
  print_val = "Yes"

print print_val
x=int(input())
print(x/3600, ':', (x-x/3600)/60, ':', x-{(x-x/3600)/60}, sep='')



### FIXED CODE ###

x=int(input())
a=x//3600
b=(x-a*3600)//60
c=x-(a*3600+b*60)
print(a, ':', b, ':', c, sep='')
int(input())
print(-1*int(input())+sum(list(map(int,input().split()))))


### FIXED CODE ###

n=int(input())
print(sum(list(map(int,input().split())))-n)
N=int(input())
lst=list(map(int,input().split()))
P=round(sum(lst)/N)
print(sum[(i-P)**2 for i in lst])

### FIXED CODE ###

N=int(input())
lst=list(map(int,input().split()))
P=round(sum(lst)/N)
print(sum([(i-P)**2 for i in lst]))
n = int(input())
if n[1] == n[2]:
    if n[0] == n[1] or n[2] == n[3]:
        print('Yes')
        exit()
print('No')

### FIXED CODE ###

n = input()
if n[1] == n[2]:
    if n[0] == n[1] or n[2] == n[3]:
        print("Yes")
        exit()
print("No")
a, b = map(int,input().split())
ans = 0
while ((a-1)*ans < b):
  ans++
print(ans)

### FIXED CODE ###

a, b = map(int,input().split())
ans = 0
plug = 1
while (plug < b):
  plug += a-1
  ans += 1
print(ans)
aqqn = list(input())
ans = 0
for i in range(2**(len(n)-1)):
    plus = [""]*(len(n))
    fomula = ""
    for j in range(len(n)-1):
        if(i>>j & 1):
            plus[j] = "+"
    for i in range(len(n)):
        fomula += n[i] + plus[i]
    ans += eval(fomula)
print(ans)


### FIXED CODE ###

n = list(input())
ans = 0
for i in range(2**(len(n)-1)):
    plus = [""]*(len(n))
    fomula = ""
    for j in range(len(n)-1):
        if(i>>j & 1):
            plus[j] = "+"
    for i in range(len(n)):
        fomula += n[i] + plus[i]
    ans += eval(fomula)
print(ans)
# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
LENGTH =int(input())
A = input().split()

# 変数
N = 0
M = LENGTH - 1
CHANGE = 0

# BubbleSort(A)
#  for i = 0 to A.length-1
while N <= LENGTH -1 :
    M = LENGTH - 1
#    print ('N=',N)
#      for j = A.length-1 downto i+1
    while M >= N + 1:
#       print  ('-M=',M)
#          if A[j] < A[j-1]
        if A[M] < A[M-1] :
            tmp = A[M-1]
            A[M-1] = A[M]
            A[M] = tmp
            CHANGE += 1
        M -= 1
    N += 1

print(" ".join(map(str,A)))
print (CHANGE)



### FIXED CODE ###

# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
LENGTH =int(input())
A = input().split()

# 変数
N = 0
M = LENGTH - 1
CHANGE = 0

# BubbleSort(A)
#  for i = 0 to A.length-1
while N <= LENGTH -1 :
    M = LENGTH - 1
#    print ('N=',N)
#      for j = A.length-1 downto i+1
    while M >= N + 1:
#       print  ('-M=',M)
#          if A[j] < A[j-1]
        if int(A[M]) < int(A[M-1]) :
            tmp = A[M-1]
            A[M-1] = A[M]
            A[M] = tmp
            CHANGE += 1
        M -= 1
    N += 1

print(" ".join(map(str,A)))
print (CHANGE)
#!/usr/bin/env python3
import sys
from collections import Counter

def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = Counter(a)
    if len(l)==1 and 0 in l:
        print('Yes')
        exit()
    if N%3==0 and len(l)==2 and 0 in l and l[0] == N//3:
        print('Yes')
        exit()
    if N%3==0 and len(l)==3:
        x,y,z = l.keys()
        if l[x] == l[y] == N//3 and x^y^z == 0:
            print('Yes')
            exit()
    print('No')


    

if __name__ == '__main__':
    main()


### FIXED CODE ###

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

def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = Counter(a)
    if len(l)==1 and 0 in l:
        print('Yes')
        exit()
    if N%3==0 and len(l)==2 and 0 in l and l[0] == N//3:
        print('Yes')
        exit()
    if N%3==0 and len(l)==3:
        x,y,z = l.keys()
        if l[x] == l[y] == N//3 and x^y^z == 0:
            print('Yes')
            exit()
    print('No')

if __name__ == '__main__':
    main()
n=int(input())
A=[]
B=[]
c=0
for i in range(n):
  S=input()
  a=0
  b=0
  for j in range(len(S)):
    if S[j]=="(":
      a=a+1
    else:
      a=a-1
      if a<b:
        b=a
  if b==0:
    c=c+a
  elif a>0:
    B.append([b,-1*a])
  else:
    A.append([-1*b,a])
A.sort()
B.sort()
f=0
for i in range(len(B)):
  if c+B[i][0]<0:
    f=1
  c=c-A[i][1]
for i in range(len(A)):
  if c-A[i][0]<0:
    f=1
  c=c+A[i][1]
if c!=0:
  f=1
if f==0:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

n=int(input())
A=[]
B=[]
c=0
for i in range(n):
  S=input()
  a=0
  b=0
  for j in range(len(S)):
    if S[j]=="(":
      a=a+1
    else:
      a=a-1
      if a<b:
        b=a
  b=b*-1
  if b==0:
    c=c+a
  elif a>=0:
    B.append([b,-1*a])
  else:
    A.append([b+a,a])
A.sort()
B.sort()
f=0
for i in range(len(B)):
  if c-B[i][0]<0:
    f=1
  c=c-B[i][1]
d=0
for i in range(len(A)):
  if d-A[i][0]<0:
    f=1
  d=d-A[i][1]
if c!=d:
  f=1
if f==0:
  print("Yes")
else:
  print("No")
import numpy as np
from heapq import *

X, Y, Z, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
B = np.array(list(map(int, input().split())))
C = np.array(list(map(int, input().split())))

# 二分探索でK個の選び方を探す
up = 10 ** 10 + 1 # 不可能
low = 0 # 可能
while up > low + 1:
    mid = (up+low)// 2
    if (A >= A.max() - mid).sum() * (B >= B.max() - mid).sum() * (C >= C.max() - mid).sum() >= K:
        low = mid
    else:
        up = mid
A = [x for x in A if x >= A.max() - low]
B = [x for x in B if x >= B.max() - low]
C = [x for x in C if x >= C.max() - low]
L = []
heapify(L)
for a in A:
    for b in B:
        for c in C:
            heappush(L, -(a+b+c))
for i in range(K):
    n = heappop(L)
    print(-n)

### FIXED CODE ###

import numpy as np
from heapq import *

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

h = []
heappush(h, (-A[0]-B[0]-C[0], 0, 0, 0))
used = set()
result = []

for _ in range(K):
    sum, i, j, k = heappop(h)
    result.append(-sum)
    if i < X-1 and (i+1, j, k) not in used:
        heappush(h, (-A[i+1]-B[j]-C[k], i+1, j, k))
        used.add((i+1, j, k))
    if j < Y-1 and (i, j+1, k) not in used:
        heappush(h, (-A[i]-B[j+1]-C[k], i, j+1, k))
        used.add((i, j+1, k))
    if k < Z-1 and (i, j, k+1) not in used:
        heappush(h, (-A[i]-B[j]-C[k+1], i, j, k+1))
        used.add((i, j, k+1))

for v in result:
    print(v)
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, atan, degrees
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
import heapq
import numpy as np

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
A = LIST()

A_acc = list(accumulate(A))
S = [0] + A_acc
S = [(S[i]-i)%K for i in range(N+1)]

ans = 0
dic = [0]*K

for i, x in enumerate(S):
	ans += dic[x]  # 範囲内かつ今まで出てきたxの個数を探す
	dic[x] += 1
	if i >= K-1:  # 範囲外のものを捨てる
		dic[S[i-K+1]] -= 1

print(ans)


### FIXED CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, atan, degrees
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
import heapq
import numpy as np

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
A = LIST()

A_acc = list(accumulate(A))
S = [0] + A_acc
S = [(S[i]-i)%K for i in range(N+1)]

ans = 0
dic = defaultdict(int)

for i, x in enumerate(S):
	ans += dic[x]  # 範囲内かつ今まで出てきたxの個数を探す
	dic[x] += 1
	if i >= K-1:  # 範囲外のものを捨てる
		dic[S[i-K+1]] -= 1

print(ans)
N = input()
lst1 = list(map(int, input().split()))
lst2 = sorted(lst1)

n = 0
for x, y in zip(lst1, lst2):
    if x != y:
        n += 1
        if n > 2:
            break

if n > 2:
    print('NO')
else:
    print('YES')


### FIXED CODE ###

A, B = map(int, input().split())
if (A - B) % 2 == 1:
    print('IMPOSSIBLE')
else:
    print(abs(A + B) // 2)
N,L = map(int,open(0).read().split())
L.sort()
if sum(L[::-2]) < L[-1]:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

N = int(input())
L = list(map(int,input().split()))
L.sort()
if sum(L[:-1]) > L[-1]:
    print("Yes")
else:
    print("No")
n=int(input())
s=[]
for i in range(1:n+1):
  if i%3==0:
    continue
  elif i%5==0:
    continue
  else:
    s.append(i)
print(sum(s))

### FIXED CODE ###

print('YNeos'[not ('7'in input())::2])
N, K = int(input())

while N <= int(K/2):
  N = min([abs(N-K), N % K])

print(N)

### FIXED CODE ###

N, K = list(map(lambda n: int(n), input().split(" ")))
 
while N > int(K/2):
  N = min([abs(N-K), N % K])

print(N)
"""everything has a cost!"""

from math import *
import random
import functools
import collections
import sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')

n, m = 0, 0


# @functools.lru_cache(maxsize=None, typed=False)
def read(ti: int, tj: int, i: int, j: int) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
        return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m):
        return read(ti+1, tj+1, i, j)
    else:
        if (ti == i and tj == j):
            return 3
        elif (ti == i):
            return 2
        else:
            return 1


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    mins = 0
    i, j = 0, 0
    while (mins < k):
        v = read(i, j, i, j)
        # print(i, j, v)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)


### FIXED CODE ###

"""everything has a cost!"""

from math import *
import random, functools, collections, sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')


if __name__ == '__main__':
    n,m,k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    for i in range(1,len(a)):
        a[i] += a[i-1]
    for i in range(1,len(b)):
        b[i] += b[i-1]

    j = len(b)-1
    while j > -1 and b[j] > k:
        j -= 1
    ans = j+1
    i = len(a)-1
    while i > -1 and a[i] > k:
        i -= 1
    ans = max(i+1,ans)

    for inx in range(i+1):
        while j > -1 and a[inx] + b[j] > k:
            j -= 1
        ans = max(inx+j+2,ans)
    print(ans)
x=int(input())
for b in range(-10000,10000):
    a=(x+b**5)**0.2
    if a.is_integer():
        print(a,b)
        exit()

### FIXED CODE ###

x=int(input())
a=0
b=0
for i in range(500):
  for j in range(-1000,1000):
    if i**5-j**5==x:
       a=i
       b=j
       break
print(a,b)
import numpy as np


A, B, M = map(int, input().split())
a = list(map(int, input().split()))
a = np.array(a)
b = list(map(int, input().split()))
b = np.array(b)
z = [list(map(int, input().split())) for _ in range(M)]
z = np.array(z)


xx, yy = np.meshgrid(a, b)
xx1 = xx.flatten()
yy1 = yy.flatten()

xxyy = np.c_[xx1, yy1]
mem = xxyy.sum(axis=1)
# mem = list(mem)

for x, y, c in z:
    # mem.append(a[x - 1] + b[y - 1] - c)
    np.append(mem, [a[x - 1] + b[y - 1] - c])


print(min(mem))
# print(mem)


### FIXED CODE ###

A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
L = [[int(x) for x in input().split()] for _ in range(M)]

reg = min(a) + min(b)
for x, y, c in L:
    tmp = a[x-1] + b[y-1] - c
    reg = min(reg, tmp)
print(reg)
N, M = map(int, input().split())

if(2*N <= M):
    m = M-2*N
    print(N+m//4)
else:
    print(m//2)

### FIXED CODE ###

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

if(2*N <= M):
    m = M-2*N
    print(N+m//4)
else:
    print(M//2)
n=int(input())
t,a=(int(i) for i in input().split(' '))
h=[int(i) for i in input().split(' ')]
T=[]
for i in range(n):
  T[i]=t-h[i]
index=0
for i in range(n):
  if abs(T[i]-a)<abs(T[index]-a):
    index=i
print(index+1)

### FIXED CODE ###

n=int(input())
t,a=(int(i) for i in input().split(' '))
h=[int(i) for i in input().split(' ')]
T=[]
for i in range(n):
  T=T+[t-0.006*h[i]]
index=0
for i in range(n):
  if abs(T[i]-a)<abs(T[index]-a):
    index=i
print(index+1)
n = int(input())
hs = map(int, input.split())
count = 1
maxh = 0
for i in range(1,n):
	maxh = max(maxh, hs[i-1])
	if hs[i]>maxh:
		count += 1
print(count)

### FIXED CODE ###

n = int(input())
hs = input().split()
count = 1
maxh = 0
for i in range(1,n):
    maxh = max(maxh, int(hs[i-1]))
    if int(hs[i])>=maxh:
        count += 1
print(count)
N=int(input())
n=0
while N>=2**n:
  n+=1
print(2**(n-1)

### FIXED CODE ###

N=int(input())
n=0
while N>=2**n:
  n+=1
print(2**(n-1))
while True:
    C = C - B
    if C <= 0:
        print('Yes')
        break
    A = A - D
    if A <= 0:
        print('No')
        break

### FIXED CODE ###

A, B, C, D = map(int, input().split())
while True:
    C = C - B
    if C <= 0:
        print('Yes')
        exit()
    A = A - D
    if A <= 0:
        print('No')
        exit()
O = str(input().rstrip())
E = str(input().rstrip())
ans = ""
for i in range(len(O)):
    ans += O[i]
    ans += E[i]
print(ans)

### FIXED CODE ###

O = str(input().rstrip())
E = str(input().rstrip())
ans = ""
for i in range(len(O)):
    ans += O[i]
    if len(E) - (i+1) >= 0:
	    ans += E[i]
print(ans)
keys = input()
outputs = []

for key in keys:
    if key == 'B':
        if outputs is None:
            pass
        else:
            outputs.pop()
    else:
        outputs.append(key)

for output in outputs:
    print(output, end='')


### FIXED CODE ###

keys = input()
outputs = []

for key in keys:
    if key == 'B':
        if not outputs:
            pass
        else:
            outputs.pop()
    else:
        outputs.append(key)

for output in outputs:
    print(output, end='')
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        try:
            k = K - abs(i - a)
            d = abs(ary[i])
        except:print(1)
        if k > 0:
                d += abs(ary[i] - ary[k + a])
                if ans > d:
                    ans = d

print(0 if ans == float("Inf") else ans)


### FIXED CODE ###

N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)
import math
      
n = input()
      
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
      
def koch(n, px, py, qx, qy):
 if n == 0:
  return
       
 s.x = (px*2+qx*1)/3
 s.y = (py*2+qy*1)/3
 t.x = (px*1+qx*2)/3
 t.y = (py*1+qy*2)/3
 u.x = (t.x-s.x)*math.cos(math.pi/3) - (t.y-s.y)*math.sin(math.pi/3) + s.x
 u.y = (t.x-s.x)*math.sin(math.pi/3) + (t.y-s.y)*math.cos(math.pi/3) + s.y
      
 koch(n-1, px, py, s.x, s.y)
 print s.x, s.y
 koch(n-1, s.x, s.y, u.x, u.y)
 print u.x, u.y
 koch(n-1, u.x, u.y, t.x, t.y)
 print t.x, t.y
 koch(n-1, t.x, t.y, qx, qy)
      
print px, py
koch(n, px, py, qx, qy)
print qx, qy

### FIXED CODE ###

import math
          
def koch(n, h, i, j, k):
 if n == 0:
  return
           
 sx = (h*2+j)/3
 sy = (i*2+k)/3
 tx = (h+j*2)/3
 ty = (i+k*2)/3
 ux = (tx-sx)*math.cos(math.pi/3) - (ty-sy)*math.sin(math.pi/3) + sx
 uy = (tx-sx)*math.sin(math.pi/3) + (ty-sy)*math.cos(math.pi/3) + sy
          
 koch(n-1, h, i, sx, sy)
 print sx, sy
 koch(n-1, sx, sy, ux, uy)
 print ux, uy
 koch(n-1, ux, uy, tx, ty)
 print tx, ty
 koch(n-1, tx, ty, j, k)
   
n = input()
          
px = float(0)
py = float(0)
qx = float(100)
qy = float(0)
          
print px, py
koch(n, px, py, qx, qy)
print qx, qy