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 = list(map(int, input().split()))
h = [int(input()) for i in range(n)]

h.sort()

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

Submission status: Wrong Answer

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

h.sort()
k //= 2
ans = 1e+12
for i in range(k, n-k):
  ans = min(ans, h[i+k] - h[i-k])
  
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=sorted(h)
print(min(h_sort[j+k-1]-h[j] for j in range(n-k+1)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


ans = 10 ** 9
for i in range(n - k):
    tmp = 0
    b = -1
    for j in range(i, k):
        if b == -1:
            b = a[j]
            continue
        tmp += b - a[j]

    if tmp <= ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)


def input():
    return sys.stdin.readline()[:-1]


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

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)


def input():
    return sys.stdin.readline()[:-1]


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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

h.sort()

h_front_k = h[:k]
max_ = max(h_front_k)
min_ = min(h_front_k)
ans = max(h_front_k) - min(h_front_k)

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

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,k=map(int, input().split())
    l=[int(input()) for i in range(n)]
    l.sort()
    ans=10*18
    for i in range(0,n-k+1):
        ans=min(ans,l[i+k-1]-l[i])
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)


def input():
    return sys.stdin.readline()[:-1]


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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

h.sort()

h1 = h[:k]
h2 = h[::-1][:k]

print(min(max(h1) - min(h1), max(h2) - min(h2)))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
h = [int(input()) for _ in range(N)]
h.sort()
ans = 1e10
for i in range(N-K+1):
    tmp = h[i:i+K]
    print(tmp)
    ans = min(ans,max(tmp)-min(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()

ans = float("inf")

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

print(ans)

Submission status: Wrong Answer

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

h.sort()

ans = float("inf")

for n in range(N-2):
    ans = min(ans, h[n+2] - h[n])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
List = []
for i in range(N):
  tmp = int(input())
  List.append(tmp)
List = sorted(List)[::-1]
print(str(List[K-1] - List[0]))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
input = sys.stdin.readline


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N, K = readnums()
    h = np.sort(np.array([readint() for _ in range(N)]))
    print(min(h[K - 1] - h[0], h[-1] - h[-K]))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li = []
lia = [0]*(n-2)
for i in range(n):
    li.append(int(input()))
li.sort()
for i in range(n-2):
    lia[i] += li[i+2] - li[i]
print(min(lia))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    h = []
    for i in range(n):
        h.append(int(input()))
    h = sorted(h)
    ans = 10**5
    for i in range(n-k+1):
        test = h[i+k-1]-h[i]
        ans = min(test, ans)
    print(ans)
resolve()

Submission status: Wrong Answer

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

ans=1000000000
h.sort()
for i in range(n-k):
    tmp=abs(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=[]
for _ in range(n):
    h.append(int(input()))

ans=1000000000
h.sort()
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:
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()
minimum = 10**9+1
subs = []
for i in range(1,n):
    subs.append(h[i]-h[i-1])

subs.sort()
print(sum(subs[:k-1]))

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

minDistance = 10 ** 9
for i in range(0, N - K):
    if h[i + K] - h[i] < minDistance:
        minDistance = h[i + K] - h[i]

print(minDistance)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

min = 10**100
for i in range(n-k):
    if min > h[k+i-1]-h[i]:
        min = h[k+i-1]-h[i]

print(min)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, K =mi()
A = [ii() for _ in range(N)]

tmp = float('inf')
for i in range(N-K+1):
    if A[i+K-1] - A[i] < tmp:
        tmp = A[i+K-1] - A[i]
print(tmp)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, K =mi()
A = [ii() for _ in range(N)]

tmp = float('inf')
for i in range(N-K+1):
    if A[i+K-1] - A[i] < tmp:
        tmp = A[i+K-1] - A[0]
print(tmp)

Submission status: Wrong Answer

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

h.sort()

ans = 100000000

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 i in range(n)]

h.sort()

if h[k-1]-h[0] <= h[-1]-h[-k]:
  print(h[k-1]-h[0])
else:
  print(h[-1]-h[-k])

Submission status: Wrong Answer

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

h.sort()

print(h)
print(h[k-1],h[0],h[-1],h[-k])

if h[k-1]-h[0] <= h[-1]-h[-k]:
  print(h[k-1]-h[0])
else:
  print(h[-1]-h[-k])

Submission status: Wrong Answer

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

h.sort()

if h[k-1]-h[0] < h[-1]-h[-k]:
  print(h[k-1]-h[0])
else:
  print(h[-1]-h[-k])

Submission status: Wrong Answer

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

h.sort()

if h[2]-h[0] < h[-1]-h[-3]:
  print(h[2]-h[0])
else:
  print(h[-1]-h[-3])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

trees.sort()
min_val = trees[k-1] - trees[0]
for i in range(1, n - k + 1):
    min_val = min(abs(trees[i-k+1] - trees[i]), min_val)
    
print(min_val)

Submission status: Wrong Answer

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

trees.sort()
min_val = trees[k-1] - trees[0]
for i in range(1, n - k + 1):
    min_val = min(abs(trees[n - k + 1] - trees[i]), min_val)
    
print(min_val)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,K = map(int,input().split())
H_input = list()
H = deque(maxlen=K)
count = 1

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

# print(H_input)
for h in H_input:
    if count <= K:
        H.append(h)
        count += 1
        # print('追加')
    else:
        # print(H)
        if abs(H[0]-H[K-1]) >= abs(H[1]-h):
            H.append(h)
# print(H)
print(abs(H[0]-H[K-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque

N,K = map(int,input().split())
H_input = list()
H = deque(maxlen=K)
count = 0

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

for h in H_input:
    if count <= K:
        H.append(h)
    else:
        if abs(H[0]-H[K]) >= abs(H[1]-h):
            H.append(h)
# print(H)
print(abs(H[0]-H[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.sort()
min_val = 0
for i in range(len(trees) - 1):
    min_val = min(abs(trees[i+1] - trees[i]), min_val)
    
print(min_val)

Submission status: Wrong Answer

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

min_val = 0
for i in range(len(trees) - 1):
    min_val = min(abs(trees[i+1] - trees[i]), min_val)
    
print(min_val)

Submission status: Wrong Answer

Solution code:
# Problem C - Christmas Eve

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

# initialization
h = sorted(h)
min_ans = float('INF')
for i in range(N-K):
    tmp_max = h[i+K-1]
    tmp_min = h[i]
    min_ans = min(min_ans, tmp_max - tmp_min)

# output
print(min_ans)

Submission status: Wrong Answer

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

trees = []

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

trees.sort()
print(trees)
min_diff = 10**10
for j in range(n-k+1):
    diff = abs(trees[j] - trees[j+k-1])
    if diff < min_diff:
        min_diff = diff

print(min_diff)

Submission status: Wrong Answer

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

trees = []

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

trees.sort()

min_diff = 10**10
for j in range(n-k):
    diff = abs(trees[j] - trees[j+k-1])
    if diff < min_diff:
        min_diff = diff

print(min_diff)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    hi = int(input())
    h.append(hi)

h.sort()
tmp = 100000000
for i in range(0, n-k+1):
    tmp = min(tmp, abs(h[i]-h[i+k-1]))
print(tmp)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    n,k=map(int,input().split())
    h=sorted([int(input()) for i in range(n)])
    ans=10**20
    for i in range(n-2):
        ans=min(ans,h[i+2]-h[i])
    print(ans)


resolve()

Submission status: Wrong Answer

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)
    print(max(H[:K]) - min(H[:K]))


if __name__ == '__main__':
    main()

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

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

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 = cur = max(H[:K]) - min(H[:K])
for i in range(K, N):
    cur = cur - H[i - K] + H[i]
    ans = min(ans, cur)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
diff = [h[i+1] - h[i] for i in range(N-1)]
cost = 10*9
for i in range(len(diff)-((K-1)-1)):
    cost = min(sum(diff[i:i+K-1]), cost)

print(cost)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

# import os
# path = os.getcwd()
# path = os.path.join(path,"input.txt")
# sys.stdin = open(path)

input = sys.stdin.readline
N,K = map(int,input().split())
h = [int(input()) for _ in range(N)]
h.sort()
print(h)
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 sys
import numpy as np

# import os
# path = os.getcwd()
# path = os.path.join(path,"input.txt")
# sys.stdin = open(path)

input = sys.stdin.readline
N,K = map(int,input().split())
h = [int(input()) for _ in range(N)]
h.sort()
ans = float("inf")
for i in range(N-K):
    ans = min(ans,h[i+K-1]-h[i])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split(" "))
H = sorted([int(input()) for _ in range(N)])
Min = 10**10
print(H)
for i in range(K-1,len(H))[::-1]:
    Abs = H[i]-H[i-(K-1)]
    if Abs<Min:
        Min = Abs
print(Min)

Submission status: Wrong Answer

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

for i in range(K-1,len(H))[::-1]:
    Abs = H[i]-H[i-(K-1)]
    if Abs<Min:
        Min = Abs
print(Min)

Submission status: Wrong Answer

Solution code:
N , M = (map(int , input().split()))
tree = []
for i in range(N):
    tree.append(int(input()))
sort_tree = sorted(tree , reverse = True)
print(max(sort_tree[:M]) - min(sort_tree[:M]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    abs_tree = min(num_list[k-1]-num_list[0], num_list[n-1]-num_list[n-k])

    print(abs_tree)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
A = np.array([input() for _ in range(N)], dtype=np.int64)
A.sort()

ans = (A[-K:] - A[:K]).min()
print(ans)

Submission status: Wrong Answer

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]
        print(h_)
        if max(h_) - min(h_) < mi:
            mi = max(h_) - min(h_)
    print(mi)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h=[int(input()) for i in range(n)]
h.sort()
ans=float('inf')
for i in range(n-k):
  d=h[i+k-1]-h[i]
  if d<ans:
    ans=d
print(ans)

Submission status: Wrong Answer

Solution code:
def cChristmas(n, k, hl):


    hl.sort()
    ans = 10**10

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

    return ans

def main():
    n, k = map(int, input().split())
    hl = [int(input()) for i in range(n)]
    print(cChristmas(n, k, hl))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def cChristmas(n, k, hl):


    hl.sort()
    ans = 10**6

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

    return ans

def main():
    n, k = map(int, input().split())
    hl = [int(input()) for i in range(n)]
    print(cChristmas(n, k, hl))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

m = 100000000000
for i in range(k-1, n):
  g = max(h[i], h[i-1], h[i-2])
  t = min(h[i], h[i-1], h[i-2])
  if g - t < m:
    m = g - t

print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = (int(x) for x in input().split())
h = sorted([int(input()) for i in range(n)])
res = 10 ** 6
for i in range(n-k+1):
  res = min(res, h[i+k-1]-h[i])
print(res)

Submission status: Wrong Answer

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

for i in range(len(h)-k+1):
    if h[i+k-1] - h[i] < ans:
        ans = h[i+k-1] - h[i]
        if ans == 0:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []

    for i in range(n):
        tree_heights.append(int(input()))
    tree_heights.sort()
    #print(tree_heights)

    mn = 1000000000000000000000000000000
    for i in range(n-k+1):
        print(i,abs(tree_heights[i]-tree_heights[i+k-1]))
        mn = min(abs(tree_heights[i]-tree_heights[i+k-1]),mn)
    print(mn)

    #print(tree_heights)
    #print(tree_diffs)
    #print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []

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

    mn = 1000000000000000000000000000000
    for i in range(n-k+1):
        print(i,abs(tree_heights[i]-tree_heights[i+k-1]))
        mn = min(abs(tree_heights[i]-tree_heights[i+k-1]),mn)
    print(mn)

    #print(tree_heights)
    #print(tree_diffs)
    #print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []

    for i in range(n):
        tree_heights.append(int(input()))
    tree_heights.sort(reverse = True)
    tree_diffs = [0]
    first = tree_heights.pop()
    while tree_heights:
        second = tree_heights.pop()
        tree_diffs.append(abs(first-second))
        first = second

    tree_diffs.sort()

    #print(tree_heights)
    #print(tree_diffs)
    print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []
    heapq.heapify(tree_heights)

    for i in range(n):
        heapq.heappush(tree_heights,int(input()))
    #print(tree_heights)

    tree_diffs = [0]
    first = heapq.heappop(tree_heights)
    while tree_heights:
        second = heapq.heappop(tree_heights)
        tree_diffs.append(abs(first-second))
        first = second

    tree_diffs.sort()

    #print(tree_heights)
    #print(tree_diffs)
    print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []
    heapq.heapify(tree_heights)

    for i in range(n):
        heapq.heappush(tree_heights,int(input()))
    print(tree_heights)

    tree_diffs = [0]
    first = heapq.heappop(tree_heights)
    while tree_heights:
        second = heapq.heappop(tree_heights)
        tree_diffs.append(abs(first-second))
        first = second

    tree_diffs.sort()

    #print(tree_heights)
    #print(tree_diffs)
    print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

H.sort()

ans = H[2] - H[0]

for i in range(N - K + 1):
    tmp = H[i + K - 1] - H[i]
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

d = sorted(h)
res = 10**10
for i in range(N-K):
    res = min(res,d[i+K-1] - d[i])
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# int(input()) # 入力が1つ
n, k = map(int, input().split()) # 入力が複数
tree = []
for _ in range(n):
    h = int(input())
    tree.append(h)
tree.sort(reverse=True)

diff = 0
for i in range(1, k):
    t = tree[i - 1] - tree[i] 
    diff = max(diff, t)
print(diff)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
hn=[]
for i in range(n):
    hi=int(input())
    hn.append(hi)
hnms=sorted(hn,reverse=True)
hnss=sorted(hn)
hnmsk=[]
hnssk=[]
for i in range(k):
    hnmsk.append(hnms[i])
for i in range(k):
    hnssk.append(hnss[i])
a=max(hnmsk)-min(hnmsk)
b=max(hnssk)-min(hnssk)
if a<=b:
    print(a)
else:
    print(b)

Submission status: Wrong Answer

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

mins = 10000000000
for i in range(n - k+1):
  cost = max(h[i:i+k]) - min(h[i:i+k])
  if cost < mins:
    mins = cost
    
print(cost)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n-(k-1)):
  ls+=str(abs(h[i]-h[i+k-1]))

print(min(ls))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N, K = 5,3
h = [5,7,5,7,7]


sorted_h = sorted(h)


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

Submission status: Wrong Answer

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

sorted_h = sorted(h)


ans = 10**8
for i in range(0,N,2):
    test = sorted_h[i:i+3]
    if len(test) == 3:
        tmp = max(test) - min(test)
        if ans >= tmp:
            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()))

sorted_h = sorted(h)

front_s = sorted_h[K-1:]
rea_s = sorted_h[0:K]

if (max(front_s) - min(front_s)) <=  (max(rea_s) - min(rea_s)):
    print((max(front_s) - min(front_s)))
else:
    print((max(rea_s) - min(rea_s)))

Submission status: Wrong Answer

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

for i in range(n-2):
  ls+=str(abs(h[i]-h[i+2]))

print(min(ls))

Submission status: Wrong Answer

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

for i in range(n-1):
  ls+=str(abs(h[i]-h[i+1]))
  
ls.sort()
ans=0
for i in range(k-1):
  ans+=int(ls[i])
  
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
h=[]
for i in range(n):
    h1=int(input())
    h.append(h1)
h=sorted(h)
ans=10000000000
tmp=0
hs=list(set(h))
if len(h)==len(hs):
    for i in range(n-k):
        tmp=h[i+k-1]-h[i]
        ans=min(ans,tmp)
else:
    ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
h=[]
for i in range(n):
    h1=int(input())
    h.append(h1)
h=sorted(h)
ans=10000000000
tmp=0
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=int(input())
n,k=map(int,input().split())
#hl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
hl=[int(input()) for i in range(n)]

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

Submission status: Wrong Answer

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

h.sort()
g = [None] * (N - 1)
for i in range(N - 1):
    g[i] = h[i + 1] - h[i]

g.sort()
print(sum(g[:K - 1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k= map(int, input().split())
 
#print(n,k)
Ar = [0]*100001
 
for i in range(n):
#	print("i=",i)
	dat = int(input())
	Ar[i] = dat
#	print(Ar)
 
ans = 2000000000
 
for i in range(n-k+1):
	h = Ar[i]
	l = Ar[i]
#	print("Start = ",i)
	for j in range(i,i+k+1):
		if h < Ar[j]:
			h = Ar[j]
		if l > Ar[j]:
			l = Ar[j]
	if ans > h-l:
		ans = h-l
#	print("diff=",ans,h,l)
 
print (ans)