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:
from sys import stdin
N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) for _ in range(N)]
h.sort()
diff = [h[k + K] - h[k] for k in range(N - K)]
diff.sort()
print(diff[0])

Submission status: Wrong Answer

Solution code:
from sys import stdin
N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) for _ in range(N)]
h.sort()
diff = [h[k + 2] - h[k] for k in range(N - 2)]
diff.sort()
print(diff[0])

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) for _ in range(N)]
h.sort()


def solve(a):
    if len(a) >= 6:
        center = math.floor(len(a) / 2)
        prev = a[:center]
        post = a[center:]
        return min(solve(prev), solve(post), a[center] - a[center - 2], a[center + 1] - a[center - 1])
    else:
        diff = [a[k + 2] - a[k] for k in range(len(a) - 2)]
        diff.sort()
        return diff[0]


print(solve(h))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
N, K = [int(x) for x in stdin.readline().rstrip().split()]
h = [int(stdin.readline().rstrip()) for _ in range(N)]
h.sort()


def solve(a):
    if len(a) >= 6:
        center = math.floor(len(a) / 2)
        prev = a[:center]
        post = a[center:]
        return min(solve(prev), solve(post))
    else:
        diff = [a[k + 2] - a[k] for k in range(len(a) - 2)]
        diff.sort()
        return diff[0]


print(solve(h))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 10**9
for i in range(n-k+1):
    num = h[i+k-i] - h[i]
    if num < ans:
        ans = num

print(ans)

Submission status: Wrong Answer

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

for i in range(n):
    count[i-1] = h[i] - h[i-1]

ans = 10**9
for i in range(k):
    num = sum(count[i:i+k-1])
    if num < ans:
        ans = num

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 = 99999
for i in range(0,N+1-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 = [int(input()) for i in range(N)]

h.sort()
h_diff = []
for i in range(0,N-1):
    h_diff.append(h[i+1] - h[i])

h_diff.sort()

print(sum(h_diff[:K-1]))

Submission status: Wrong Answer

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

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

for i in range(N-2) : 
    tmp = [h[i], h[i+1], h[i+2]]
    d = h[i+2] -h[i]
    min_d = min(d, min_d)
    
print(min_d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from __future__ import print_function
import numpy as np
import sys
import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy,functools
# from itertools import accumulate
# from collections import deque
import random

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

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

h.sort()
# eprint(h)
ll = np.zeros(n-k+1,dtype=int)
for i in range(n-(k-1)):
    ll[i] = abs(h[i] - h[i+(k-1)])
    
print(ll)
print(ll.min())
    
    
# sorted(h)
# np.sort(h)
# h.sort()
# print(h)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 14 20:13:03 2019

@author: avina
"""

n,k = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort()
i = 0
mins = 1e6
while i < n - k +1:
  mins = min(mins, l[i+k-1] - l[i])
  i+=1
print(mins)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 14 20:13:03 2019

@author: avina
"""

n,k = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort(reverse=True)
i = 0
mins = 1e6
while i < n - k +1:
  mins = min(mins, l[i+k-1] - l[i])
  i+=1
print(mins)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 14 20:13:03 2019

@author: avina
"""

n,k = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort(reverse=True)
i = 0
mins = 1e6
while i + k -1< n:
  mins = min(mins, l[i] - l[i+k-1])
  i+=1
print(mins)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 14 20:13:03 2019

@author: avina
"""

n,k = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
l.sort()
i = 0
mins = 1e6
while i + k -1< n:
  mins = min(mins, l[i+k-1] - l[i])
  i+=1
print(mins)

Submission status: Wrong Answer

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

h.sort()

ans = 10 ** 9
for i in range(N-2):
    tmp = h[i+2] - h[i]
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k=map(int,input().split())
l=[]
for i in range(n):
    t=int(input())
    l.append(t)
l.sort()
#print(l)
ans=100000000000
for i in range(len(l)-2):
    if(ans>l[i+2]-l[i]):
        ans=l[i+2]-l[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
p=[int(input()) for i in range(n)]
p.sort()
q=9999999999999
for i in range (n-k+1):
    print(p[k+i-1])
    print(p[i])
    if p[k+i-1]-p[i]<q:
        q=p[k+i-1]-p[i]
print(q)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
p=[int(input()) for i in range(n)]
p.sort()
q=999999
for i in range (n-k+1):
    if p[k+i-1]-p[i]<q:
        q=p[k+i-1]-p[i]
print(q)

Submission status: Wrong Answer

Solution code:
import random

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

tmp = random.sample(H,K)
scr = max(tmp) - min(tmp)
print(scr)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
H = [int(input()) for i in range(N)]
H.sort()
result = 100000000
for i,h in enumerate(H):
    if K + i - 1 > N -1:
        break
    if result > H[K + i - 1] - H[i]:
        result = H[K + i - 1] - H[i]
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K= list(map(int,input().split(" ")))
nums = []
for i in range(N):
    nums.append(int(input()))
nums.sort()
r = 0
l = 0
ans = []
for i,num in enumerate(nums[:-(K-1)]):
    r = nums[i+K-1]
    l = num
    print("l:",l,"//r:",r)
    ans.append(r-l)
print(min(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

r=input().split()
n=int(r[0])
k=int(r[1])
h=sorted([int(x.strip()) for x in sys.stdin.readlines()])

d=sorted([h[x+1]-h[x] for x in range(n-1)])
            
print(sum(d[:k-1]))

Submission status: Wrong Answer

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

h.sort()

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

print(min_d)

Submission status: Wrong Answer

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

h.sort()

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

print(min_d)

Submission status: Wrong Answer

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

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

main()

Submission status: Wrong Answer

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

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

main()

Submission status: Wrong Answer

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

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

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

tree_list.sort(reverse=True)

for i in range(len(tree_list)-K+1):
    print(tree_list[0+i],tree_list[K+i-1])
    new_tree_list.append(tree_list[0+i]-tree_list[K+i-1])
new_tree_list.sort()
print(new_tree_list[0])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=[]
for i in range(N) :
  L.append(int(input()))
M=[]
for x,y in zip(L,L[K-1:]) :
  M.append(y-x)
print(min(M))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(min_height)

Submission status: Wrong Answer

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

c.sort()

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

print(temp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import heapq

mINF=-10**10

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

arr=[mINF]*N
for z in range(N):
  heapq.heapreplace(arr,int(input()))

arr2=[0]*K
for z in range(K):
  arr2[z]=heapq.heappop(arr)

r=arr2[-1]-arr2[0]

z=0
try:
  while True:
    arr2[z]=heapq.heappop(arr)
    if z==0:
      zz=K-1
    else:
      zz=z+1

    rb=arr2[z]-arr2[zz]
    if rb<r:
      r=rb

    z+=1
    if z==K:
      z=0

except Exception:
  pass

print(r)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
List=[]
for i in range(K):
  h=int(input())
  List.append(h)
m=max(List)
n=min(List)
d=m-n
for i in range(N-K):
  hh=int(input())
  List_m=[hh]
  List_m.extend(List)
  List_m.remove(m)
  m_m=max(List_m)
  n_m=min(List_m)
  d_m=m_m-n_m
   
  List_n=[hh]
  List_n.extend(List)
  List_n.remove(n)
  m_n=max(List_n)
  n_n=min(List_n)
  d_n=m_n-n_n
  
  if d_m<d:
    List=List_m
    m=max(List)
    n=min(List)
    d=d_m
  if d_n<d:
    List=List_n
    m=max(List)
    n=min(List)
    d=d_n  
print(d)

Submission status: Wrong Answer

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

h_list.sort(reverse = True)
diff = [h_list[i] - h_list[i+K-1] for i in range(N-K+1)]
min(diff)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import heapq


def solve(N: int, K: int, h: "List[int]"):
    minDiff = 10 ** 18
    for i in range(N - K):
        minDiff = min(minDiff,h[i+K] - h[i])
    print(minDiff)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    q = []
    heapq.heapify(q)
    for i in range(N):
        heapq.heappush(q,int(next(tokens)))
    solve(N, K, q)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

SORT_h = sorted(h)

MIN_h = 10**9
for i in range(N - K + 1):
    TEMP = h[i+K-1] - h[i]
    
    if TEMP < MIN_h:
        MIN_h = TEMP
        
print(MIN_h)

Submission status: Wrong Answer

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

SORT_h = sorted(h)

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

Submission status: Wrong Answer

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

SORT_h = sorted(h)

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

Submission status: Wrong Answer

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

ans = 10**10
for i in range(d, n):
	ans = min(ans, h[i] - h[i-d])

print(ans)

Submission status: Wrong Answer

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

h.sort()

print(h)

min_h = 1000000000

for i in range(K - 1, N):

    print([h[i], h[i - K + 1], h[i] - h[i - K + 1], min_h])
    min_h = min(min_h, h[i] - h[i - K + 1])

print(min_h)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
trees= [int(input()) for i in range(n)]
trees.sort()
min_dif=10**9+8
for j in range(k):
    if j+k-1 > n-1:
        break
    dif_trees = trees[j+k-1] -trees[j]
    if dif_trees <= min_dif:
        min_dif = dif_trees
print(min_dif)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
trees= [int(input()) for i in range(n)]
trees.sort()
min_dif=10**9
for j in range(k):
    if j+k-1 > n-1:
        break
    dif_trees = trees[j+k-1] -trees[j]
    if dif_trees <= min_dif:
        min_dif = dif_trees
print(min_dif)

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()
h_distance = [0]*(N-1)
for i in range(N-1):
    h_distance[i] = abs(h[i]-h[i+1])
h_distance.sort()
print(sum(h_distance[:K-1]))

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()
print(h[K-1] - h[0])

Submission status: Wrong Answer

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

diffs = []

for i in range(trees_num - illuminations_num + 1):
    diffs.append(trees[illuminations_num] - trees[i])

print(min(diffs))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
h_li = []
for i in range(n):
    h = int(input())
    h_li.append(h)
h_li.sort()
d = 10 ** 7
m = k - 1
while m < n:
    if d > (h_li[m] - h_li[m - k + 1]):
        d = h_li[m] - h_li[m - k + 1]
    m += 1
print(d)

Submission status: Wrong Answer

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

h = sorted(h)

ans = 10000000000
for i in range(N - K):
    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 = sorted(h)

ans = 10000000000
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,p=map(int,input().split())

item=[]
for i in range(n):
    b=int(input())
    item.append(b)
    
item.sort()
temp=[]

for i in range(n-p):
    a=item[i+p-1]-item[i]
    temp.append(a)
    
temp.sort()
print(temp[0])

Submission status: Wrong Answer

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

item=[]
for i in range(n):
    b=int(input())
    item.append(b)
    
item.sort()
temp=[]

for i in range(n-p):
    a=item[i+p]-item[i]
    temp.append(a)
    
temp.sort()
print(temp[0])

Submission status: Wrong Answer

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

item=[]
for i in range(n):
    p=int(input())
    item.append(p)
    
item.sort()
temp=[]
for i in range(n-1):
    a=item[i+1]-item[i]
    temp.append(a)
    
temp.sort()
print(temp[0])

Submission status: Wrong Answer

Solution code:
import collections
def c():
    n, k = map(int, input().split())
    h = []
    for i in range(n):
        h.append(int(input()))
    h.sort()
    d = collections.deque(maxlen = k)
    mins = []
    while len(d) < k:
        d.append(h.pop())
    while h:
        mins.append(d[k-1] - d[0])
        d.append(h.pop())
    mins.append(d[k-1] - d[0])
    print(min(mins))
c()

Submission status: Wrong Answer

Solution code:
import collections
n, k = map(int, input().split())
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
d = collections.deque(maxlen = k)
mins = []
while len(d) < k:
    d.append(h.pop())
while h:
    mins.append(max(d) - min(d))
    d.append(h.pop())
print(min(mins))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

h.sort()
length = []

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

length.sort()

ans = 0
for i in range(k-1):
    ans += length[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)
ans = 10**9
for i in range(N-K+1):
    if ans > h[i+K-1] - h[i]:
        ans = h[i+K-1] - h[i]
    print(i, ans)

print(ans)

Submission status: Wrong Answer

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

h = sorted(h)
ans = 10**5
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:
###C - Christmas Eve
n, k = map(int, input().split())
h = []
ans = 10*9+1
for i in range(n):
    h.append(int(input()))
h.sort()
for i in range(n-k+1):
    ans = min(h[k+i-1] - h[i], ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())  # 複数数値入力
li = list()
for n in range(N):
    li.append(int(input()))

li.sort()
sa = list()
last = 0
for height in li:
    if last == 0:
        last = height
        continue
    sa.append(height - last)
    last = height

sa.sort()

ans = 0
for i in range(K - 1):
    ans += sa[i]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(" "))
treehight = []
difference = []
for i in range(N):
    treehight.append(int(input()))
treehight.sort()
for j in range(N - 1):
    difference.append(treehight[j+1]-treehight[j])

print(min(difference))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,K = read_int()
h = [read_int()[0] for _ in range(N)]
h.sort()

def solve():
    ans = float('INF')
    for i in range(N - K):
        ans = min(ans, h[i + K - 1] - h[i])
    return ans

if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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 = 100000000
for i in range(0,n-k+1):
    diff = h[i+k-1] - h[i]
    ans = min(ans,diff)
print(ans)

Submission status: Wrong Answer

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

differences = sorted([trees[i] - trees[i-1] for i in range(1, trees_num)])
print(sum(differences[:illuminations_num - 1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import combinations

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

C = list(combinations(h,3))

ans = 10**9
for c in C:
    if max(c) - min(c) < ans:
        ans = max(c) - min(c)
        
print(ans)

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

hmax_hmin = []
for i in range(N-K+1):
  print(H[i+K-1],H[i])
  hmax_hmin.append(H[i+K-1] - H[i]) 
  
print(min(hmax_hmin))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

ans = min(h[:k][-1] - h[:k][0], h[::-1][:k][0] - h[::-1][:k][-1])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import scipy as sp

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

tree = sp.array([], dtype = "int")
for i in range(N):
    tree = sp.append(tree, int(input()))

#tree.sort()

print(int(min(tree[K-1:]-tree[:N-K+1])))

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

Submission status: 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
loop = min(K+1,N-K)
for i in range(loop+1):
    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
loop = min(K+1,N-K)
for i in range(loop):
    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
loop = min(K,N-K+1)
for i in range(loop):
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)

Submission status: 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
loop = min(K,N-K)
for i in range(loop):
    tmp = h[K+i-1] - h[0+i]
    if(tmp < ans):
        ans = tmp
print(ans)