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 = 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):
    if(K+i > N):
        break
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Wrong Answer

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):
    if(K+i == N):
        break
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Wrong Answer

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):
    if(K+i == K):
        break
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
 
def main():
    input = sys.stdin.readline
    
    n, m = map(int, input().split())
    h = [int(input()) for i in range(n)]
    
    h.sort()
    
    ask = h[m - 1] - h[0]
    desc = h[len(h) - 1] - h[len(h) - m]
    
    print(ask if ask <= desc else desc)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[]
for _ in range(n):
  a+=[int(input())]
a.sort()
s=10**9
for i in range(n-k):
  s=min(s,a[i+k]-a[i])
print(s)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[]
for _ in range(n):
  a+=[int(input())]
a.sort()
s=10**9
for i in range(n-k):
  s=min(s,a[i]-a[i+k])
print(s)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[]
for _ in range(n):
  a+=[int(input())]
a.sort()
s=10**9
m=0
for i in range(n):
  if i>=k:
    m+=a[i]-a[i-k]
    s=min(s,m)
  else:
    m+=a[i]
print(s)

Submission status: Wrong Answer

Solution code:
def christmas_eve(N: int, K: int, H: list) -> int:
    sorted_H = sorted(H, reverse=True)
    return sorted_H[0] - sorted_H[K-1]


if __name__ == "__main__":
    N, K = [int(s) for s in input().split()]
    H = [int(input()) for _ in range(N)]
    ans = christmas_eve(N, K, H)
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
h.sort()
ans=10**6

for i in range(n-k+1):
  if ans>h[i+k-1]-h[i]:
    ans=h[i+k-1]-h[i]
    
print(ans)

Submission status: Wrong Answer

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

for i in range(n-k+1):
  if ans>h[i+k-1]-h[i]:
    ans=h[i+k-1]-h[i]
    
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

h.sort()

m = 10**10

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

print(m)

Submission status: Wrong Answer

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

h.sort()

for d in range(0,N-1):
    dif.append(h[d+1] - h[d])

sm = 10**10
for k in range(0,len(dif)-K+1):
    now = 0
    for l in range(k,K+k-1):
        now += dif[k]
    sm = min(sm,now)

print(sm)

Submission status: Wrong Answer

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

h.sort()
m = 10**5+9
for i in range(N-K+1):
    m = min(m, h[i+K-1]-h[i])
print(m)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools


sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    N, K = LI()
    h = []
    [h.append(I()) for i in range(N)]

    h = sorted(h)
    h_diff = [h[i+2] - h[i] for i in range(N-2)]
    print(min(h_diff))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
h = [int(input()) for _ in range(N)]
ans = 10000
h.sort(reverse=True)
for i in range(0,N+K,1):
  if i+K-1 == N:
    break
  elif h[i]-h[i+K-1] <= ans:
    ans = h[i]-h[i+K-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
h = [int(input()) for _ in range(N)]
ans = 10000
h.sort(reverse=True)
for i in range(0,N+K,1):
  if i+K-1 == N:
    break
  if h[i]-h[i+K-1] <= ans:
    ans = h[i]-h[i+K-1]
print(ans)

Submission status: Wrong Answer

Solution code:

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

a.sort()
min_high=1000000000000

for i in range(n-k):
    high=-a[i]+a[i+k-1]
    if high<min_high:
        min_high=high

print(min_high)

Submission status: Wrong Answer

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(a))
        a.append(b)

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

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    h = [int(input()) for _ in range(N)]
    h.sort()
    print(min([h[i + K - 1] - h[i] for i in range(N - K)]))
        
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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):
    print(h[i:i+K])
    ans = min(ans, max(h[i:i+K])-min(h[i:i+K]))

print(ans)

Submission status: Wrong Answer

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

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

H.sort()

print(H)

ans=H[n-1]-H[0]

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
h=[int(input()) for i in range(N)]
h=sorted(h)
#size:Kのスライディングウィンドウ
def_list = [h[i+K-1]-h[i] for i in range(N-K+1)]
print(def_list)
print(min(def_list))

Submission status: Wrong Answer

Solution code:
NK=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: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

min = 10000000000000

h.sort()

for i in range(0, len(h)-k):
    diff = h[i+k-1] - h[i]
    if diff < min:
        min = diff

print(min)

Submission status: Wrong Answer

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

min = 1000000000

h.sort()

for i in range(0, len(h)-k):
    diff = h[i+k-1] - h[i]
    if diff < min:
        min = diff

print(min)

Submission status: Wrong Answer

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

m = []
h.sort()
for i in range(len(h)-k):
        m.append(h[i+k-1] - h[i])
print(sorted(m)[0])

Submission status: Wrong Answer

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

m = 10e9
h.sort()
for i in range(len(h)-k):
    if h[i+k-1]-h[i] < m:
        m = h[i+k-1] - h[i]
print(m)

Submission status: Wrong Answer

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

m = 10e9
h.sort()
for i in range(len(h)-k):
    if h[i+k-1]-h[i] <= m:
        m = h[i+k-1] - h[i]
print(m)

Submission status: Wrong Answer

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

m = 10e9
h.sort()
for i in range(len(h)-k):
    if h[i+k-1]-h[i] <= m:
        print(h[i+k-1],h[i])
        m = h[i+k-1] - h[i]
print(m)

Submission status: Wrong Answer

Solution code:
# C
N, K = map(int, input().split())
h = [0]*N
for i in range(N):
    h[i] = int(input())
result = 1e10
for i in range(N-K+2):
    h_list = h[i:i+K-1]
    result = min(result, max(h_list) - min(h_list))
print(result)

Submission status: Wrong Answer

Solution code:
n = raw_input()
n,k = int(n.split(" ")[0]), int(n.split(" ")[1])

h = []
for i in range(n):
	h.append(int(raw_input()))
h = sorted(h)

min_diff = 9999999
for i in range(n-k+1):
	diff = h[i+k-1]-h[i]
	if diff<min_diff:
		min_diff = diff
print(min_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()
g = float('inf')
for i in range(n-k+1):
  g = min(g, h[i] - h[i+k-1])
print(g)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
NK = input().split(" ")
N = int(NK[0])
K = int(NK[1])

hls = []
for i in range(0,N):
    hls.append(int(input()))

hls.sort()
tousa = []

for i in range(0,N-1):
    tousa.append(abs(hls[i] - hls[i+1]))

tousa.sort()
sm = tousa[0]+tousa[K-2]
print(sm)

Submission status: Wrong Answer

Solution code:
NK = input().split(" ")
N = int(NK[0])
K = int(NK[1])

hls = []
for i in range(0,N):
    hls.append(int(input()))

hls.sort()
tousa = []

for i in range(0,N-1):
    tousa.append(abs(hls[i] - hls[i+1]))

tousa.sort()
sm = tousa[0]+tousa[K-1]
print(sm)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
h = [0]*(n+1)

for i in range(1,n+1) :
    h[i] = int(input())

h.sort()
c = h[n] #highest

for j in range(1,n-k+1) :
    d = h[j+k-1] - h[j]
    c = d + 0 if c > d else c

print(c)

Submission status: Wrong Answer

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-1]-L[0]
for i in range(c-d):
    if L[d+i-1]-L[i]<x:
        x=L[d+i-1]-L[i]
print(x)

Submission status: Wrong Answer

Solution code:
#ABC115C-ChristmasEve.py

from sys import stdin

input = stdin.readline

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

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

hdiff = [None]*(N-K+1)

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

print(h)
print(hdiff)
print(min(hdiff))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = (int(i) for i in input().split())

h=[]
for i in range(n):
    h.append(int(input()))
h=np.array(h)

sorted_h = np.sort(h)
diff = np.hstack((sorted_h[2:], (0,0)))

print(np.min(np.abs(diff - sorted_h)[:-2]))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = (int(i) for i in input().split())

h=[]
for i in range(n):
    h.append(int(input()))
h=np.array(h)

sorted_h = np.sort(h)
diff = np.hstack((sorted_h[2:], (0,0)))

print(np.min(np.abs(diff - sorted_h)))

Submission status: Wrong Answer

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

# 方針として、高さの違いをソートして小さい順からk-1個取得すればよい
hn = sorted(hn)
hn_dif = []
for i in range(0,len(hn)-1):
    hn_dif.append(hn[i+1]-hn[i])
hn_dif = sorted(hn_dif)
print(sum(hn_dif[:k-1]))

Submission status: Wrong Answer

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

# 方針として、高さの違いをソートして小さい順からk-1個取得すればよい
hn = sorted(hn)
hn_dif = []
for i in range(0,len(hn)-1):
    hn_dif.append(hn[i+1]-hn[i])
print(sum(hn_dif[:k-1]))

Submission status: Wrong Answer

Solution code:
import math
import sys
from operator import itemgetter

# N, K = map(int, input().split())
# h = list(map(int, input().split()))
# S = [input() for _ in range(H)]
# X = [[0 for j in range(100)] for i in range(200)]


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

if __name__ == '__main__':
	### input
	n, k = map(int, input().split())
	h = []
	for i in range(n):
		h.append(int(input()))
	###solve
	solve(n, k, h)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree = []
for i in range(n):
    tree.append(int(input()))
tree.sort()
num_m=100000000
for i in range(n-m+1):
    num_m = min(num_m,tree[i+m-1]-tree[i])
print(num_m)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree = []
for i in range(n):
    tree.append(int(input()))
tree.sort()
num_m=100000000
for i in range(n-m+1):
    # print(tree[i+m-1],tree[i])
    n = tree[i+m-1] - tree[i]
    if num_m>n:
        num_m = n
print(num_m)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h.sort()
ans = float('inf')
for i in range(2,n):
    if ans > h[i] - h[i-2]:
        ans = h[i] - h[i-2]
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
n, k = [int(i) for i in input().split()]

h_sel = []
for i in range(n):
    tmp = int(input())
    if len(h_sel) < k: 
        h_sel.append(tmp)
    else:
        h_max = copy.deepcopy(h_sel)
        h_max.remove(max(h_sel))
        h_max.append(tmp)
        h_min = copy.deepcopy(h_sel)
        h_min.remove(min(h_sel))
        h_min.append(tmp)
        h_sel_sub = max(h_sel)-min(h_sel)
        h_max_sub = max(h_max)-min(h_max)
        h_min_sub = max(h_min)-min(h_min)
        if h_sel_sub > h_max_sub or h_sel_sub > h_min_sub:
            if h_max_sub > h_min_sub:
                h_sel = h_min
            else:
                h_sel = h_max
        else:
            pass
h_sel_sub = max(h_sel)-min(h_sel)
print(h_sel_sub)

Submission status: Wrong Answer

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 = t[k-1]-t[0]
for i in range(n-k):
    s = t[i+k-1]-t[i]
    if s < min:
        min = s

print(min)
#print(t)

Submission status: Wrong Answer

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:
        min = s

print(min)
#print(t)

Submission status: Wrong Answer

Solution code:
l = map(long, raw_input().split())

height = []
for i in range(l[0]):
    height.append(long(input()))
    
height.sort()
print height

diff = 1000000000000L
for x in range(len(height)-l[1]+1):
  print height[x+l[1]-1] - height[x]
  if diff > height[x+l[1]-1] - height[x]:
    diff = height[x+l[1]-1] - height[x]
    
print diff

Submission status: Wrong Answer

Solution code:
l = map(long, raw_input().split())

height = []
for i in range(l[0]):
    height.append(long(input()))
    
height.sort()

diff = 100000000
for x in range(len(height)-l[1]+1):
  if diff > height[x+l[1]-1] - height[x]:
    diff = height[x+l[1]-1] - height[x]
    
print diff

Submission status: Wrong Answer

Solution code:
l = map(int, raw_input().split())

height = []
for i in range(l[0]):
    height.append(int(input()))
    
height.sort()

diff = 100000000
for x in range(len(height)-l[1]+1):
  if diff > height[x+l[1]-1] - height[x]:
    diff = height[x+l[1]-1] - height[x]
    
print diff

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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.sort()
maxa = 99999999
for i in range(n-k+1):
    if maxa>h_list[i+k-1]-h_list[i]:
        maxa=h_list[i+k-1]-h_list[i]
print(maxa)

Submission status: Wrong Answer

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

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.sort()
max = 99999999
for i in range(n-k+1):
    if max>h_list[n-1]-h_list[n-k]:
        max=h_list[n-1]-h_list[n-k]
print(max)

Submission status: Wrong Answer

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

err_list = [h_list[i+K-1] - h_list[i] for i in range(N-K+1)]
print(max(err_list))

Submission status: Wrong Answer

Solution code:
def run(N, K, h):
    h = sorted(h)
    diff = [h[i+1] - h[i] for i in range(N-1)]
    diff_all = max(h) - min(h)
    r_diff_sum = [0]
    for d in diff:
        r_diff_sum.append(r_diff_sum[-1] + d)
    l_diff_sum = [0]
    for d in reversed(diff):
        l_diff_sum.append(l_diff_sum[-1] + d)
    print(r_diff_sum)
    print(l_diff_sum)
    ret = diff_all
    # 右から何個を除外するか
    for i in range(N - K + 1):
        ret = min(ret, diff_all - r_diff_sum[i] - l_diff_sum[N - K - i])
    return ret


def main():
    N, K = map(int, input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    print(run(N, K, h))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def run(N, K, h):
    h = sorted(h)
    diff = [h[i+1] - h[i] for i in range(N-1)]
    diff_all = max(h) - min(h)
    r_diff_sum = [0]
    for d in diff:
        r_diff_sum.append(r_diff_sum[-1] + d)
    l_diff_sum = [0]
    for d in sorted(diff, reverse=True):
        l_diff_sum.append(l_diff_sum[-1] + d)
    ret = diff_all
    # 右から何個を除外するか
    for i in range(N - K + 1):
        ret = min(ret, diff_all - r_diff_sum[i] - l_diff_sum[N - K - i])
    return ret


def main():
    N, K = map(int, input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    print(run(N, K, h))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def run(N, K, h):
    h = sorted(h)
    diff = [h[i+1] - h[i] for i in range(N-1)]
    diff_all = max(h) - min(h)
    r_diff_sum = [0]
    for d in diff:
        r_diff_sum.append(r_diff_sum[0] + d)
    l_diff_sum = [0]
    for d in sorted(diff, reverse=True):
        l_diff_sum.append(l_diff_sum[0] + d)
    ret = diff_all
    # 右から何個を除外するか
    for i in range(N - K + 1):
        ret = min(ret, diff_all - r_diff_sum[i] - l_diff_sum[N - K - i])
    return ret


def main():
    N, K = map(int, input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    print(run(N, K, h))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def run(N, K, h):
    h = sorted(h)
    diff = [h[i+1] - h[i] for i in range(N-1)]
    diff_all = max(h) - min(h)
    r_diff_sum = [0]
    for d in diff:
        r_diff_sum.append(r_diff_sum[0] + d)
    l_diff_sum = [0]
    for d in sorted(diff, reverse=True):
        l_diff_sum.append(l_diff_sum[0] + d)
    ret = diff_all
    # 右から何個を除外するか
    for i in range(N - K):
        ret = min(ret, diff_all - r_diff_sum[i] - l_diff_sum[N - K - i])
    return ret


def main():
    N, K = map(int, input().split())
    h = []
    for _ in range(N):
        h.append(int(input()))
    print(run(N, K, h))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
start = list(map(int, input().split()))
n,k = start[0], start[1]

lst = [int(input()) for i in range(n)]

lst.sort()
lst.reverse()

diff_lst = list()
for i in range(n-k+1):
    diff_lst.append(lst[i] - lst[i+k-1])
min(diff_lst)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
h=list(int(input()) for _ in range(n))
a=[]
h.sort(reverse=True)
for i in range(0,n-k+1,2):
    a.append(h[i]-h[i+2])
print(min(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n_k = list(map(int, input().split()))
n, k = n_k[0], n_k[1]
tree_list = []
for i in range(n):
    tree_list.append(int(input()))
tree_list.sort()
min = 9999999
for i in range(n-k+1):
    if tree_list[i+k-1]-tree_list[i] < min:
        min = tree_list[i+k-1]-tree_list[i]

print(min)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
arr=[int(input()) for i in range(N)]
arr=sorted(arr)
min_dis=10000
for i in range(len(arr)-K+1):
  temp=arr[i+K-1]-arr[i]
  min_dis=min(min_dis,temp)
print(min_dis)

Submission status: Wrong Answer

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

h.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
L = sorted([int(input()) for i in range(N)])
ans = float("inf")
for i in range(N-K) :
    ans = min(ans, L[i+K-1]-L[i])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def func(L):
    return sum([L[i+1] - L[i] for i in range(len(L)-1)])


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)]

print(0)

Submission status: Wrong Answer

Solution code:
def func(L):
    return sum([L[i+1] - L[i] for i in range(len(L)-1)])


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

H = []

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

H.sort()

print(min(H)) # Sort time test

Submission status: Wrong Answer

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

for idx, x in enumerate(lst[:-K+1], 0):
    tmp = lst[idx + K-1] - x
    if tmp < ret:
        ret = tmp
print(ret)

Submission status: Wrong Answer

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

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

x.sort(reverse=True)

print(x[0]-x[k])

Submission status: Wrong Answer

Solution code:
tree_num,choice_num = map(int,input().split())
tree_list = sorted([int(input()) for _ in range(tree_num)])
mi = 99999
for idx in range(tree_num-choice_num+1):
    cand = tree_list[idx+choice_num-1] - tree_list[idx]
    if cand < mi:
        mi = cand
print(mi)

Submission status: Wrong Answer

Solution code:
tree_num,choice_num = map(int,input().split())
tree_list = sorted([int(input()) for _ in range(tree_num)])
mi = 99999
for idx in range(tree_num-choice_num):
    cand = tree_list[idx+choice_num-1] - tree_list[idx]
    if cand < mi:
        mi = cand
print(mi)

Submission status: Wrong Answer

Solution code:
tree_num,choice_num = map(int,input().split())
tree_list = sorted([int(input()) for _ in range(tree_num)])
mi = 99999
for idx in range(tree_num-choice_num):
    cand = tree_list[idx+choice_num-1] - tree_list[idx]
    if cand < mi:
        mi = cand
print(cand)

Submission status: Wrong Answer

Solution code:
import sys
N,K = list(map(int, input().strip().split(' ')))
trees = [int(line.strip()) for line in sys.stdin]
trees = sorted(trees)
print(min([trees[i+(N-1)]-trees[i] for i in range(len(trees)-(N-1))]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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):
    ans = min(ans, (h[j+k-1]-h[j]))
print(ans)

Submission status: Wrong Answer

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):
    ans = min(ans, (h[j+k]-h[j]))
print(ans)

Submission status: Wrong Answer

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

print(min(max(h[j:j+k])-min(h[j:j+k])) for j in range(n-k+1))

Submission status: Wrong Answer

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+n%k
for i in range(num):
  dist.append(trees[i+k-1]-trees[i])
  
dist=sorted(dist)

print(int(dist[0]))

Submission status: Wrong Answer

Solution code:
N ,K = map(int, input().split())
a = [int(input()) for i in range(N)]
a = sorted(a)
res = float('inf')

for i in range(N - K):
  if res > a[i+K-1] - a[i]:
    res = a[i+K-1] - a[i]

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import heapq
 
N, k = [int(s) for s in input().split(' ')]
 
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[0] - trees[-1])

Submission status: Wrong Answer

Solution code:
import heapq
 
N, k = [int(s) for s in input().split(' ')]
 
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: Wrong Answer

Solution code:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = hs[K - 1] - hs[0]
    while True:
        if i + K > N - 1:
            break
        if hs[i + 1] - hs[i] > hs[K + i] - hs[K + i - 1]:
            r = hs[K + i] - hs[i + 1]
        i += 1
    print(r)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    mins = hs[:K]
    maxs = hs[-K:]
    print(min(max(mins) - min(mins), max(maxs) - min(maxs)))


if __name__ == '__main__':
    main()