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:
n, k  = map(int,input().split()) 

wlist = list(int(input()) for i in range(n))

wlist.sort()

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

Submission status: Runtime Error

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

wlist = list(int(input()) for i in range(n))

wlist.sort()

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

Submission status: Runtime Error

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

wlist = list(int(input()) for i in range(n))

slist.sort()

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

Submission status: Runtime Error

Solution code:
a=list(map(int,input().split()))
b=list(sorted([int(input()) for i in range(a[0])]))
ans=[]
for i in range(a[0]-a[1]+1):
  ans+=[b[i+2]-b[i]]
print(min(ans))

Submission status: Runtime Error

Solution code:
a=list(map(int,input.split()))
lis=[]
for i in range(a[0]):
  lis+=int(input())
lis=lis.sort()
print(lis)

Submission status: Runtime Error

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

H.sort(reverse=True)

min_score = pow(10, 9)
for i in range(N - K + 1):
    min_score = min(min_score, H[i] - H[i + K - 1])

print(min_score)

Submission status: Runtime Error

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

min_diff = 1000000000
for i in range(N-K+1):
    min_diff = min(min_diff, H[i+2] - H[i])

print(min_diff)

Submission status: Runtime Error

Solution code:
N, M = list(map(int, input().split()))
K = [int(input()) for i in range(N)]
K = sorted(K)
print(min([K[i+M-1]-K[i] for i in range(N-M+1)])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])
ret = 10 ** 9
for i in range(N-K+1):
    ret = min(H[i+2] - H[i], ret)

print(ret)

Submission status: Runtime Error

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

h.sort()
m = []
for i in range(N):
    m.append(h[i+K-1] - h[i])

print(min(m))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
maxA=int(input())
minA=maxA
A=[]
A.append(minA)

for i in range(k-1):
  b=int(input())
  if len(A)<k:
    if b<=minA
      minA=b
      A.append(b)
    elif b>=maxA:
      maxA=b
      A.append(b)
    else:
      A.append(b)
  else:
    if b>=minA and b<=maxA:
      A.append(b)
      A.remove(max(A))
      maxA=max(A)
print(max(A)-min(A))

Submission status: Runtime Error

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

h.sort()

min_d = 100000
for i in range(n-k+1):
    d = h[i+k] - h[i]
    if d < min_d:
        min_d = d

print(min_d)

Submission status: Runtime Error

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    h = []
    for _ in range(N):
      h.append(int(input()))
  else:
    # N = 5
    # K = 3
    # h = [10,15,11,14,12]
    N = 5
    K = 3
    h = [5,7,5,7,7]

  h.sort()
  for i in range(0,len(h)-2):
    if i == 0:
      ans = 0
      for n in range(0,K-1):
        ans += (h[n+1] - h[n])
    else:
      tmp = 0
      for n in range(i, i+K-1):
        tmp += (h[n+1] - h[n])
      if ans > tmp:
        ans = tmp
  
  print(ans)

main()

Submission status: Runtime Error

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    K = int(input())
    h = []
    for _ in range(N):
      h.append(int(input()))
  else:
    # N = 5
    # K g= 3
    # h = [10,15,11,14,12]
    N = 5
    K = 3
    h = [5,7,5,7,7]

  h.sort()
  for i in range(0,len(h)-2):
    if i == 0:
      ans = (h[2] - h[1]) + (h[1] - h[0])
    else:
      tmp = (h[i+2] - h[i+1]) + (h[i+1] - h[i])
      if ans > tmp:
        ans = tmp
  
  print(ans)

main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def dfs(start,cnt):
    if cnt <= 0:
        return float('inf')
    diff = max(list[start:start+K]) - min(list[start:start+K])
    return min(diff,dfs(start+1,cnt-1))

print(dfs(0,K))

Submission status: Runtime Error

Solution code:
import numpy as np

N,K = list(map(int, input().split(",")))
h = []
for _ in range(N):
    h.append(int(input()))
h = sorted(h)
res = h[N-1] - h[0]
for i in np.arange(0,N-K+1,1):
    tmp = h[i+K-1] - h[i]
    if res > tmp:
        res = tmp
    
print(res)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math
n,k=map(int,input().split())
hList=[int(input()) for a in range(n)]
hList.sort()
min=100000000
for a in range(n-k+1):
    min=min([min,hList[a+k-1]-hList[a]])
print(min)

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.sort()
    l = 0
    r = K-1
    from math import inf
    ans = inf
    while r <= N-1:
        sub = H[r] - H[l]
        ans = min(ans, sub)
        r += 1
        l += 1
     
    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.sort()
l = 0
r = K-1
from math import inf
ans = inf
while r <= N-1:
    sub = H[r] - H[l]
    ans = min(ans, sub)
    r += 1
    l += 1

print(ans)

Submission status: Runtime Error

Solution code:
a, b = [int(i) for i in input().split()]
c = []
for i in range(a):
    c.append(int(input()))

c.sort()

temp = 999999999999999999999999999999999999999999999999999999999999999999999999999999999
for a in range(len(c) - b + 1):
    if c[a + 2] - c[a] < temp:
        temp = c[a + 2] - c[a]

print(temp)

Submission status: Runtime Error

Solution code:
a, b = [int(i) for i in input().split()]
c = []
for i in range(a):
    c.append(int(input()))

c.sort()

temp = 99999999
for a in range(len(c) - b + 1):
    if c[a + 2] - c[a] < temp:
        temp = c[a + 2] - c[a]

print(temp)

Submission status: Runtime Error

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

SORT_h = sorted(h)

MIN_h = 10^9
for i in range(len(SORT_h)-2):
    TEMP = h[i+2] - h[i]
    
    if TEMP < MIN_h:
        MIN_h = TEMP
        
print(MIN_h)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
h=sorted([int(input()) for _ in range(n)])
d=n-k
print(min(h[i+d]-h[i] for i in range(d+1)))

Submission status: Runtime Error

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

a = []
y = []

for x in range(m):
    b = int(input())
    a.append(b)
    
a = sorted(a,reverse= True)

for i in range(n):
    total = a[i] - a[i + n - 1]
    y.append(total)
    
print(min(y))

Submission status: Runtime Error

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

h = sorted(h)
Lh = len(h)
while Lh > K:
    print(h)
    head = h[1] - h[0]
    tail = h[Lh-1]-h[Lh-2]
    if head > tail:
        h = h[1:]
    else:
        h = h[:Lh-1]
    Lh = len(h)
        
print(h[Lh-1]-h[0])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
trees_num, illuminations_num = map(int, input().split())
trees = sorted(int(input()) for _ in range(trees_num))

diffs = []

for i in range(trees_num - illuminations_num + 1):
    h_max = max(trees[i:illuminations_num+1])
    h_min = min(trees[i:illuminations_num+1])

    diffs.append(h_max - h_min)

print(min(diffs))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
lengths = list(int(input()) for i in range(N))
sorted_lengths = sorted(lengthes)
min_score = 2**100

for i in range(N-K+1):
  score = sorted_lengths[i+K-1] - sorted_lengths[i]
  min_score = min(min_score, score)

print(min_score)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[n-k-1] - s[0]
for i in range(1,n-k+1):
    if s[i+n-k] - s[i] < c:
        c = s[i+n-k] - s[i]

print(c)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[n-k] - s[0]
for i in range(1,n-k+1):
    t = s[i+n-k] - s[i]
    if s[i+n-k] - s[i] < c:
        c = t

print(c)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = 10 ** 12
for i in range(n-k+1):
    t = s[i+n-k] - s[i]
    if s[i+n-k] - s[i] < c:
        c = t

print(c)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
hs = sorted([int(input()) for i in range(N)])
anslist = []
for h in range(K-1,N+1,K-1):
      anslist.append(hs[h]-hs[h-(K-1)])
print(min(anslist))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
tree = []
for i in range(N):
  tree.append(int(input()))
tree.sort()
mini = 1000000000
for i in range(N-K+1):
  mini = min(mini, tree[i+K-1]-[i])
print(mini)

Submission status: Runtime Error

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

ans = 1e9+7
for i in range(N-K+1):
  ans = min(ans, h[i+K]-h[i])
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
hs = [int(input()) for _ in range(N)]
hs_sort = sorted(hs)
diff_min = 1e5
for i in range(N-K+1):
    diff = hs_sort[i+2] - hs_sort[i]
    if diff < diff_min:
        diff_min = diff
    else:
        continue
print(diff_min)

Submission status: Runtime Error

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

m = []
for i in range(N-K+1):
    m.append(h[i+2] - h[i])
print(min(m))

Submission status: Runtime Error

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

m = []
for i in range(N - K + 1):
    m.append(h[i+2] - h[i])
print(min(m))

Submission status: Runtime Error

Solution code:
import numpy as np
N, K = map(int,input().split())
H = np.array([int(input()) for _ in range(0, N)])
H = np.sort(H)
#print(H)
H_diff = abs(H[1:]-H[:-1])
#print(H_diff)
best = np.sum(H_diff[0, K-1])
for i in range(0, len(H_diff)-K+2):
    #print(i)
    if np.sum(H_diff[i:i+K-1]) < best:
        #print(H_diff[i:i+K-1])
        best = np.sum(H_diff[i:i+K-1])
#print('best:{}'.format(best))
print(best)

Submission status: Runtime Error

Solution code:
import scipy as sp

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

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

tree.sort()

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

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
h = []
for _ in range(N):
    h.append(int(input()))
h = sorted(h)
tmp = 0
loop = N-K+1
for i in range(loop):
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
h = []
for _ in range(N):
    h.append(int(input()))
h = sorted(h)
tmp = 0
ans = 1000000000
loop = min(K+1,N-K+1)
for i in range(loop+1):
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
h = []
for _ in range(N):
    h.append(int(input()))
h = sorted(h)
tmp = 0
ans = 1000000000
if (N == K):
    ans = sum(h)
else:
    for i in range(K):
        tmp = h[K+i-1] - h[0+i]
        if(tmp < ans):
            ans = tmp
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
h = []
for _ in range(N):
    h.append(int(input()))
h = sorted(h)
tmp = 0
ans = 1000000000
for i in range(K):
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N, K = [int(x) for x in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))

height.sort()
height.reverse()
sublist = []
for i in range(N-K+1):
    hmax-hmin = height[i]-height[i+K-1]
    sublist.append(hmax-hmin)

print(min(sublist))

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N, K = [int(x) for x in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))

height.sort()
height.reverse()
sublist = []
for i in range(N-K+1):
    hmaxhmin = height[i]-height[i+K]
    sublist.append(hmaxhmin)

print(min(sublist))

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N, K = [int(x) for x in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))

height.sort()
height.reverse()
sublist = []
for i in range(N-K+1):
    hmaxhmin = height[i]-heght[i+K]
    sublist.append(hmaxhmin)

print(min(sublist))

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N, K = [int(x) for x in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))

height.sort()
height.reverse()
sublist = []
for i in range(N-K+1):
    hmax-hmin = height[i]-heght[i+K]
    sublist.append(hmax-hmin)

print(min(sublist))

Submission status: Runtime Error

Solution code:
import numpy as np

N,K=list(map(int,input().split()))
h=np.zeros(N)
for i in range(N):
    h[i]=int(input())
    
h=np.sort(h)
h_min=10**9
for i in range(N-K+1):
    dh=h[i+K-1]-h[i]
    if dh<dh_min:
        dh_min=dh
print(dh_min)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split(" "))
H=[int(input()) for _ in range(N)]
H.sort()
tmax=10**9
for a in range(N-K+1):
    h1,h2=H[a],H[a+K]
    tmax=min(tmax,abs(h1-h2))
    if(tmax==0):
        break
print(tmax)

Submission status: Runtime Error

Solution code:

n,k=map(int,(input().split()))
a=[]
for i in range(k):
    a.append(int(input()))
    
a.sort(reverse=True)
for i in range(n-k):
    b=int(input())
    if max(a)>b:
        a.remove(max(a))
        a.append(b)
    elif min(a)<b:
        a.remove(min(b))
        a.append(b)

print(max(a)-min(a))

Submission status: Runtime Error

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

h.sort()

ans = 10 ** 9
for i in range(N-K+1):
    ans = min(ans, h[i+K]-h[i])

print(ans)

Submission status: Runtime Error

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

def solve(N,K,trees):
    trees.sort()

    diffs = []
    for i,h in enumerate(trees[:N-K+1]):
        min_h = h
        max_h = trees[i+K-1]
        print(min_h,max_h)
        diffs.append(max_h-min_h)

    print(min(diffs))
return

solve(N,K,trees)

Submission status: Runtime Error

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

trees.sort()

diffs = []
for i,h in trees[:N-K]:
  min_h = h
  max_h = trees[i+K]
  diffs.append(max_h-min_h)

print(min(diffs))

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):
        # print(i)
        dif = H[i+K-1] - H[i]
        ans.append(dif)

print(min(ans))

Submission status: Runtime Error

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

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

H.sort()

ans = []
for i in range(N-K-1):
        # print(i)
        dif = H[i+K-1] - H[i]
        ans.append(dif)

print(min(ans))

Submission status: Runtime Error

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

H.sort()
ans = []

for i in range(N-K-1):
        # print(i)
        dif = H[i+K-1] - H[i]
        ans.append(dif)

print(min(ans))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
H=[int(input()) for i in range(n)]
H=H.sort(reverse=True)
HK=H[0:k+1]
result=int(max(HK)-min(HK))
print(result)

Submission status: Runtime Error

Solution code:
NK=int(input())
ListNK=NK.split() #ListNK[0]=N,[1]=K
N=int(ListNK[0])
K=int(ListNK[1])
Listh=[]
ListQ=[]
for i in range(0,N,1):
  hi=int(input())
  Listh.append(hi)

Listh.sort() #Listhの要素を小さい順に並べる
for i in range(0,N-K,1):
  Q=int(Listh[K+i-1]-Listh[i]) #hmax-hminの候補.Qminを求めればよい
  ListQ.append(Q)
ListQ.sort()
print(ListQ[0])

Submission status: Runtime Error

Solution code:
NK=int(input())
ListNK=NK.split() #ListNK[0]=N,[1]=K
N=ListNK[0]
K=ListNK[1]
Listh=[]
ListQ=[]
for i in range(0,N,1):
  hi=int(input())
  Listh.append(hi)

Listh.sort() #Listhの要素を小さい順に並べる
for i in range(0,N-K+1,1):
  Q=int(Listh[K+i-1]-Listh[i]) #hmax-hminの候補.Qminを求めればよい
  ListQ.append(Q)
ListQ.sort()
print(ListQ[0])

Submission status: Runtime Error

Solution code:

# coding: utf-8

# In[3]:


import numpy as np


# In[19]:


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


# In[20]:


h = np.array(h)


# In[21]:


sorted_h = np.sort(h)


# In[22]:


print(min(sorted_h[k-1:] - sorted_h[:k]))

Submission status: Runtime Error

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

list = sorted(list_original)
sub = 10000000000

for j in range(n-k+1):
    if list[j+k-1]-list[j] <= sub:
        sub =list[j+k-1]-list[j]
    else:
        pass

print(sub)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
li=range(n)
for i in range(n):
    li[i]=int(input())
li.sort()
ans=100000000000
for i in range(n-k+1):
    if ans>(li[i+k-1]-li[i]):
        ans=li[i+k-1]-li[i]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
h = list(map(int,input().split()))
h.append(0)

count=0
while 1:
  flag = 0
  for i in range(N):
    if h[i]>0:
      h[i] -= 1
      flag = 1
      if h[i+1] == 0:
        count += 1
      else:
        pass
  if flag == 0:
    break
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
h = list(map(int,input().split()))
h.append(0)

count=0
while 1:
	flag=0
    for i in range(N):
    	if h[i]>0:
        	h[i] -= 1
      	    flag = 1
        if h[i+1] == 0:
        	count += 1
        else:
        	pass
    if flag == 0:
    	break
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
h = list(map(int,input().split()))
h.append(0)

count=0
while 1:
  flag=0
  for i in range(N):
    if h[i]>0:
      h[i] -= 1
      flag = 1
      if h[i+1] == 0:
        count += 1
      else:
        pass
  if flag == 0:
    break
print(count)

Submission status: Runtime Error

Solution code:
def main():
    N, K = map(int, input().split())
    h = [int(input()) for count in range(n)]
    h.sort()
    min_value = min(h[i + K - 1] - h[i] for i in range(N - K + 1))
    print(min_value)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
a,b=input().split()
c,d=int(a),int(b)
l=[int(input()) for i in range(c)]
L=sorted(l)
x=L[d]-L[0]
for i in range(c-d+1):
    if L[d+i]-L[i]<x:
        x=L[d+i]-L[i]
print(x)

Submission status: Runtime Error

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

hn.sort()

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

Submission status: Runtime Error

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

# 方針として、高さの違いをソートして小さい順からk-1個取得すればよい
hn.sort()

print(hn)

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

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  int N, K;
  cin >> N >> K;
  vector <int> h(N,0);
  for( int i=0;i<N;i++ )
    cin >> h[i];

  sort(h.begin(), h.end());

  int ans = 1000000000;
  for( int i=0;i<N-K+1;i++ )
    {
      ans = min(ans, h[i+K-1]-h[i]);
    }

  cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
#115-C
n,k = (int(i) for i in input().split())
t = [int(input()) for i in range(n)] 
#t = [10,15,11,14,12]
t = sorted(t)
min = float('inf')
for i in range(n-k):
    s = t[i+k-1]-t[i]
    if s < min:

print(min)
#print(t)

Submission status: Runtime Error

Solution code:
n,k = (int(i) for i in input().split())
t = [int(input()) for i in range(n)] 
t = [10,15,11,14,12]
t = sorted(t)
min = float('inf')
for i in range(n-k):
    s = t[i+k-1]-t[i]
    if s < min:

print(min)
#print(t)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
NK = [int(i) for i in input().split()]
N,K = NK[0],NK[1]
h = []
h1 = []
for i in range(N):
    h.append(int(input()))
h.sort()
for i in range(N-1):
    h1.append(h[i+1]-h[i])
hh,hhh = [],[]
for i in range(N-K):
    hh.append([0]*i+h1+[0]*(K-2-i))
for i in range(N-K+1):
    h2 = 0
    for j in range(K-1):
        h2 = h2 + hh[j][i+K-2]
    hhh.append(h2)
print(min(hhh))

Submission status: Runtime Error

Solution code:
NK = [int(i) for i in input().split()]
N,K = NK[0],NK[1]
h = []
h1 = []
for i in range(N):
    h.append(int(input()))
h.sort()
for i in range(N-1):
    h1.append(h[i+1]-h[i])
hh,hhh = [],[]
for i in range(N-K):
    hh.append([0]*i+h1+[0]*(K-2-i))
for i in range(N-K+1):
    h2 = 0
    for j in range(K-1):
        h2 = h2 + hh[j][i+K-2]
        hhh.append(h2)
print(min(hhh))

Submission status: Runtime Error

Solution code:
NK = [int(i) for i in input().split()]
N,K = NK[0],NK[1]
h = []
h1 = []
for i in range(N):
  h.append(int(input()))
h.sort()
for i in range(N-1):
  h1.append(h[i+1]-h[i])
hh,hhh = [],[]
for i in range(N-K):
  hh.append([0]*i+h1+[0]*(K-2-i))
for i in range(N-K+1):
  h2 = 0
  for j in range(K-1):
      h2 = h2 + hh[j][i+K-2]
  hhh.append(h2)
print(min(hhh))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
h=[i for i in range(n)]
h.sort()
l=[]
for i in range(k-2):
  l.appwnd(abs(h[i]-h[i+k-1]))
print(min(l))

Submission status: Runtime Error

Solution code:
import multiprocessing as mp


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

H = []

for n in range(N):
    H.append(int(input()))

H.sort()

R = [H[i:i+K] for i in range(N+1-K)]


def mp_func(L):
    result = 0
    for i in range(len(L)-1):
        result += L[i+1] - L[i]
    return result

P = mp.Pool(mp.cpu_count())
R = P.map(mp_func, R)

print(min(R))

Submission status: Runtime Error

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

x=[]
for i in range(n):
    x.append(int(input()))

x.sort()

y=[]

for j in range(n):
  p=x[j+k-1]-x[j]
  y.append(p)

y.sort()

print(y[0])

Submission status: Runtime Error

Solution code:

def solve():
    N, K = map(int, input().split())
    data = [int(input()) for _ in range(N)]
    data.sort()
    ans = []
    cnt = 0
    while True:
        h_max1 = data[cnt + K - 1]
        h_min1 = data[cnt]
        h_max2 = data[-cnt-1]
        h_min2 = data[-cnt - K]
        tmp1 = h_max1 - h_min1
        tmp2 = h_max2 - h_min2
        ans.append(tmp1)
        ans.append(tmp2)
        cnt = cnt + 1
        if data[cnt] == data[-cnt-K]:
            break

    print(min(ans))


solve()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

trees=[]
for i in range(n):
  trees.append(int(input()))
  
trees=sorted(trees)

dist=[]

num=n//k+2
for i in range(num):
  dist.append(trees[i+k-1]-trees[i])
  
dist=sorted(dist)

print(int(dist[0]))

Submission status: Runtime Error

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

trees=[]
for i in renge(n):
  trees.append(int(input()))
  
trees=sorted(trees)

dist=[]

num=n//k+2
for i in range(num):
  dist.append(trees[i+k-1]-trees[i])
  
dist=sorted(dist)

print(int(dist[0]))

Submission status: Runtime Error

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

trees=[]
for i in renge(n):
  trees.append(int(input()))
  
trees=sorted(trees)

dist=[]

num=n//k+2
for i in range(num):
  dist.append(trees[i+k-1]-trees[i])
  
dist=sorted(dist)

print(int(dist[0]))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
res = []
for i in range(N-K+1):
    res.append(tmp[i+K-1] - tmp[i])
print(min(res))

Submission status: Runtime Error

Solution code:
n, k = input().split()
n = int(n)
k = int(k)
i = 0
j = 0
m = 0
o = 0
p = 0
arr = []
min = 10000
while( i < n ):
  arr.append(int(input()))
  i = i + 1
  while( j < n ):
    j1 = j + 1
    while(j1 < n):
      if arr[j1] < arr[j]:
        p = arr[j]
        arr[j] = arr[j1]
        arr[j1] = p 
        j1 = j1 + 1
        j = j + 1
        while( m < len(arr) - k):
          while( o < k):
            if min > arr[m+o] - arr[m]:
              min = arr[m+o] - arr[m]
              o += 1
              m += 1
              print(min)

Submission status: Runtime Error

Solution code:
n, k = input().split()
n = int(n)
k = int(k)
i = 0
j = 0
p = 0
arr = []
while( i < n ):
  arr.append(int(input()))
  i = i + 1
  while( j < n ):
    j1 = j + 1
    while(j1 < n):
      if arr[j1] < arr[j]:
        p = arr[j]
        arr[j] = arr[j1]
        arr[j1] = p 
        j1 = j1 + 1
        j = j + 1
print(arr[k -1] - arr[0])

Submission status: Runtime Error

Solution code:
n, k = input().split()
n = int(n)
k = int(k)
i = 0
j = 0
p = 0
arr = []
while( i < n ):
  arr.append(int(input()))
  i = i + 1
  while( j < n ):
    j1 = j + 1
    while(j1 < n):
      if arr[j1] < arr[j]:
        p = arr[j]
        arr[j] = arr[j1]
        arr[j1] = p 
        j1 = j1 + 1
        j = j + 1
        print(arr[k -1] - arr[0])

Submission status: Runtime Error

Solution code:
N, k = [int(s) for s in input().split(' ')]
 
h = []
while len(h) != N:
    heapq.heappush(h, int(input()))
  
min_height = 1000000000
h.sort()
for start in range(0, N - k + 1):
    end = start + k - 1
    diff = h[end] - h[start]
    if diff < min_height:
        min_height = diff
print(min_height)

Submission status: Runtime Error

Solution code:
import heapq

N, k = int(input()), int(input())

h = []
while N > 0:
  heapq.heappush(h, int(input()))
  N -= 1
  
trees = heapq.nlargest(k, h)
if len(trees) <= 1:
  print(0)
else:
  print(trees[-1] - trees[0])

Submission status: Runtime Error

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


def check(k, hs, mx, mn, r):
    if mx - mn > r:
        return r
    if k == 0:
        return min(r, mx - mn)
    for i, h in enumerate(hs):
        hs.remove(h)
        r = min(r, check(k - 1, hs, max(mx, h), min(mn, h), r))
        hs.insert(i, h)
    return r


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9, 10 ** 9))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def check(k, hs, mx, mn, memo):
    if (k, mx, mn) in memo:
        return memo[(k, mx, mn)]
    if k == 0:
        return mx - mn
    r = []
    for i, h in enumerate(hs):
        hs.remove(h)
        r.append(check(k - 1, hs, max(mx, h), min(mn, h), memo))
        hs.insert(i, h)
    memo[(k, mx, mn)] = min(r)
    return min(r)


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9, {}))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def check(k, hs, mx, mn):
    if k == 0:
        return mx - mn
    r = []
    for i, h in enumerate(hs):
        hs.remove(h)
        r.append(check(k - 1, hs, max(mx, h), min(mn, h)))
        hs.insert(i, h)
    return min(r)


def main():
    hs = [int(input()) for i in range(N)]
    print(check(K, hs, 0, 10 ** 9))


if __name__ == '__main__':
    main()