Problem:


Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).



Sample input:
5 3
10
15
11
14
12


Sample output:
2

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, K = [int(x) for x in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
h.sort()
a = N - K
i = 0
h_left = h[:a]
h_right = h[K:]
h_middle = h[a:K]
if a != N//2:
    ans = min(h_middle[-1] - h_left[0], h_right[-1] - h_middle[0]) 
    for i in range(len(h_right)-1):
        j = a - i - 1
        ans_ = h_right[i] - h_left[j]
        ans = min(ans, ans_)
    print(ans)
else:
    ans = min(h_middle[-1] - h_left[0], h_right[-1] - h_right[0]) 
    for i in range(len(h_right)-1):
        j = a - i - 1
        ans_ = h_right[i] - h_left[j]
        ans = min(ans, ans_)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, K = [int(x) for x in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
h.sort()
a = N - K
i = 0
h_left = h[:a]
h_right = h[K:]
h_middle = h[a:K]
ans = min(h_middle[-1] - h_left[0], h_right[-1] - h_middle[0]) 
for i in range(len(h_right)-1):
    j = a - i - 1
    ans_ = h_right[i] - h_left[j]
    ans = min(ans, ans_)
print(ans)

Submission status: Runtime Error

Solution code:
def main(n: int, k: int, h: list):
    h.sort()

    print(min([h[i + k - 1] - h[i] for i in range(n - k + 1)]))


if __name__ == "__main__":
    n, k = map(int, input().split())
    h = [input() for _ in range(n)]

    main(n, k, h)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=[int(input()) for i in range(n)]
a.sort()
res=float("INF")
for i in range(0,n-k+1):
    b=a[i+2]-a[i]
    if res>b:
        res=b
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
h = [int(input()) for i in range(N)]
h.sort()
print(min(h[i+K-1] - h[i] for i in range(N-K+1)))

Submission status: Runtime Error

Solution code:
n = int(input())
sentences = [[] for i in range(n)]

for i in range(n):
    nn = int(input())

    for j in range(nn):
        p, t = map(int, input().split())
        sentences[i].append([p - 1, t])

ans = 0
count = 0
for i in range(2 ** n):
    check = {}
    success = True
    for j in range(n):
        # print('i, j:', i, j, bin(i))
        if ((i >> j) & 1):
            sentence = sentences[j]

            for pp, tt in sentence:
                if pp in check:
                    if check[pp] == tt:
                        pass                         
                    else:
                        success = False
                        break
                else:
                    check[pp] = tt
            #print(check)
        else:
            sentence = sentences[j]

            for pp, tt in sentence:
                if pp in check:
                    if check[pp] != tt:
                        pass                         
                    else:
                        success = False
                        break
                else:
                    if tt == 1:
                        check[pp] = 0
                    else:
                        check[pp] = 1

        if success == False:
            # print('break')
            break

    if success:
        # print(check)
        count = bin(i).count('1')    
        ans = max(ans, count)

    # print('c, a:', count, ans)

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
h_max = 0
h_min = 0
r = 0
ans = []
#print(h)
for i in range(r,n-k+1):
    if n - r < k: 
        break
    h_max = h[r+k]
    #print(h[r:k])
    h_min = h[r])
    ans.append(h_max - h_min)
    r += 1
print(min(ans))

Submission status: Runtime Error

Solution code:
Cn,k = map(int,input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
h_max = 0
h_min = 0
r = 0
ans = []
#print(h)
for i in range(r,n):
    h_max = max(h[r:k+r])
    #print(h[r:k])
    h_min = min(h[r:k+r])
    if n -r < k: 
        break
    ans.append(h_max - h_min)
    r += 1
print(min(ans))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
ret=float("inf")
print(min([abs(l[i]-l[i+k]) for i in range(n-k)]))

Submission status: Runtime Error

Solution code:
n,k = [int(i) for i in input().split()]
h = [int(inoput()) for _ in range(n)]
h.sort()

ans = 10**9
for i,j in zip(h,h[k-1:]):
  ans = min(ans,j-i)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [(int(input()) for _ in range(N)]
fin = h[K-1] - h[0]
for i in range(1, N-K+1):
  t = h[i+K-1] - h[i]
  if fin > t :
    fin = t
print(fin)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [(int(input()) for _ in range(N))]
fin = h[K-1] - h[0]
for i in range(1, N-K+1):
  t = h[i+K-1] - h[i]
  if fin > t :
    fin = t
print(fin)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [list(int(input()) for _ in range(N))]
fin = h[K-1] - h[0]
for i in range(1, N-K+1):
  t = h[i+K-1] - h[i]
  if fin > t :
    fin = t
print(fin)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

lis = []

for _ in range(n):
    lis.append(int(input())

lis.sort()

min = 10**10
for i in range(len(lis)-k+1):
    dist = lis[i+k-1] - lis[i]
    if min > dist: min = dist
print(min)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
L = []
for val in rnage(n):
    L.append(val)

L = sorted(L)
ans_L = []
for i in range(n-k+1):
    ans = L[i+k-1] - L[i]
    ans_L.append(ans)
print(min(ans_L))

Submission status: Runtime Error

Solution code:
n.k = map(int,input().split())
L = []
for val in rnage(n):
    L.append(val)

L = sorted(L)
ans_L = []
for i in range(n-k+1):
    ans = L[i+k-1] - L[i]
    ans_L.append(ans)
print(min(ans_L))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
hlist = [int(input()) for _ in range(N)]
h_sorted = sorted(hlist)
mae_3 = sum(h_sorted)[:3]
usiro_3 = sum(h_sorted)[-3:]
print(mae_3 if mae_3 < usiro_3 else usiro_3)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().strip().split())
#print(N, K)
H = []
for i in range(N):
    h = int(input().strip())
    H.append(h)
#print(H)
H.sort()
#print(H)
smallest = 10 ** 10
least = []
for i in range(len(H) - K + 1):
    test = H[i:i + K]
    difference = test[2] - test[0]
    if difference < smallest:
        smallest = difference
        least = test
print(smallest)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h = [int(input()) for _ in range(N)]

h.sort()
m = sum(h)

for i in range(n - k):
    m = min(h[i + k] - h[i], m)
print(m)

Submission status: Runtime Error

Solution code:
#AtCoder

N, K = (int(x) for x in input().split())
S = (int(input()) for _ in range(N))

lst = list(S)

print(max(lst)-min)

Submission status: Runtime Error

Solution code:
(n, k) = [int(x) for x in input().split()]
banig = []
for x in range(n):
  banig.append(int(input()))
banig.sort()

difBanig = []
for mini, maxi in zip(range(0,n-k+1), range(n-k,n+1)):
  difBanig.append(banig[maxi] - banig[mini])
print(min(difBanig))

Submission status: Runtime Error

Solution code:
(n, k) = [int(x) for x in input().split()]
banig = []
for x in range(n):
  banig.append(int(input()))
banig.sort()

difBanig = []
for mini, maxi in zip(range(0,n-k+1), range(n-k,n+1)):
  difBanig.append(banig[maxi] - banig[mini])
print(max(difBanig))

Submission status: Runtime Error

Solution code:
nk = input()
nk = nk.split()
n = int(n[0])
k = int(n[1])
trees = []
for i in range(n):
  trees.append(int(input()))
trees.sort()
minimum = 1000000000
i = 0
while i<=n-k:
  if trees[i+k-1]-trees[i]<minimum:
    minimum = trees[i+k-1]-trees[i]
  i+=1
print(minimum)

Submission status: Runtime Error

Solution code:
n, k = (int(x) for x in input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h.sort()
ans = []
for i in range(n-k-1):
    ans.append(h[i+k-1]-h[i])
print(min(ans))

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

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()
h = [INT() for _ in range(N)]

h.sort()

ans = INF
for i in range(K):
	ans = min(ans, h[i+K-1]-h[i])

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
H = sorted(H)
mi = 10**9
for i in range(N-K+1):
    _ = H[i+K] - H[i]
    if mi > _:
        mi = _
print(mi)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
H = H.sorted()
mi = 10**9
for i in range(N-K+1):
    _ = H[i+K] - H[i]
    if mi > _:
        mi = _
print(mi)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = map(int, open(0))
H = H.sorted()
mi = 10**9
for i in range(N-K+1):
    _ = H[i+K] - H[i]
    if mi > _:
        mi = _
print(mi)

Submission status: Runtime Error

Solution code:
n,k = map(int,input())
h = [int(input()) for i in range(n)].sort()
ans = 1000000000
for i in range(n-k):
  ans = min(ans, h[i+k-1]-h[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input())
h = [int(input()) for i in range(n)].sort()
ans = 1000000000
for i in range(n-k+1):
  ans = min(ans, h[i+k-1]-h[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input())
h = [int(input()) for i in range(n)].sort()
ans = 1000000000
for i in range(n-k):
  ans = min(ans, h[i+k]-h[i])
print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list()
    for i in range(N):
        h.append(int(input()))

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K + 1):
        if res > h[i] - h[i+K]:
            res = h[i] - h[i+K]
    
    print(res)

Submission status: Runtime Error

Solution code:
def christmasEve():
    N, K = map(int, input().split())
    h = [int(input()) for i in range(N)]
    strees = sorted(h)
    result=10**9
    for i in range(len(strees)):
        result = min(result, strees[i+K-1] - strees[i])
    print(result)

if __name__ == "__main__":
    christmasEve()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
lst = []
for i in range(N):
    lst.append(int(input()))
lst.sort()
dif = 10**9
for i in range(N):
    if lst[i+K-1] - lst[i] < dif:
        dif = lst[i+K-1] - lst[i]
print(dif)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
lst = []
for i in range(N):
    lst.append(int(input()))
lst.sort()
dif = 10**9
for i in range(K):
    if lst[i+K-1] - lst[i] < dif:
        dif = lst[i+K-1] - lst[i]
print(dif)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h = []
for i in range(n):
  h.append(int(input()))

h.sort()
  
ans = []
for i in range(n-k+1):
  ans.append(h[i+k]-h[i])
  
print(min(h))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)])
ans = 1000000000

for i in range(n - k + 1):
    ans = min(ans, h[i + k - 11] - h[i])

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a=[]
for i in range(n):
	a.append(int(input()))

sa = sorted(a)
v=1000000
for i in range(n-k+1):
	if v > sa[i+k-1]-sa[i] 
		v = sa[i+k-1]-sa[i]
print(v)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
d=[0]*(n-1)
for j in range(n-1):
  d[j+1]=d[j]+h[j+1]-h[j]
m=float('inf')
for l in range(1,n-k+1):
  m=min(m,d[l+k-1]-d[l])
print(m)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
d=[0]*(n-1)
for j in range(n-1):
  d[j+1]=d[j]+h[j+1]-h[j]
m=1000000001
for l in range(1,n-k+1):
  m=min(m,d[l+k-1]-d[l])
print(m)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
d=[0]*(n-1)
for j in range(n-1):
  d[j+1]=d[j]+h[j+1]-h[j]
m=1000000001
for l in range(n-k):
  m=min(m,d[l+k-1]-d[l])
print(m)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
h = [int(input()) for i in range(N)]
h.sort()
ans=10**9
for i in range(n-k+1):
    ans=min(ans,max(h[i:i+k])-min(h[i:i+k]))
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
h = []
for i in range(n):
	h.append(int(input()))
h.sort()

ans_l = []
for i in range(n-k+1):
	ans_l.append(ans_l[i+k-1] - ans_l[i])
print(min(ans_l))

Submission status: Runtime Error

Solution code:
N, K=map(int,input().split())2h=[int(input())foriinrange(N)]3h.sort()4print(min(h[i+K-1]-h[i]foriinrange(N-K+1)))

Submission status: Runtime Error

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


def main():
	n,k = map(int,input().split())
	h = []
	for i in range(n):
		h.append(int(input()))
	h.sort()
	print(h)
	for i in range(n-k+1):
		ans.append(h[i+k-1] - h[i])
	print(min(ans))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input())

tree = [int(input()) for i in range(n)]
tree.sort()

res = (tree[i+k-1] - tree[i] for i in range(n-k+1))
print(min(res))

Submission status: Runtime Error

Solution code:
n, k = map(int, input())

tree = [int(input()) for i in range(n)]
tree.sort()

res = (tree(i+k-1) - tree(i) for i in range(n-k+1))
print(min(res))

Submission status: Runtime Error

Solution code:
n, k = map(int, input())

tree = [int(input()) for i in range(n)]
tree.sort()

res = (tree(i+k-1) - tree(i) for i range(n-k+1))
print(min(res))

Submission status: Runtime Error

Solution code:
n, k = map(int, input())

tree = [int(input()) for i in range(n)]
tree.sort()

res = (tree(i+k-1) - tree(i) for i range(n-k+11)
print(min(res))

Submission status: Runtime Error

Solution code:
import itertools

def q49(n, k, h):
    h.sort()
    a = [h[i+k-1]-h[i] for i in range(n-k-1)]
    return min(a)
            
N, K = [int(i) for i in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
print(q49(N, K, h))

Submission status: Runtime Error

Solution code:
import itertools

def q49(n, k, h):
    h.sort()
    a = [h[i+k-1]-h[i] for i in range(n-1)]
    return min(a)
            
N, K = [int(i) for i in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
print(q49(N, K, h))

Submission status: Runtime Error

Solution code:
import itertools

def q49(n, k, h):
    h.sort()
    a = [h[i+k-1]-h[i] for i in range(n-2)]
    return min(a)
            
N, K = [int(i) for i in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
print(q49(N, K, h))

Submission status: Runtime Error

Solution code:
# 2019/07/25

input=open(0).readline

n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]

h.sort()
ans=float('inf')
for i in range(n-2):
    res=h[i+k-1]-h[i]
    ans=min(ans,res)

print(ans)

Submission status: Runtime Error

Solution code:
n , k = list(map(input().split()))
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
min = h[-1]
for i in range(n - k):
    if min > h[i + k] - h[i]:
        min = h[i + k] - h[i]
print(min)

Submission status: Runtime Error

Solution code:
1 N, K = map(int, input().split())
2 h = [int(input()) for i in range(N)]
3 h.sort()
4 print(min(h[i+K-1] - h[i] for i in range(N-K+1)))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]

# 小さい順にソートする
#def sort(h):
#    if len(h) <= 1:
#        return h
#    base = h[-1]
#    low_temp = [];
#    high_temp = [];
#    base_count = 1
#    for i in range(len(h)-1):
#        if h[i] < base:
#            low_temp.append(h[i])
#        elif h[i] > base:
#            high_temp.append(h[i])
#        else:
#            base_count += 1
#    low = sort(low_temp)
#    high = sort(high_temp)
#    return low + [base]*base_count + high

h.sort()
hantei = 10*9
for i in range(N-K+1):
    if (h[i+K-1] - h[i]) < hantei:
        hantei = h[i+K] - h[i]
print(hantei)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]

# 小さい順にソートする
#def sort(h):
#    if len(h) <= 1:
#        return h
#    base = h[-1]
#    low_temp = [];
#    high_temp = [];
#    base_count = 1
#    for i in range(len(h)-1):
#        if h[i] < base:
#            low_temp.append(h[i])
#        elif h[i] > base:
#            high_temp.append(h[i])
#        else:
#            base_count += 1
#    low = sort(low_temp)
#    high = sort(high_temp)
#    return low + [base]*base_count + high

h.sort()
hantei = 10*9
for i in range(N-K+1):
    if (h[i+K] - h[i]) < hantei:
        hantei = h[i+K] - h[i]
print(hantei)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]

# 小さい順にソートする
#def sort(h):
#    if len(h) <= 1:
#        return h
#    base = h[-1]
#    low_temp = [];
#    high_temp = [];
#    base_count = 1
#    for i in range(len(h)-1):
#        if h[i] < base:
#            low_temp.append(h[i])
#        elif h[i] > base:
#            high_temp.append(h[i])
#        else:
#            base_count += 1
#    low = sort(low_temp)
#    high = sort(high_temp)
#    return low + [base]*base_count + high

h.sort()
hantei = 10**10
for i in range(N-K+1):
    temp = h[i:i+K]
    if (temp[-1] - temp[0]) < hantei:
        hantei = temp_dif

print(hantei)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
# 小さい順にソートする
#def sort(h):
#    if len(h) <= 1:
#        return h
#    base = h[-1]
#    low_temp = [];
#    high_temp = [];
#    base_count = 1
#    for i in range(len(h)-1):
#        if h[i] < base:
#            low_temp.append(h[i])
#        elif h[i] > base:
#            high_temp.append(h[i])
#        else:
#            base_count += 1
#    low = sort(low_temp)
#    high = sort(high_temp)
#    return low + [base]*base_count + high
h.sort()
hantei = 10**10
for i in range(N-K+1):
    temp = h[i:i+K]
    temp_dif = temp_dif[-1] - temp_dif[0]
    if temp_dif < hantei:
        hantei = temp_dif
print(hantei)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]

# 小さい順にソートする
#def sort(h):
#    if len(h) <= 1:
        return h
#    base = h[-1]
#    low_temp = [];
#    high_temp = [];
#    base_count = 1
#    for i in range(len(h)-1):
#        if h[i] < base:
#            low_temp.append(h[i])
#        elif h[i] > base:
#            high_temp.append(h[i])
#        else:
#            base_count += 1
#    low = sort(low_temp)
#    high = sort(high_temp)
#    return low + [base]*base_count + high

h.sort()
hantei = 10**10
for i in range(N-K+1):
    temp = h[i:i+K]
    temp_dif = max(temp) - min(temp)
    if temp_dif < hantei:
        hantei = temp_dif

print(hantei)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
h_list = []
for i in range(n):
    h_list.append(int(input()))
h_list.sort()
min = 10**10
for j in range(n-k+1):
    if min > h_list[j+2] - h_list[j]:
        min = h_list[j+2] - h_list[j]

print(min)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
lst =[]
for i in range(N):
    a = int(input())
    lst.append(a)
lst.sort(reverse=True)
l = []
l.append(lst[K-1])
l.append(lst[K])
l.append(lst[K+1])
print(abs(max(l)-min(l)))

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
ls = k[int(input()) for _ in range(n)]
ls.sort()
tmp = 10 ** 9
for i in range(n-k+1):
    diff = ls[i+k-1] - ls[i]
    if diff < tmp:
        tmp = diff
print(diff)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]
A.sort()
m=A[K-1]-A[0]
for i in range(N-K):
    if m > A[K+i]-A[i+1]:
        m = A[K+1]-A[i+1]
print(m)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())

height_list = [0] * N

for n in range(N):
    height_list[n] = int(input())
height_list = sorted(height_list, reverse=True)

diff_list = [0] * (N - 2)

for i in range(N - 2):
    diff_list[i] = height_list[i] - height_list[i + K - 1]
print(min(diff_list))

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
ans = min(h[i+K-1] - h[i] for i in range(N-k+1))
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
c = [int(input()) for _ in range(n)]
c.sort()
ans = []
for i in range(n-k+1):
    ans.append(c[i+k]-c[i])
print(min(ans))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
c = [int(input()) for _ in range(n)]

c.sort()

ans = []
for i in range(n-k+1):
    ans.append(max(c[i:k+i])-min(c[i:k+i])))

print(min(ans))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
c = [int(input()) for _ in range(n)]

c.sort()

for i in range(n-k+1):
    ans = min(ans,max(c[i:k+i])-min(c[i:k+i]))

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split(" "))


H = sorted([int(input()) for i in range(N)],reverse=True)


#print(H)

L = []

for k in range(N-K+1):
    #print(k,k+1,k+2)
    
    L.append(H[k]-H[k+K])

print(min(L))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split(" "))


H = sorted([int(input()) for i in range(N)],reverse=True)


#print(H)

L = []

for k in range(N-K+1):
    #print(k,k+1,k+2)
    
    L.append(H[k]-H[k+2])

print(min(L))

Submission status: Runtime Error

Solution code:
import numpy as np
import math as m


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

H = []

for i in range(N):
    h = int(input())

    H.append(h)

def Merge(A, p, q, r):
    n_1 = q - p + 1
    n_2 = r - q
    B = A[p : q + 1]
    B = np.append(B, 10e11)
    C = A[q + 1 : r + 1]
    C = np.append(C, 10e11)
    i = 0
    j = 0
    k = p

    while i <= n_1 - 1 or j <= n_2 - 1:
        if B[i] <= C[j]:
            A[k] = B[i]
            i += 1
            k += 1
        else:
            A[k] = C[j]
            j += 1
            k += 1

    return A

def Merge_Sort(A, p, r):
    if p < r:
        q = m.floor( (p + r)/2 )
        A = Merge_Sort(A, p, q)
        A = Merge_Sort(A, q + 1, r)
        A = Merge(A, p, q, r)

    return A

H = np.array(H)

p = 0
r = len(H) - 1

H = Merge_Sort(H, p, r)

Dummy = []

for i in range(N - K + 1):

    Dummy.append(H[K - 1 + i] - H[i]) 

print(min(int(Dummy)))

Submission status: Runtime Error

Solution code:
import numpy as np
import math as m


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

H = []

for i in range(N):
    h = int(input())

    H.append(h)


def Merge(A, p, q, r):
    n_1 = q - p + 1
    n_2 = r - q
    B = A[p : q + 1]
    B = np.append(B, 100000)
    C = A[q + 1 : r + 1]
    C = np.append(C, 100000)
    i = 0
    j = 0
    k = p

    while i <= n_1 - 1 or j <= n_2 - 1:
        if B[i] <= C[j]:
            A[k] = B[i]
            i += 1
            k += 1
        else:
            A[k] = C[j]
            j += 1
            k += 1

    return A

def Merge_Sort(A, p, r):
    if p < r:
        q = m.floor( (p + r)/2 )
        A = Merge_Sort(A, p, q)
        A = Merge_Sort(A, q + 1, r)
        A = Merge(A, p, q, r)

    return A


H = np.array(H)

p = 0
r = len(H) - 1

H = Merge_Sort(H, p, r)

Answer = 10e10

for i in range(N - K + 1):

    Dummy = H[K - 1 + i] - H[i]
    if Answer > Dummy:
        Answer = Dummy

print(int(Answer))

Submission status: Runtime Error

Solution code:
import numpy as np
import math as m

# 初期値を入力

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

H = []

for i in range(N):
    h = int(input())

    H.append(h)

# 関数定義

def Merge(A, p, q, r):
    # 配列Aのpからr番目の部分配列をソートすることを目的とする関数．ここで，pからqの部分配列と，q+1からrの部分配列はそれぞれソートされているものとする．
    n_1 = q - p + 1
    n_2 = r - q
    B = A[p : q + 1]
    B = np.append(B, 100000)
    C = A[q + 1 : r + 1]
    C = np.append(C, 100000)
    i = 0
    j = 0
    k = p

    while i <= n_1 - 1 or j <= n_2 - 1:
        if B[i] <= C[j]:
            A[k] = B[i]
            i += 1
            k += 1
        else:
            A[k] = C[j]
            j += 1
            k += 1

    return A

def Merge_Sort(A, p, r):
    if p < r:
        q = m.floor( (p + r)/2 )
        A = Merge_Sort(A, p, q)
        A = Merge_Sort(A, q + 1, r)
        A = Merge(A, p, q, r)

    return A


# メインプログラム

H = np.array(H)

p = 0
r = len(H) - 1

H = Merge_Sort(H, p, r)

Answer = 10e10

for i in range(N - K + 1):

    Dummy = H[K - 1 + i] - H[i]
    if Answer > Dummy:
        Answer = Dummy

print(int(Answer))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])

print(min(H[i+K-1] - H[i] for i in range(N-2)))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])

ans = 10**9
for i in range(N-2):
    diff = H[i+K-1] - H[i]
    if diff < ans:
        ans = diff
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])

ans = 10**9
for i in range(N-K+1):
    diff = H[i+2] - H[i]
    if diff < ans:
        ans = diff
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import math as m

# 初期値を入力

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

# H = []
# 
# for i in range(N):
#     h = int(input())
# 
#     H.append(h)

H = [int(input()) for i in range(N)]

# 関数定義

def Merge(A, p, q, r):
    # 配列Aのpからr番目の部分配列をソートすることを目的とする関数．ここで，pからqの部分配列と，q+1からrの部分配列はそれぞれソートされているものとする．
    n_1 = q - p + 1
    n_2 = r - q
    B = A[p : q + 1]
    B = np.append(B, 100000)
    C = A[q + 1 : r + 1]
    C = np.append(C, 100000)
    i = 0
    j = 0
    k = p

    while i <= n_1 - 1 or j <= n_2 - 1:
        if B[i] <= C[j]:
            A[k] = B[i]
            i += 1
            k += 1
        else:
            A[k] = C[j]
            j += 1
            k += 1

    return A

def Merge_Sort(A, p, r):
    if p < r:
        q = m.floor( (p + r)/2 )
        A = Merge_Sort(A, p, q)
        A = Merge_Sort(A, q + 1, r)
        A = Merge(A, p, q, r)

    return A


# メインプログラム

H = np.array(H)

p = 0
r = len(H) - 1

H = Merge_Sort(H, p, r)

Answer = 10e10

for i in range(N - K + 1):

    Dummy = H[K - 1 + i] - H[i]
    if Answer > Dummy:
        Answer = Dummy

print(int(Answer))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
ans=[]
for i in range(len(h)-K+1):
  b=h[K+1-i]-h[i]
  ans.append(b)
print(min(b))

Submission status: Runtime Error

Solution code:
def ans(n,x):
    if n == 1:
        if x == 1:
            return 0
        elif x == 2:
            return 1
        elif x == 3:
            return 2
        else:
            return 3
    else:
        if x == 1:
            return 0
        if 2 <= x <= 2**(n+1) -2:
            return ans(n-1,x-1)
        if x == 2**(n+1) -1:
            return 2**n - 1 + 1
        if 2**(n+1) <= x <= 2**(n+2) -4:
            return 2**n - 1 + 1 + ans(n-1,x-2**(n+1)+1)
        else:
            return 2 * (2**n - 1) + 1
        
N, X = map(int,input().split())

print(ans(N,X))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
h=list(int(input())for i in range(n))
h.sort()
l=[]
for i in range(n-k+1):
    l.append(h[k-i+1]-h[i])


print(min(l))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
h=list(int(input())for i in range(n))
h.sort()
l=[]
for i in range(n-k+1):
    l.append(h[k-i+1]-h[i])


print(min(h))

Submission status: Runtime Error

Solution code:
import heapq

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

h = []
for _ in range(n):
    heapq.heappush(h, int(input()))

q = [heapq.heappop(h) for _ in range(k-1)]

ans = 1e9
while len(h):
    p = i % (k-1)
    s = q[p]
    e = heapq.heappop(h)
    q[p] = e
    ans = min(ans, e - s)

print(ans)