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: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
h = sorted([int(input()) for x in range(n)])
d = 10 ** 9

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

    if d == 0:
        break

print(d)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

# check
min = min([max(t) - min(t) for t in list(combinations(h, K))])

print(min)

Submission status: Time Limit Exceeded

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

ans = float('inf')
for i in range(N-K+1):
  ans = min(ans, sum(diff_h[i:i+K-1]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h.sort()
m = 10**10
for i in range(n-k+1):
    a = max(h[i:i+k])
    b = min(h[i:i+k])
    if a - b < m:
        m = a - b

print(m)

Submission status: Time Limit Exceeded

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

print(min(map(lambda x: x[-1] - x[0], [H[i:i + K] for i in range(len(H)) if len(H[i:i + K]) == K])))

Submission status: Time Limit Exceeded

Solution code:
## C - Christmas Eve
import itertools
N, K = map(int, input().split())
rt = [max(x)-min(x) for x in list(itertools.combinations([int(input()) for i in range(N)], K))]
print(min(rt))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
ans=float('inf')
for i in itertools.permutations(h,k):
    tmp=max(i)-min(i)
    ans=min(ans,tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
import fractions
import functools
import copy

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

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

print(minimum)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
H = [INT() for i in range(N)]
H.sort()
ans = INF
for i in range(N-K+1):
    mi = H[i:i+K][0]
    ma = H[i:i+K][-1]
    tmp = ma - mi
    ans = min(tmp, ans)
print(ans)

Submission status: Time Limit Exceeded

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(0,len(h)-k+1):
  ans.append(max(h[i: i+k]) - min(h[i: i+k]))

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

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

hCombi = list(itertools.combinations(h, K))
hMax = list(map(max, hCombi))
hMin = list(map(min, hCombi))
print(min([hMax[i] - hMin[i] for i in range(len(hMax))]))

Submission status: Time Limit Exceeded

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

h.sort()
subs = []
for i in range(1,n):
    subs.append(h[i]-h[i-1])

minimum = 10**10
for i in range(len(subs)-1):
    minimum = min(minimum,sum(subs[i:k+i-1]))


print(minimum)

Submission status: Time Limit Exceeded

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

h.sort()
subs = []
for i in range(1,n):
    subs.append(h[i]-h[i-1])


sums = []

for i in range(len(subs)-1):
    sums.append(sum(subs[i:k+i-1]))


print(min(sums))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
score = float('inf')
for x in itertools.combinations(h, k):
    score = min(score, max(x) - min(x))
print(score)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
def score(a):
    return max(a) - min(a)

def dfs(i, a):
    if len(a) == k:
        return score(a)

    s = float('inf')
    for x, y in enumerate(h[i:]):
        a.append(y)
        s = min(s, dfs(i + x + 1, a))
        a.pop()
    return s
print(dfs(0, []))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = np.array(sorted(h))
diff = np.diff(h)
low_score = np.inf
for i in range(n - k + 1):
    mask = np.arange(i, i + k - 1)
    cost = diff[mask].sum()
    low_score = min(low_score, cost)
print(low_score)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]
h = np.array(sorted(h))
diff = np.diff(h)
low_score = np.inf
for i in range(n - k + 1):
    cost = diff[i: i + k - 1].sum()
    low_score = min(low_score, cost)
print(low_score)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, k = map(int, input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h = np.array(sorted(h))
diff = np.diff(h)
low_score = np.inf
for i in range(n - k + 1):
    cost = np.sum(diff[i: i + k - 1])
    low_score = min(low_score, cost)
print(low_score)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    *N, = map(int, open(0).read().split())
    h = N[2:]
    K = N[1]
    N = N[0]

    D = [[] for i in range(0, N)]
    for i in range(0, N):
        for j in range(0, N):
            D[i].append(abs(h[i] - h[j]))
        D[i].sort()
    S = []
    for i in range(0, N):
        S.append(sum(D[i][0:K]))

    print(min(S))


main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    *N, = map(int, open(0).read().split())
    h = N[2:]
    K = N[1]
    N = N[0]

    D = [[] for i in range(0, N)]
    for i in range(0, N):
        for j in range(0, N):
            if not i == j:
                D[i].append(abs(h[i] - h[j]))
        D[i].sort()
    S = []
    for i in range(0, N):
        S.append(sum(D[i][0:K - 1]))

    print(min(S))


main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    *N, = map(int, open(0).read().split())
    h = N[2:]
    K = N[1]
    N = N[0]

    D = [[] for i in range(0, N)]
    for i in range(0, N):
        for j in range(0, N):
            if not i == j:
                D[i].append(abs(h[i] - h[j]))
        D[i].sort()

    ans = max(h) - min(h)
    for i in range(0, N):
        tmp = sum(D[i][0:K - 1])
        if tmp < ans:
            ans = tmp

    print(ans)


main()

Submission status: Time Limit Exceeded

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

h = []
ans = 10**9

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

h.sort()

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


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
import math
import string

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    H = [ii() for _ in range(N)]
    H = sorted(H, reverse=True)
    cnt = len(H)-K+1
    ret = [0]*cnt
    for i in range(cnt):
        ret[i] = max(H[i:i+K]) - min(H[i:i+K])
    print(min(ret))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

H.sort()

d = max(H)
for i in range(N-K+1):
    d = min(max(H[i:i+K]) - min(H[i:i+K]), d)
print(d)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tup(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,K=pin()
    H=[int(input()) for i in range(N)]
    H.sort()
    ans=1000000000
    for i in range(N-K+1):
        temp=H[i:i+K]
        xam=max(temp)
        nim=min(temp)
        ans=min(xam-nim,ans)
    print(ans)
#%%submit!
resolve()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, K = map(int, input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    mi = 10**9
    for i in range(N - K + 1):
        h_ = H[i:i+K]
        diff = h_[-1] - h_[0]
        if diff < mi:
            mi = diff
    print(mi)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, K = map(int, input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    mi = 10**9
    for i in range(N - K + 1):
        h_ = H[i:i+K]
        diff = max(h_) - min(h_)
        if diff < mi:
            mi = diff
    print(mi)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, K = map(int, input().split())
    H = [int(input()) for i in range(N)]
    H.sort()
    mi = 10**9
    for i in range(N - K + 1):
        h_ = H[i:i+K]
        if max(h_) - min(h_) < mi:
            mi = max(h_) - min(h_)
    print(mi)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools 
n,k=map(int,input().split())
H=[int(input()) for i in range(n)]
Hn=list(itertools.combinations(H,k))
print(min([max(list(i))-min(list(i)) for i in Hn]))

Submission status: Time Limit Exceeded

Solution code:
n,k=list(map(int,input().split()))
h=[]
d=[0]*(n-1)
ans=10**9

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

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

print(ans)

Submission status: Time Limit Exceeded

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

def bubble_sort(arr):
    change = True
    while change:
        change = False
        for i in range(len(arr) - 1):
            if arr[i] > arr[i + 1]:
                arr[i], arr[i + 1] = arr[i + 1], arr[i]
                change = True
    return arr

B=[]
for i in range(N):
    B.append(A[i][0])
    
C=bubble_sort(B)

ans = C[K-1] - C[0]
for i in range(N-K+1):
    if ans > C[i+K-1] -C[i]:
        ans = C[i+K-1] -C[i]
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
hlist = [int(input())for i in range(n)]
hlist.sort()
dlist = [0]*(n-1)

ans = float('inf')
i = 0
while i+(k-1) <= n-1:
  ans = min(ans, sum((hlist[j+1]-hlist[j]for j in range(i,k-1+i))))
  i += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
hlist = [int(input())for i in range(n)]
hlist.sort()
dlist = [0]*(n-1)

for i in range(n-1):
  dlist[i] = hlist[i+1]-hlist[i]

ans = float('inf')
i = 0
while i+(k-1) <= n-1:
  ans = min(ans, sum((dlist[j]for j in range(i,k-1+i))))
  i += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

res = 10**10
for i in combinations(h,K):
    tmp = max(i) - min(i)
    res = min(res,tmp)
print(res)

Submission status: Time Limit Exceeded

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

mindiff = float('inf')
for i in range(N-K+1):
    selected = sor[i:i+K] 
    diff = selected[-1] - selected[0]
    if diff <= mindiff:
        mindiff = diff
print(mindiff)

Submission status: Time Limit Exceeded

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

mindiff = float('inf')
for i in range(N-K+1):
    selected = sor[i:i+K]
    diff = selected[-1] - selected[0]
    if diff <= mindiff:
        mindiff = diff
print(mindiff)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, K = map(int, input().split())
    h = [0]*N
    for i in range(N):
        h[i] = int(input())
    hh = sorted(h)

    ans = 10**10
    for i in range(N-K+1):
        ans = min(ans, max(hh[i:i+K])-min(hh[i:i+K]))
    print(ans)
    
main()

Submission status: Time Limit Exceeded

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

ans = 10**10
for i in range(N-K+1):
    max_h = max(hh[i:i+K])
    min_h = min(hh[i:i+K])
    ans = min(ans, max_h-min_h)
print(ans)

Submission status: Time Limit Exceeded

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

tree = []
for _ in range(n):
    tree.append(int(input()))

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

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
h = []
ans = 10**10

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

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

Submission status: Time Limit Exceeded

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

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

print(seed)

Submission status: Time Limit Exceeded

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

seed = h[0]
for i in range(0,n-k+1):
    high = h[i:i+k][0] - h[i:i+k][k-1]
    if seed > high:
        seed = high

print(seed)

Submission status: Time Limit Exceeded

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

seed = max(h)
for i in range(0,n-k+1):
    high = max(h[i:i+k]) - min(h[i:i+k])
    if seed > high:
        seed = high

print(seed)

Submission status: Time Limit Exceeded

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

h_n = []
for i in range(0,n):
    l = h[i:i+k]
    if len(l) == k:
        h_n.append(l)

seed = max(h)
for i in range(0,len(h_n)):
    high = max(h_n[i]) - min(h_n[i])
    if seed > high:
        seed = high

print(seed)

Submission status: Time Limit Exceeded

Solution code:

import numpy as np

def submit():
    n, k = map(int, input().split())
    hlist = np.array([int(input()) for _ in range(n)])

    hlist.sort()
    min_diff = float('inf')
    for i in range(0, n - k + 1):
        choices = hlist[i : i + k]
        maxh = np.max(choices)
        minh = np.min(choices)

        if maxh - minh < min_diff:
            min_diff = maxh - minh

    print(min_diff)
        

if __name__ == "__main__":
    submit()

Submission status: Time Limit Exceeded

Solution code:

def submit():
    n, k = map(int, input().split())
    hlist = [int(input()) for _ in range(n)]

    hlist.sort()
    min_diff = float('inf')
    for i in range(0, n - k + 1):
        choices = hlist[i : i + k]
        maxh = max(choices)
        minh = min(choices)

        if maxh - minh < min_diff:
            min_diff = maxh - minh

    print(min_diff)
        

if __name__ == "__main__":
    submit()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

ans = float('inf')
for i in range(N-K+1):
    m = min(h[i:i+K])
    M = max(h[i:i+K])
    ans = min(ans,M-m)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

sub = 10 ** 9
for i in range(n-k+1):
    hmax = max(h[i:i+k])
    hmin = min(h[i:i+k])
    sub = min(hmax - hmin, sub)
print(sub)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)])
a=10**9
for i in range(n-k+1):
    h1=h[i:i+k]
    a1=h1[-1]-h1[0]
    a=min(a,a1)
print(a)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)])
a=10**9
for i in range(n-k+1):
  h1=h[i:i+k]
  a1=max(h1)-min(h1)
  a=min(a,a1)
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
l = [int(input()) for _ in range(n)]
l = sorted(l)
#print(l)
x = []
for i in range(n-1):
    x.append(l[i+1] - l[i])
x  = sorted(x)
#print(x)

z = []
for j in range(n-k):
    z.append(sum(x[j:j+k-1]))
print(min(z))

Submission status: Time Limit Exceeded

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

for i in range(n-k+1):
    x.append(max(l[i:i+k]) - min(l[i:i+k]))

print(min(x))

Submission status: Time Limit Exceeded

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

from itertools import combinations
x = list(combinations(l, k))

y = []
for i in range(len(x)):
    y.append(max(x[i]) - min(x[i]))

print(min(y))

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n,k = map(int,input().split())
h = sorted([int(input()) for _ in range(n)])
x = deque(h[:k])
mn = max(x) - min(x)

for i in range(k,n):
    x.append(h[i])
    x.popleft()
    mn = min(mn,max(x) - min(x))
print(mn)

Submission status: Time Limit Exceeded

Solution code:
S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(input()))

def calculate(n,k,arr):
    arr.sort()
    drr = []
    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])

    i = 0
    sums = []
    while (i + k-1) <= len(drr):
        tmp = sum(drr[i:i + k-1])
        sums.append(tmp)
        i = i + 1
    print(min(sums))

calculate(n,k,arr)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N,K = map(int,input().split())
h_list = []
result = []
for i in range(N):
    bisect.insort(h_list,int(input()))
print(min([h_list[i+K-1]-h_list[i] for i in range(N-K+1)]))

Submission status: Time Limit Exceeded

Solution code:
import bisect
N,K = map(int,input().split())
h_list = []
result = []
for i in range(N):
    bisect.insort(h_list,int(input()))
for i in range(N-K+1):
    result.append(h_list[i+K-1]-h_list[i])
print(min(result))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k, *a = map(int, open(0).read().split())
a.sort()
mn = 1e18
for i in range(n-k+1):
    tmp = 0
    for j in range(k-1): tmp+=abs(a[i+j]-a[i+j+1])
    mn = min(mn, tmp)
print(mn)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

h.sort()
ans = 10**9
for i in range(0, n-k+1):
   hmax = max(h[i:i+k])
   hmin = min(h[i:i+k])
   if (hmax - hmin < ans):
      ans = hmax - hmin

print(ans)

Submission status: Time Limit Exceeded

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

h.sort()
ans = 10**9
for i in range(0, n-k+1):
   x = h[i:i+k]
   if (max(x) - min(x) < ans):
      ans = max(x) - min(x)

print(ans)

Submission status: Time Limit Exceeded

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

h = sorted(h)
ans = []

for i in range(len(h)-K+1):
  a = h[i:i+K]
  ans.append(max(a)-min(a))
  if ans[-1] == 0:
    print(0)
    exit()

print(min(ans))

Submission status: Time Limit Exceeded

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

h = sorted(h)
ans = []

for i in range(len(h)-K+1):
  a = h[i:i+K]
  ans.append(max(a)-min(a))

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N, K = [int(n) for n in input().split()]
h = [int(input()) for _ in range(N)]
ans = 10**9+1
for c in combinations(h, K):
    mc = max(c)-min(c)
    if ans > mc:
      ans = mc
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N, K = [int(n) for n in input().split()]
h = sorted([int(input()) for _ in range(N)])
ans = []
for c in combinations(h, K):
    ans.append(c[K-1]-c[0])
print(min(ans))

Submission status: Time Limit Exceeded

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

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

print(min_diff)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    N, K = list(map(int,input().split()))
    h = [0] * N
    for i in range(N):
        h[i] = int(input())

    h = sorted(h)

    abs_p = []
    for j in range(0,N-1):
        abs_p.append(abs(h[j]-h[j+1]))

    abon = []
    for k in range(0,N-K+1):
        abon.append(sum(abs_p[k:k+K-1]))

    print(min(abon))

main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
hs = sorted([int(input()) for _ in range(N)])[::-1]
 
min_diff = 10 ** 10
for i in range(0, N-K+1):
    tmp = hs[i:i+K]
    min_diff = min(min_diff, tmp[0] - tmp[-1])
 
print(min_diff)

Submission status: Time Limit Exceeded

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

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

print(min_diff)

Submission status: Time Limit Exceeded

Solution code:

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

ans = float("inf")
for i in range(N-K+1):
  t = H[i:i+K]
  ans = min(ans, t[K-1]-t[0])
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = float("inf")
for i in range(N-K+1):
  t = H[i:i+K]
  ans = min(ans, max(t)-min(t))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N, K = map(int, input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
H.sort()
L = []
for i in range(N-K+1):
    tmp = H[i:i+K]
    L.append(abs(tmp[0] - tmp[-1]))
print(min(L))

Submission status: Time Limit Exceeded

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

hl_s = sorted(hl, reverse=True)
delta = 10*10**9

if n > 1000:
    while n < 1000:
        if hl_s[n//2] < (hl_s[0] + hl_s[n-1])//2:
            hl_s = hl_s[:n//2]
        else:
            hl_s = hl_s[n//2:]
        n = len(hl_s)

for i in range(len(hl_s)-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

Submission status: Time Limit Exceeded

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

hl_s = sorted(hl, reverse=True)
delta = 10*10**9

for i in range(n-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

Submission status: Time Limit Exceeded

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

h.sort()


divide=[]
for i in range(N-K+1):
    divide.append(max(h[i:i+K])-min(h[i:i+K]))

print(min(divide))

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	#print(name)
	sys.stdin = open(name)

input = sys.stdin.readline

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

#print(l)

l = sorted(l)

#print(l)

ansmin = float("inf")
for i in range(n-k+1):
	if ansmin > max(l[i:i+k])-min(l[i:i+k]):
		ansmin = max(l[i:i+k])-min(l[i:i+k])

print(ansmin)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
H = sorted([int(input()) for _ in range(n)])
ans = float("INF")
for i in range(0,n - k + 1):
  selected = H[i:k + i]
  ans = min(ans,max(selected) - min(selected))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
N, K = [int(x) for x in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
h.sort()
i = 0
ans = []
while N - i >= K:
    temp = h[i:i+K]
    ans.append(temp[-1] - temp[0])
    i += 1
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
import numpy as np
N, K = [int(x) for x in input().split()]
h = []
for _ in range(N):
    h.append(int(input()))
h.sort()
i = 0
ans = []
while N - i >= K:
    temp = h[i:i+K]
    ans.append(temp[-1] - temp[0])
    i += 1
print(min(ans))