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: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
H = [0]*N
for i in range(N):
  H[i] = int(input())
from itertools import permutations, combinations,combinations_with_replacement,product
ans = 1000000000000000
for i in combinations(H,3):
  ans = min(ans,max(i)-min(i))
print(ans)

Submission status: Wrong Answer

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

d=10**10
for i in range(N):
  d=min(d,abs(h[i]-h[i-K]))

print(d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

h = [int(input()) for i in range(N)]
ans = float('INF')
for i in range(N - K +1):
    ans = min(ans, h[i + K -1] - h[i])
    
print(int(ans))

Submission status: Wrong Answer

Solution code:

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

h = [int(input()) for i in range(N)]
ans = float('INF')
for i in range(N - K +1):
    ans = min(ans, h[i + K -1] - h[i])
    
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())

h = [int(input()) for i in range(N)]
h = np.array(h)
h_sort = np.sort(h)

h_diff = h_sort[1:] - h_sort[:N-1]

h_diff_sort = np.sort(h_diff)

print(np.sum(h_diff_sort[:K-1]))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())

h = [int(input()) for i in range(N)]
h = np.array(h)
h_sort = np.sort(h)
h_diff = h_sort[1:] - h_sort[:N-1]
h_diff_sort = np.sort(h_diff)

print(np.sum(h_diff_sort[:K]))

Submission status: Wrong Answer

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

q=sorted(h)
ansList=[]
for c,d in zip(q,q[k-1:]):
  print(c,d)
  ansList.append(d-c)

print(min(ansList))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    h = [int(input()) for i in range(N)]
    h.sort()
    ans = 10**5 + 7
    for i in range(N-(K-1)):
        ans = min(ans,h[i+(K-1)] - h[i])
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
c = collections.Counter(h)
v, cnts = zip(*c.most_common())
result = max(h) - min(h)
if c.most_common()[0][1] >= K:
  result = 0
else:
  for i in range(len(c)-K+1):
    dict = i+K-1
    if dict < len(c):
	    result = min(result, v[dict] - v[i])

print(result)

Submission status: Wrong Answer

Solution code:
import collections
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
c = collections.Counter(h)
v, cnts = zip(*c.most_common())
result = max(h) - min(h)
if c.most_common()[0][1] >= K:
  result = 0
else:
  for i in range(len(c)+K-1):
    dict = i+K-1
    if dict < len(c):
	    result = min(result, v[dict] - v[i])

print(result)

Submission status: Wrong Answer

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

print(result)

Submission status: Wrong Answer

Solution code:
import collections
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
c = collections.Counter(h)
v, cnts = zip(*c.most_common())
result = max(h) - min(h)
if c.most_common()[0][1] >= K:
  result = 0
else:
  for i in range(len(c)):
    dict = i+K-1
    if dict < len(c):
	    result = min(result, v[dict]-v[i])

print(result)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import itertools



############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])
X = int(temp[1])

data = []
for i in range(N):
    data.append(int(stdin.readline()))
    
d = sorted(data)

min = 1000
for i in range(N-X+1):
    dif = d[i+X-1] - d[i]
    if dif < min:
        min = dif
        
print(min)

Submission status: Wrong Answer

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-1]-h[i])
  
print(min(h))

Submission status: Wrong Answer

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

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

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

print(diff)

Submission status: Wrong Answer

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

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

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

print(diff)

Submission status: Wrong Answer

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

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

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

print(diff)

Submission status: Wrong Answer

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

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

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

print(diff)

Submission status: Wrong Answer

Solution code:
N,A= [int(i) for i in input().split()]
n=[]
m=[]
for i in range(N):
	n.append(int(input()))
n.sort()
for i in range(N-1):
	m.append(n[i+1] - n[i])
m.sort() 
print(sum(m[0:(A-1)]))

Submission status: Wrong Answer

Solution code:
def christmas_eve(n,k,h):
    _h = sorted(h)
    opt = 0
    ans = 10**9 + 1
    loop = n-(k-1)
    for i in range(loop):
        l = _h[i:i+k]
        opt = max(l) - min(l)
        if ans >= opt:
            ans = opt
        else:
            break

    return ans
if __name__ == '__main__':
    n,k = map(int,raw_input().split())
    h = [int(raw_input()) for _ in range(n)]

    print christmas_eve(n,k,h)

Submission status: Wrong Answer

Solution code:
def christmas_eve(n,k,h):
    _h = sorted(h)
    opt = 0
    ans = 10**9 + 1

    for i in range(n-2):
        l = _h[i:i+3]
        opt = max(l) - min(l)
        if ans > opt:
            ans = opt

    return ans
if __name__ == '__main__':
    n,k = map(int,raw_input().split())
    h = [int(raw_input()) for _ in range(n)]


    print christmas_eve(n,k,h)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
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()
li = [ni() for i in range(n)]
li.sort()
ans = 1000000
for i in range(n - k + 1):
    dif = li[i + k - 1] - li[i]
    ans = min(ans, dif)
    if ans == 0:
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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: Wrong Answer

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):
	v = min(v,sa[i+k-1]-sa[i]) 

print(v)

Submission status: Wrong Answer

Solution code:
import sys
import math

#sys.setrecursionlimit(10000)

n,k = map(int, input().split())
h = sorted(list(map(int, (input() for i in range(n)))))

l = []
for i in range(n - 1):
    l.append(h[i + 1] - h[i])

print(l)
ans = max(l)
for j in range(len(l) - (k - 2)):
    ans = min(ans,sum(l[j:j + k - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, 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(max(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

psa=0
for i in range(n):
  sa=hs[k+i]-hs[i]
  if psa>sa:
    psa=sa
  if k+i+1==n:
    break

print(psa)

Submission status: Wrong Answer

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

psa=0
for i in range(n):
  sa=hs[k+i]-hs[i]
  if psa>sa:
    psa=sa
  if k+i+1==n:
    break

print(psa)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
hs=[int(input()) for _ in range(n)]
hs.sort()
k=k-1
sa1=hs[k]-hs[0]
sa2=hs[-1]-hs[-1-k]

if sa1<sa2:
  print(sa1)
else:
  print(sa2)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
Hs = sorted([int(input()) for i in range(N)])
nums = []
for h in range(2,len(Hs)):
    nums.append(Hs[h]-Hs[h-2])
print(min(nums))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = [0]*n
for _ in range(n):
  l[_] = int(input())
l.sort()
print(l)

ans = 10**9+1
for i in range(n-k+1):
  d = l[i+k-1]-l[i]
  ans = min(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n, k = map(int, input().split())
p = list(int(input()) for _ in range(n))
p2 = list(itertools.combinations(p,3))
ans = []

for i in range(len(p2)):
    dif = abs(max(p2[i])-min(p2[i]))
    ans.append(dif)

print(min(ans))

Submission status: Wrong Answer

Solution code:
import itertools
n, k = map(int, input().split())
p = list(int(input()) for _ in range(n))
p2 = list(itertools.combinations(p,2))
ans = []

for i in range(len(p2)):
    dif = abs(p2[i][0]-p2[i][1])
    ans.append(dif)

print(min(ans))

Submission status: Wrong Answer

Solution code:
import math
import sys
from itertools import accumulate
from functools import reduce
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = list(map(int, sys.stdin.readlines()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    n,k= map(int, sys.stdin.readline().split())
    h = list(map(int, sys.stdin.readlines()))
    h.sort()
    ans=10**5
    for i in range(n-k+1):
        # print("i+k:",h[i+k-1])
        # print("i:",h[i])
        tmp=h[i+k-1]-h[i]
        ans=min(ans,tmp)
        # print("tmp:",ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys
from itertools import accumulate
from functools import reduce
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = list(map(int, sys.stdin.readlines()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    n,k= map(int, sys.stdin.readline().split())
    h = list(map(int, sys.stdin.readlines()))
    h.sort()
    ans=10**5
    for i in range(n-k+1):
        print("i+k:",h[i+k-1])
        print("i:",h[i])
        tmp=h[i+k-1]-h[i]
        ans=min(ans,tmp)
        print("tmp:",ans)
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

list = []

for i in range(N):
  list.append(i) 
  
list.sort()

print(list[-1] - list[-K])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import heapq

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

h.sort()

ans = []

for i in range(N):
	if i+1==N:
		break
	ans.append(h[i+1]-h[i])

take=[]
for i in range(K-1):
	take.append(heapq.heappop(ans))

print(sum(take))

Submission status: Wrong Answer

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

h = []
diff = 100000000

for i in range(n):
    h.append(int(input()))

h = sorted(h)

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

print(diff)

Submission status: Wrong Answer

Solution code:
import itertools

min_all = 1000000
    
N,K = [int(i) for i in input().split(" ")]
H = [int(input()) for _ in range(N)]
H.sort()
    
for i in range(N+1-K):
  min_now = H[K-1+i] - H[i]
  if min_all > min_now:
    min_all = min_now
print(min_all)

Submission status: Wrong Answer

Solution code:
import itertools
min_all = 1000000
    
N,K = [int(i) for i in input().split(" ")]
H = [int(input()) for _ in range(N)]
H.sort()
print(H)
    
for i in range(N+1-K):
  min_now = H[K-1+i] - H[i]
  if min_all > min_now:
    min_all = min_now
print(min_all)

Submission status: Wrong Answer

Solution code:
import itertools
min_all = 1000000
    
N,K = [int(i) for i in input().split(" ")]
H = [int(input()) for _ in range(N)]
    
for h in itertools.combinations(H,K):
  min_now = max(h) - min(h)
  if min_all > min_now:
    min_all = min_now
print(min_all)

Submission status: Wrong Answer

Solution code:
I=int;w,*l,=open(0);n,k=w.split();l.sort()
print(min(I(j)-I(i)for i,j in zip(l,l[I(k)-1:])))

Submission status: Wrong Answer

Solution code:
w,*l=open(0);n,k=w.split();l.sort();print(min(int(j)-int(i) for i,j in zip(l,l[int(k)-1:])))

Submission status: Wrong Answer

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

h_dif = []
ans = 10 ** 9 + 1

for i in range(1, n):
    h_dif.append(h[i]-h[i-1])
    if i >= 2:
        ans = min(ans, sum(h_dif[i-2:i]))

print(ans)

Submission status: Wrong Answer

Solution code:
# ABC 115 C

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

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

h.sort()

ans = 1.0e+9

print(h)

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

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
tree_dict = {}
tree_list = []
for i in range(N):
    a = int(input())
    tree_list.append(a)
    if a not in tree_dict:
        tree_dict[a] = 1
    else:
        tree_dict[a] += 1

tree_list = sorted(tree_list)
min_count = 10 ** 9
tree_set = list(set(tree_list))
count = 0
is_min = False
min_num = 0
max_num = 0

for i in range(len(tree_set)):
    min_num = tree_set[i]
    max_num = tree_set[i]
    count += tree_dict[tree_set[i]]
    for u in range(i + 1, len(tree_set)):
        if count >= K:
            break
        count += tree_dict[tree_set[u]]
        max_num = tree_set[u]
    if max_num - min_num < min_count and count >= K:
        min_count = max_num - min_num
    count = 0
print(min_count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#ABC115C
import sys

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
n = hoge[0]
k = hoge[1]

h=[]
for i in range(0,n):
	tmp = int(input())
	h.append(tmp)

h.sort()
#print(h)

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

Submission status: Wrong Answer

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

height = []
for i in range(N):
  height.append(int(input()))
                
height.sort()
dif = [0]*(N-1)
for i in range(N-1):
  dif[i] = height[i+1]-height[i]

diflist = [dif[0]]*(N-1)
for i in range(1,N-1):
  diflist[i] = diflist[i-1] + dif[i]

diflist.insert(0,0)
ans = 10**5
for i in range(N-K+1):
  pro = diflist[i+K-1]-diflist[i]
  ans = min(ans,pro)
  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
ls = sorted([int(input()) for _ in range(n)])
ans = float('inf')

for i in range(n-k):
    ans = min(ans, abs(ls[i] - ls[i+k-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k=map(int,input().split())
x=list(int(input()) for _ in range(n))
y=sorted(x)

df=[]

for i in range(n-2):
	df.append(y[i+2]-y[i])
    
print(min(df))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#Coding:Utf-8

N,K=list(map(int, input().split()))
a=[int(input()) for i in range(N)]

a.sort()

min=a[N-1]-a[0]
print(a)
for l in range(N-K+1):
    if min>a[l+K-1]-a[l]:
        min=a[l+K-1]-a[l]

print(min)

Submission status: Wrong Answer

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

diff = heights[K-1] - heights[0]
ans = diff

for i in range(N)[1:N-K+1]:
  diff = heights[i+K-1] - heights[i-1]
  ans = min(ans, diff)
print(ans)

Submission status: Wrong Answer

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

diff = heights[K-1] - heights[0]
ans = diff

for i in range(N)[1:N-K+1]:
  diff = diff - heights[i-1] + heights[i+K-1]
  ans = min(ans, diff)
print(ans)

Submission status: Wrong Answer

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-1] - h[i]
print(hantei)

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int,input().split(' '))
v =[]
for i in range(0,n):
    x = int(input())
    v.append(x)
v.sort()
ans = 200000
for i in range(0,n):
    if i >= k - 1:
        ans = min(ans,abs(v[i] - v[i-k+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int,input().split(' '))
v =[]
for i in range(0,n):
    x = int(input())
    v.append(x)
v.sort()
ans = 200000
for i in range(0,n):
    ans = min(ans,abs(v[i] - v[i-k+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys 
import numpy as np

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

h = np.zeros(N)

for n in range(N):
    h[n] = int(input())

h = np.sort(h)

m = h[N-1] - h[0]

for n in range(0,N-K+1):
    d = h[n+K-1] - h[n]
    if d < m:
        m = d

print(int(d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
result = float('INF')
cumulative = [0]
for i in range(n - 1):
    cumulative.append(tree[i + 1] - tree[i])
for i in range(n - k):
    result = min(result, cumulative[i + k - 1] - cumulative[i])
print(result)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
result = float('INF')
cumulative = []
for i in range(n - 1):
    cumulative.append(tree[i + 1] - tree[i])
for i in range(n - k):
    result = min(result, cumulative[i + k - 1] + cumulative[i])
print(result)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
# 入力をすべてソートしたあと、後の操作のために末尾に２つ要素を付け加える
height = sorted([int(input()) for i in range(n)]) + [0, 0]    # 0であれば他の操作に影響しない
ans = min([height[i+2] - height[i] for i in range(n)][0:-2])  # スライスで末尾２つを省略
print(ans)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = [int(input()) for _ in range(a)]
c.sort()
d = -1
for i in range(2, a):
    if d == -1:
        d = abs(c[i] - c[i-b+1])
    else:
        if abs(c[i] - c[i-b+1]) < d:
            d = abs(c[i] - c[i-b+1])
print(d)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = [int(input()) for _ in range(a)]
c.sort()
d = 0
for i in range(2, a):
    if d == 0:
        d = abs(c[i] - c[i-b+1])
    else:
        if abs(c[i] - c[i-b+1]) < d:
            d = abs(c[i] - c[i-b+1])
print(d)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = [int(input()) for _ in range(a)]
c.sort()
d = 0
for i in range(2, a):
    if d == 0:
        d = abs(c[i] - c[i-b+1])
    else:
        if abs(c[i] - c[i-b+1]) < d:
            d = abs(c[i] - c[i-2])
print(d)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = [int(input()) for _ in range(a)]
c.sort()
d = 0
for i in range(2, a):
    if d == 0:
        d = abs(c[i] - c[i - 2])
    else:
        if abs(c[i] - c[i-2]) < d:
            d = abs(c[i] - c[i-2])
print(d)

Submission status: Wrong Answer

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

def h_find(list,n):
    k=list[n-1]-list[0]
    for i in range(1,len(list)-n+1):
        k2=list[i+n-1]-list[i]
        if k2<k:
            k=k2
        if k==0:
            break
    return k

print(h_find(h,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = input().split()
K = int(K)
N = int(N)
h = [int(input()) for i in range(N)]
h.sort()
for i in range(K - 1):
    if abs(h[0] - sum(h) / len(h)) >= abs(h[-1] - sum(h) / len(h)):
        h.pop(0)
    else:
        h.pop(-1)

print(h[K - 1] - h[0])

Submission status: Wrong Answer

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-K+1))

Submission status: Wrong Answer

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

A.sort()
print(A)
mini=10000000000
for i in range(N-K+1):
    mini=min(mini,A[i+K-1]-A[i])

print(mini)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
lst =[]
for i in range(N):
    a = int(input())
    lst.append(a)
lst = sorted(lst)
print(abs(lst[K-1]-lst[K]))

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
dis = []
for i, j in zip(h, h[1:]):
    dis += [j - i]
print(sum((sorted(dis)[:k-1])))

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h.sort()
dis = []
for i, j in zip(h, h[1:]):
    dis += [j - i]
print(dis)
print(sum((sorted(dis)[:k-1])))

Submission status: Wrong Answer

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 + 2]
print(min(diff_list))

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
h = [int(input()) for _ in range(n)]
h.sort()
import copy
b = copy.deepcopy(h)
for index in range(n):
  if index == 0:
    continue
  h[index-1] = h[index]-h[index-1]
temp = sum(h[0:k-1])
ans = k-1
for i in range(k, n):
  temp2 = temp + h[i-1] - h[i-k]
  if temp2 < temp:
    ans = i
  temp = temp2
print(b[ans]-b[ans-k+1])

Submission status: Wrong Answer

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

diff_lst = [lst[i] - lst[i+2] for i in range(N-2)]
print(min(diff_lst))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = (int(i) for i in input().split())

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

h.sort()

difference = []

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

print(min(difference))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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+2] - H[i]
    if diff < ans:
        ans = diff
print(ans)

Submission status: Wrong Answer

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

h.sort()
ans = 1e9

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

Submission status: Wrong Answer

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

for n in range(N):
    h[n] = int(input())
    
h.sort()

tree = h[:K]
height = []
height.append(tree[-1]-tree[0])

for i in range(K,N):
    tree.pop(0)
    tree.append(h[n])
    height.append(tree[-1]-tree[0])  

print(min(height))

Submission status: Wrong Answer

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

h = []

for i in range(n):
    h.append(int(input()))

h.sort()

ans = 100000

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

print(ans)

Submission status: Wrong Answer

Solution code:
in_1 = input().split()
n = int(in_1[0])
k = int(in_1[1])

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

trees_height.sort()


def sub_minmax(list):
    return max(list)-min(list)

subs = []

a = 10**5
for i in range(n+1-k):
    if sub_minmax(trees_height[i:i+k]) < a:
        a = sub_minmax(trees_height[i:i+k]) 

print(a)

Submission status: Wrong Answer

Solution code:
in_1 = input().split()
n = int(in_1[0])
k = int(in_1[1])

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

trees_height.sort()


def sub_minmax(list):
    return max(list)-min(list)

subs = []

a = 10**5+1
for i in range(n+1-k):
    if sub_minmax(trees_height[i:i+k]) < a:
        a = sub_minmax(trees_height[i:i+k]) 

print(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)])
gap=[h[i]-h[i-1] for i in range(1,n)]
tmp=0
lst=[gap[0]]
for i in range(1,len(gap)):
    lst.append(gap[i]+lst[i-1])
print(gap,lst)
ans=[lst[k-2]]
for i in range(k,n):
    ans.append(lst[i-1]-lst[i-k])
print(ans)
print(min(ans))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

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

    sums = [0 for _ in range(N)]
    sums[0] = H[0]
    for i in range(1, len(H)):
        sums[i] = sums[i-1] + H[i]

    min_cost = 100000000
    for i in range(len(H)-K+1):
        min_cost = min(min_cost, H[i+K-1]-H[i])
    print(min_cost)

main()