input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
n, k = list(map(int,input().split())) li = list(int(eval(input())) for i in range(n)) li.sort() num = 0 li2 = [] for i in range(len(li)-k+1): li2.append( max(li[i:i+k]) - min(li[i:i+k])) print((min(li2)))
n, k = list(map(int,input().split())) li = list(int(eval(input())) for i in range(n)) li.sort() num = 0 li2 = [] for i in range(len(li)-k+1): li2.append(li[i+k-1] - li[i]) print((min(li2)))
p03208
# 2019-11-11 18:30:26(JST) import sys # import collections # import math # from string import ascii_lowercase, ascii_uppercase, digits # from bisect import bisect_left as bi_l, bisect_right as bi_r # import itertools # from functools import reduce # import operator as op # from scipy.misc import comb # float # import numpy as np def main(): n, k, *h = (int(x) for x in sys.stdin.read().split()) h.sort() min_diff = h[-1] - h[0] # default value for i in range(n - k + 1): heights = h[i:i+k] diff = heights[-1] - heights[0] min_diff = min(min_diff, diff) print(min_diff) if __name__ == "__main__": main()
# 2019-11-11 18:30:26(JST) import sys # import collections # import math # from string import ascii_lowercase, ascii_uppercase, digits # from bisect import bisect_left as bi_l, bisect_right as bi_r # import itertools # from functools import reduce # import operator as op # from scipy.misc import comb # float # import numpy as np def main(): n, k, *h = (int(x) for x in sys.stdin.read().split()) h.sort() min_diff = h[-1] - h[0] # default value for i in range(n - k + 1): diff = h[i+k-1] - h[i] min_diff = min(min_diff, diff) print(min_diff) if __name__ == "__main__": main()
p03208
# 2019-11-11 18:30:26(JST) import sys # import collections # import math # from string import ascii_lowercase, ascii_uppercase, digits # from bisect import bisect_left as bi_l, bisect_right as bi_r # import itertools # from functools import reduce # import operator as op # from scipy.misc import comb # float # import numpy as np def main(): n, k, *h = (int(x) for x in sys.stdin.read().split()) h.sort() min_diff = h[-1] - h[0] # default value for i in range(n - k + 1): diff = h[i+k-1] - h[i] min_diff = min(min_diff, diff) print(min_diff) if __name__ == "__main__": main()
# 2019-11-11 18:30:26(JST) import sys # import collections # import math # from string import ascii_lowercase, ascii_uppercase, digits # from bisect import bisect_left as bi_l, bisect_right as bi_r # import itertools # from functools import reduce # import operator as op # from scipy.misc import comb # float # import numpy as np def main(): n, k, *h = (int(x) for x in sys.stdin.read().split()) h.sort() diffs = [] for i in range(n - k + 1): diffs.append(h[i+k-1] - h[i]) print((min(diffs))) if __name__ == "__main__": main()
p03208
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] h.sort() DEF_MIN = max(h)-min(h) for i in range(n-k+1): tmp = max(h[i:i+k])-min(h[i:i+k]) DEF_MIN = min(DEF_MIN, tmp) print(DEF_MIN)
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] h.sort() DEF_MIN = max(h)-min(h) for i in range(n-k+1): tmp = h[i+k-1]-h[i] DEF_MIN = min(DEF_MIN, tmp) print(DEF_MIN)
p03208
import bisect N,K = list(map(int,input().split())) h_list = [] result = [] for i in range(N): bisect.insort(h_list,int(eval(input()))) print((min([h_list[i+K-1]-h_list[i] for i in range(N-K+1)])))
N,K = list(map(int,input().split())) h_list = [int(eval(input())) for i in range(N)] h_list.sort() result = [] print((min([h_list[i+K-1]-h_list[i] for i in range(N-K+1)])))
p03208
N, K = list(map(int, input().split())) H = [int(eval(input())) for _ in range(N)] H = sorted(H, reverse=True) ans = 1e9 for i in range(N-K+1): h = H[i:i+K] dh = h[0]-h[-1] if dh < ans: ans = dh print(ans)
N, K = list(map(int, input().split())) H = [int(eval(input())) for _ in range(N)] H = sorted(H) ans = 1e9 for i in range(N-K+1): ans = min(ans, H[i+K-1]-H[i]) print(ans)
p03208
N, K = list(map(int, input().split())) h = [int(eval(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)
N, K = list(map(int, input().split())) h = [int(eval(input())) for _ in range(N)] h.sort() ans = float('inf') for i in range(N-K+1): ans = min(ans,h[i+K-1]-h[i]) print(ans)
p03208
N, K = list(map(int, input().split())) tree = [int(eval(input())) for i in range(N)] tree.sort() m = float("inf") for i in range(N-K+1): d = tree[i : i + K] dif = max(d) - min(d) m = dif if m > dif else m print((int(m)))
N, K = list(map(int, input().split())) t = [int(eval(input())) for i in range(N)] t.sort() m = 10 ** 9 for i in range(N-K+1): m = min(t[i+K-1] - t[i], m) print((int(m)))
p03208
n,k = input().split(' ') n = int(n) k = int(k) h = [] for i in range(n): h.append(int(eval(input()))) h.sort() mindiff = 1000000000 for i in range(n-k+1): t = h[i:i+k] diff = max(t) - min(t) if diff < mindiff: mindiff = diff print(mindiff)
n,k = input().split(' ') n = int(n) k = int(k) h = [] for i in range(n): h.append(int(eval(input()))) h.sort() diffs = [] for i in range(n-k+1): diffs.append(h[i+k-1] - h[i]) print((min(diffs)))
p03208
N,K = list(map(int,input().split())) H = [int(eval(input())) for _ in range(N)] H.sort() ans = 10**10 for i in range(N-K+1): ans = min(ans, H[i-1+K]-H[i]) print(ans)
# Python3 (3.4.3) import sys input = sys.stdin.readline # ------------------------------------------------------------- # function # ------------------------------------------------------------- # ------------------------------------------------------------- # main # ------------------------------------------------------------- N,K = list(map(int,input().split())) H = [int(eval(input())) for _ in range(N)] H.sort() ans = 10**10 for i in range(N-K+1): ans = min(ans, H[i+K-1]-H[i]) print(ans)
p03208
N, K = list(map(int, input().split())) h = [] for _ in range(N): h.append(int(eval(input()))) h.sort() iter = len(h) - K c_hmin = 99999999999999999999999999999999 for i in range(iter+1): cell = h[i: K + i] hmin = cell[-1] - cell[0] if c_hmin > hmin: c_hmin = hmin print(c_hmin)
N, K = list(map(int, input().split())) h = [] for _ in range(N): h.append(int(eval(input()))) h.sort() iter = len(h) - K c_hmin = 99999999999999999999999999999999 for i in range(iter+1): hmin = h[K+i-1] - h[i] if c_hmin > hmin: c_hmin = hmin print(c_hmin)
p03208
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=[h[x+1]-h[x] for x in range(n-1)] rui=[0]+[sum(d[:x+1]) for x in range(len(d))] dd=[rui[x+k-1]-rui[x] for x in range(n-k+1)] ans=min(dd) print(ans)
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=[h[x+k-1]-h[x] for x in range(n-k+1)] ans=min(d) print(ans)
p03208
## C - Christmas Eve import itertools N, K = list(map(int, input().split())) rt = [max(x)-min(x) for x in list(itertools.combinations([int(eval(input())) for i in range(N)], K))] print((min(rt)))
## C - Christmas Eve N, K = list(map(int, input().split())) H = sorted([int(eval(input())) for i in range(N)]) rt = 10**9 for i in range(0,N-K+1): rt = H[K - 1 + i] - H[i] if rt > H[K - 1 + i] - H[i] else rt print(rt)
p03208
import itertools N, K = (int(i) for i in input().split()) heights = [int(eval(input())) for i in range(N)] heights_lists = list(itertools.combinations(heights, K)) min_diff = max(heights) - min(heights) for heights_one in heights_lists: if min_diff > (max(heights_one) - min(heights_one) ): min_diff = (max(heights_one) - min(heights_one) ) print(min_diff)
import itertools N, K = (int(i) for i in input().split()) heights = [int(eval(input())) for i in range(N)] heights.sort() min_diff = max(heights) - min(heights) for i in range(N-K+1): diff = heights[i+K-1] - heights[i] if diff < min_diff: min_diff = diff print(min_diff)
p03208
N, K = list(map(int, input().split())) H=[] for i in range(N): H.append(int(eval(input()))) H.sort() ans=10**9 for i in range(N-K+1): ans = min(ans, max(H[i:i+K]) - min(H[i:i+K])) print(ans)
N, K = list(map(int, input().split())) H=[] for i in range(N): H.append(int(eval(input()))) H.sort() ans=10**9 for i in range(N-K+1): ans = min(ans, H[i+K-1] - H[i]) print(ans)
p03208
import sys def main() -> None: n, k = [int(s) for s in input().split()] h = [] for i in range(n): h += [int(eval(input()))] h.sort() mn = sys.maxsize for i in range(n - k + 1): cnt = h[i + k - 1] - h[i] mn = min(mn, cnt) print(mn) if __name__ == '__main__': main()
import sys n, k = [int(s) for s in input().split()] h = sorted([int(eval(input())) for _ in range(n)]) mn = sys.maxsize for i in range(n - k + 1): cnt = h[i + k - 1] - h[i] mn = min(mn, cnt) print(mn)
p03208
N, K = list(map(int, input().split())) h = [] for i in range(N): h.append(int(eval(input()))) h_sort = sorted(h) aa = h_sort[1:] bb = h_sort[:-1] diff = [a - b for a, b in zip(aa, bb)] ans = [] for i in range(len(diff) - (K - 2)): tmp = 0 for j in range(K - 1): tmp += diff[i + j] ans.append(tmp) print((min(ans)))
N,K = list(map(int,input().split())) h = [] for _ in range(N): h.append(int(eval(input()))) h.sort() minimum = h[N - 1]- h[0] for i in range(N - K + 1): minimum = min(minimum, h[i + K - 1] - h[i]) print(minimum)
p03208
import itertools N,K=list(map(int,input().split())) L=[] n=0 while n<N: L.append(int(eval(input()))) n=n+1 ans=[] for r in list(itertools.combinations(L,K)): ans.append(max(r)-min(r)) print((min(ans)))
N, K = list(map(int, input().split())) h = [int(eval(input())) for i in range(N)] h.sort() ans=[] for i in range(len(h)-K+1): b=h[K+i-1]-h[i] ans.append(b) print((min(ans)))
p03208
N, K = list(map(int, input().split())) H = [0] * N for i in range(N): H[i] = int(eval(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)
N, K = list(map(int, input().split())) H = [0] * N for i in range(N): H[i] = int(eval(input())) H.sort() d = H[K-1] - H[0] for i in range(1, N-K+1): d = min(H[i+K-1] - H[i], d) print(d)
p03208
# -*- coding: utf-8 -*- N, K = list(map(int, input().split())) H = [] for i in range(N): H.append(int(eval(input()))) H.sort() ans = H[-1] - H[0] for i in range(len(H) - K + 1): ans = min(ans, H[i + K - 1] - H[i]) print(ans)
N, K = list(map(int, input().split())) H = sorted([int(eval(input())) for _ in range(N)]) print((min([H[i + K - 1] - H[i] for i in range(N - K + 1)])))
p03208
N,K=list(map(int,input().split())) h=[int(eval(input())) for i in range(N)] h.sort() s=[] for i in range(N-K+1): a=h[i:i+K] s.append(a) cost=[] for i in range(N-K+1): cost.append(max(s[i])-min(s[i])) print((min(cost)))
N,K=list(map(int,input().split())) H=[int(eval(input())) for i in range(N)] H.sort() ans=float('inf') for i in range(N-K+1): ans=min(ans,H[i+K-1]-H[i]) print(ans)
p03208
n, k = list(map(int, input().split())) h = sorted([int(eval(input())) for _ in range(n)]) min_diff = 10**9 for i in range(n - k + 1): min_diff = min(min_diff, h[i + k - 1] - h[i]) print(min_diff)
n, k = list(map(int, input().split())) h = sorted([int(eval(input())) for _ in range(n)]) print((min(h[i + k - 1] - h[i] for i in range(n - k + 1))))
p03208
n, k = list(map(int,input().split())) h = [] for i in range(n): h.append(int(eval(input()))) h = sorted(h) minval = int(1e10) #print(h) for i in range(n-k+1): if minval >= h[i+(k-1)] - h[i]: minval = h[i+(k-1)] - h[i] print(minval)
n, k = list(map(int, input().split())) h =[] for i in range(n): h.append(int(eval(input()))) h.sort() ans = int(1e10) for i in range(n-(k-1)): if h[i+(k-1)] - h[i] < ans: ans = h[i+(k-1)] - h[i] #print(h) print(ans)
p03208
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 = list(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)
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 = list(map(int, read().split())) H.sort() print((min(H[i+K-1]-H[i] for i in range(N-K+1))))
p03208
n,k = list(map(int,input().split())) h=list(int(eval(input())) for i in range(n)) h.sort() mi=float('inf') for i in range(n-k+1): mi = min(mi, h[i+k-1]-h[i]) print(mi)
n,k = list(map(int,input().split())) h=[int(eval(input())) for i in range(n)] h.sort() mi=float('inf') for i in range(n-k+1): mi = min(mi, h[i+k-1]-h[i]) print(mi)
p03208
n, k = list(map(int, input().split())) h = sorted([int(eval(input())) for _ in range(n)]) ans = 10**9 for i in range(n-k+1): tmp = max(h[i:i+k])-min(h[i:i+k]) if ans > tmp: ans = tmp print(ans)
n, k = list(map(int, input().split())) h = sorted([int(eval(input())) for _ in range(n)]) ans = 10**9 for i in range(k-1, n): ans = min(ans, h[i]-h[i-k+1]) print(ans)
p03208
def resolve(): N, K = list(map(int, input().split())) h = [] for _ in range(N): h.append(int(eval(input()))) ans = 10 ** 18 h.sort() for i in range(N - K + 1): res = h[i:K + i] ans = min(ans, res[-1] - res[0]) print(ans) return resolve()
def resolve(): N, K = list(map(int, input().split())) h = [int(eval(input())) for _ in range(N)] ans = 10 ** 10 h.sort() for i in range(N - K + 1): res = h[i+K-1] - h[i] ans = min(ans, res) print(ans) return resolve()
p03208
n,k = list(map(int,input().split())) hlist = [int(eval(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)
n,k = list(map(int,input().split())) hlist = sorted([int(eval(input()))for i in range(n)]) ans = 10**9 i = 0 while i+(k-1) <= n-1: ans = min(ans, hlist[k+i-1]-hlist[i]) i += 1 print(ans)
p03208
N, K = list(map(int,input().split())) a = [] for i in range(N): a.append(int(eval(input()))) a.sort() ans = 10**9 + 7 for i in range(N-K+1): if ans > a[K-1+i] - a[i]: ans = a[K-1+i] - a[i] print(ans)
# 20200612_yorukatsu_c.py N, K = list(map(int,input().split())) M = [int(eval(input())) for i in range(N)] M.sort() lis = [0]*(N-K+1) for i in range(N-K+1): lis[i] = abs(M[i]-M[i+K-1]) print((min(lis)))
p03208
n,k = list(map(int,input().split())) list1 = [int(eval(input())) for i in range(n) ] list1.sort() treemin = 10**9 for i in range(len(list1)-k+1): treemin = min(treemin,list1[i+k-1]-list1[i]) print(treemin)
import sys readline = sys.stdin.buffer.readline def even(n): return 1 if n%2==0 else 0 n,k = list(map(int,readline().split())) lst1 = [] for i in range(n): lst1.append( int(readline())) lst1.sort() ans = 10**9 for i in range(n-k+1): ans = min(ans,abs(lst1[i+k-1]-lst1[i])) print(ans)
p03208
n,k = list(map(int,input().split())) h = sorted(list(map(int, [eval(input()) for i in range(n)]))) ans = int(max(h)) for j in range(n-k+1): ans = min(ans, (max(h[j:j+k])-min(h[j:j+k]))) print(ans)
N, K = list(map(int, input().split())) h = [int(eval(input())) for i in range(N)] h.sort() print((min(h[i+K-1] - h[i] for i in range(N-K+1))))
p03208
N, K = list(map(int, input().split())) height = [int(eval(input())) for i in range(N)] height.sort() h_min = [] for i in range(N - K + 1): h_min.append(height[K - 1 + i] - height[i]) if i > 0: if h_min[0] >= h_min[1]: del h_min[0] else: del h_min[1] print((h_min[0]))
# -*-coding: utf-8 -*- N, K = list(map(int, input().split())) h = [] for i in range(N): h.append(int(eval(input()))) h = sorted(h) nearest = 1000000000 for i in range(N-K+1): if nearest > h[i+K-1] - h[i]: nearest = h[i+K-1] - h[i] print(nearest)
p03208
N, K = list(map(int, input().split())) h = [int(eval(input())) for _ in range(N)] h.sort() ans = float('inf') for i in range(N - K + 1): s = h[i: i + K] ans = min(ans, max(s) - min(s)) print(ans)
N, K = list(map(int, input().split())) h = [int(eval(input())) for _ in range(N)] h.sort() ans = min([h[i + K - 1] - h[i] for i in range(N - K + 1)]) print(ans)
p03208
N, K = list(map(int, input().split())) height = [] for _ in range(N) : height.append(int(eval(input()))) height.sort() ans = float('inf') for i in range(N-K+1) : diff = height[i + K - 1] - height[i] ans = min(ans, diff) print(ans)
N, K = list(map(int, input().split())) H = [int(eval(input())) for _ in range(N)] H.sort() ans = 10**18 for i in range(N - K + 1): ans = min(ans, H[i + K - 1] - H[i]) print(ans)
p03208
import sys inint = lambda: int(sys.stdin.readline()) inintm = lambda: list(map(int, sys.stdin.readline().split())) inintl = lambda: list(inintm()) instrm = lambda: list(map(str, sys.stdin.readline().split())) instrl = lambda: list(instrm()) n, k = inintm() h = [] ans = 10**10 for _ in range(n): h.append(inint()) h.sort() for i in range(n-k+1): if h[i:i+k][-1] - h[i:i+k][0] < ans: ans = h[i:i+k][-1] - h[i:i+k][0] print(ans)
import sys inint = lambda: int(sys.stdin.readline()) inintm = lambda: list(map(int, sys.stdin.readline().split())) inintl = lambda: list(inintm()) instrm = lambda: list(map(str, sys.stdin.readline().split())) instrl = lambda: list(instrm()) n, k = inintm() h = [] ans = 10**10 for _ in range(n): h.append(inint()) h.sort() for i in range(n-k+1): if h[i+k-1] - h[i] < ans: ans = h[i+k-1] - h[i] print(ans)
p03208
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] h = sorted(h) hMin = 10**9 for i in range(n - k + 1): a = h[k + i - 1]-h[i] if a < hMin: hMin = a print(hMin)
n, k = list(map(int, input().split())) h = sorted([int(eval(input())) for _ in range(n)]) hMin = 10**9 for i in range(n - k + 1): if h[k + i - 1]-h[i] < hMin: hMin = h[k + i - 1]-h[i] print(hMin)
p03208
n, k = list(map(int, input().split())) h = sorted([int(eval(input())) for _ in range(n)]) hMin = 10**9 for i in range(n - k + 1): if h[k + i - 1]-h[i] < hMin: hMin = h[k + i - 1]-h[i] print(hMin)
n, k = list(map(int, input().split())) h = sorted([int(eval(input())) for _ in range(n)]) hMin = 10**9 print((min(h[k + i - 1]-h[i] for i in range(n - k + 1))))
p03208
#!/env/usr/python3 n, k = input().split() n = int(n) k = int(k) a = [] for i in range(n): a.append(int(eval(input()))) #print(a) a.sort() #print(a) height_distance = [] for i, h in enumerate(a): if i <= n-k: chosen = a[i:i+k] height_distance.append(max(chosen)-min(chosen)) #print(height_distance) print((min(height_distance)))
#!/env/usr/python3 n, k = input().split() n = int(n) k = int(k) a = [] for i in range(n): a.append(int(eval(input()))) #print(a) a.sort() #print(a) height_distance = [] for i, h in enumerate(a): if i <= n-k: #chosen = a[i:i+k] #height_distance.append(max(chosen)-min(chosen)) height_distance.append(a[i+k-1]-a[i]) #print(height_distance) print((min(height_distance)))
p03208
N,K=list(map(int,input().split())) h=[int(eval(input())) for i in range(N)] h.sort() res=[] for i in range(N): if i != 0: res.append(h[i]-h[i-1]) m=max(res) for i in range(N-K+1): ans=0 for j in range(K-1): ans+=res[j+i] if ans<m: m=ans print(m)
N, K = list(map(int, input().split())) h = [int(eval(input())) for i in range(N)] h.sort() res=[] for i in range(N-K+1): res.append(h[K+i-1]-h[i]) print((min(res)))
p03208
import sys from collections import deque input = sys.stdin.readline N, K = [int(x) for x in input().strip().split()] highest = 0 lowest = 10 ** 9 q = deque([]) ans = 10 ** 9 pre = -1 H = sorted([int(eval(input())) for _ in range(N)]) for n, ne in enumerate(H): q.append(ne) if len(q) > K: pre = q.popleft() if ne > highest: highest = ne if ne < lowest: lowest = ne if pre == highest: highest = max(q) if pre == lowest: lowest = min(q) if n >= K - 1: ans = min(ans, highest - lowest) print(ans)
import sys input = sys.stdin.readline N, K = [int(x) for x in input().strip().split()] H = sorted([int(eval(input())) for _ in range(N)]) ans = 10 ** 9 for n in range(N - (K - 1)): ans = min(ans, H[n+K-1] - H[n]) print(ans)
p03208
# import sys # input = sys.stdin.readline n, k = list(map(int, input().split())) h = [0]*n for i in range(n): h[i] = int(eval(input())) h.sort() ans = 10**9 for i in range(n-k+1): temp = h[i+k-1] - h[i] if ans > temp: ans = temp print(ans)
import sys input = sys.stdin.readline n, k = list(map(int, input().split())) # h = [0]*n # for i in range(n): # h[i] = int(input()) h = [int(eval(input())) for _ in range(n)] h.sort() ans = 10**9 for i in range(n-k+1): temp = h[i+k-1] - h[i] ans = min(ans, temp) print(ans)
p03208
N,K=(int(i) for i in input().split()) h=[int(eval(input())) for i in range(N)] h.sort() m = max(h[0:K]) - min(h[0:K]) for i in range(N-K+1): d = max(h[i:i+K]) - min(h[i:i+K]) if m>d: m = d print(m)
N,K=(int(i) for i in input().split()) h=[int(eval(input())) for i in range(N)] h.sort() m = h[K-1] - h[0] for i in range(N-K+1): d = h[i+K-1] -h[i] if m>d: m = d print(m)
p03208
if __name__ == "__main__": N, K = list(map(int, input().split())) h = list() for i in range(N): h.append(int(eval(input()))) h = sorted(h) res = 10 ** 9 + 1 for i in range(N-K + 1): if res > max(h[i:i+K]) - min(h[i:i+K]): res = max(h[i:i+K]) - min(h[i:i+K]) print(res)
if __name__ == "__main__": N, K = list(map(int, input().split())) h = list() for i in range(N): h.append(int(eval(input()))) h = sorted(h) res = 10 ** 9 + 1 for i in range(N-K+1): if res > h[i+K-1] - h[i]: res = h[i+K-1] - h[i] print(res)
p03208
#!/usr/bin/env python3 def main(): *N, = list(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()
#!/usr/bin/env python3 def main(): *N, = list(map(int, open(0).read().split())) h = N[2:] K = N[1] N = N[0] h.sort(reverse=True) ans = max(h) - min(h) for i in range(0, N - K + 1): d = h[i] - h[i + K -1] if d < ans: ans = d print(ans) main()
p03208
import sys sys.setrecursionlimit(10000000) n, k = list(map(int, input().split())) h = [int(eval(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, [])))
import itertools n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] score = float('inf') for x in itertools.combinations(h, k): score = min(score, max(x) - min(x)) print(score)
p03208
n,k=list(map(int,input().split())) h=sorted([int(eval(input())) for _ in range(n)]) ans=10**9 for i in range(n-k+1): cnt=max(h[i:i+k])-min(h[i:i+k]) ans=min(cnt,ans) print(ans)
n,k=list(map(int,input().split())) h=sorted([int(eval(input())) for _ in range(n)]) ans=10**9 for i in range(n-k+1): ans=min(h[k+i-1]-h[i],ans) print(ans)
p03208
n,k=list(map(int,input().split())) h=[int(eval(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)))
n,k=list(map(int,input().split())) h=[int(eval(input())) for _ in range(n)] h.sort() l=[] for i in range(n-k+1): l.append(h[i+k-1] - h[i]) print((min(l)))
p03208
n,k = list(map(int,input().split())) h = [int(eval(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)
n,k = list(map(int,input().split())) h = [int(eval(input())) for _ in range(n)] h.sort() m = 10**10 for i in range(n-k+1): b = h[i] a = h[i+k-1] if a - b < m: m = a - b print(m)
p03208
import sys def main(): if 'get_ipython' not in globals(): # For Subsession N,K = list(map(int, input().split())) h = [] for _ in range(N): h.append(int(eval(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 = list(range(i, i+K-1)) for n in max_j: tmp += (h[n+1] - h[n]) if ans > tmp: ans = tmp if ans == 0: print(ans) sys.exit() print(ans) main()
import sys def main(): if 'get_ipython' not in globals(): # For Subsession N,K = list(map(int, input().split())) h = [] for _ in range(N): h.append(int(eval(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 = (h[K-1] - h[0]) max_i = len(h)-K+1 for i in range(1,max_i): tmp = (h[i+K-1] - h[i]) if ans > tmp: ans = tmp if ans == 0: print(ans) sys.exit() print(ans) main()
p03208
n,k = list(map(int,input().split())) h = [int(eval(input())) for _ in range(n)] h = sorted(h) ans = float('inf') for i in range(n-k+1): hs = h[i:i+k] ans = min(ans,(hs[-1] - hs[0])) print(ans)
n,k = list(map(int,input().split())) h = [int(eval(input())) for _ in range(n)] h.sort() ans = float('inf') for i in range(n-k+1): ans = min(ans,(h[i+k-1] - h[i])) print(ans)
p03208
N,K = list(map(int,input().split())) H = [int(eval(input())) for _ in range(N)] H.sort() #res1 = max(H[:K])-min(H[:K]) #res2 = max(H[N-K:])-min(H[N-K:]) res = 1000000000 for i in range(N-K+1): tmp = max(H[i:i+K])-min(H[i:i+K]) if tmp < res: res = tmp print(res)
N,K = list(map(int,input().split())) H = [int(eval(input())) for _ in range(N)] H.sort() res = 1000000000 for i in range(N-K+1): #print(H) #print(H[i+K-1],H[i]) tmp = H[i+K-1]-H[i] if tmp < res: res = tmp print(res)
p03208
import heapq n, k = list(map(int, input().split())) h = [] d = [0 for _ in range(n)] ans = float('inf') for _ in range(n): # h.append(int(input())) heapq.heappush(h, int(eval(input()))) h = [heapq.heappop(h) for _ in range(n)] for i in range(n - k + 1): diff = h[i + k - 1] - h[i] ans = min(ans, diff) print(ans)
import heapq n, k = list(map(int, input().split())) h = [] d = [0 for _ in range(n)] ans = 100000000000 for _ in range(n): # h.append(int(input())) heapq.heappush(h, int(eval(input()))) h = [heapq.heappop(h) for _ in range(n)] for i in range(n - k + 1): diff = h[i + k - 1] - h[i] ans = min(ans, diff) print(ans)
p03208
N, K = list(map(int, input().split())) h = [0]*N for i in range(N): h[i] = int(eval(input())) h.sort(reverse=True) d = [] for i in range(N-K+1): d.append(h[i] - h[i+K-1]) print((min(d)))
N, K = list(map(int, input().split())) h = [int(eval(input())) for i in range(N)] h.sort(reverse=True) d = [h[i] - h[i+K-1] for i in range(N-K+1)] print((min(d)))
p03208
import sys def main(): N, K = list(map(int,input().split())) h = [0] * N for i in range(N): h[i] = int(eval(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()
import sys def main(): N, K = list(map(int,input().split())) h = [0] * N for i in range(N): h[i] = int(eval(input())) h = sorted(h) ave = [] for j in range(0,N-K+1): ave.append(abs(h[j]-h[j+K-1])) print((min(ave))) main()
p03208
n, k = list(map(int, input().split())) h = sorted(list(int(eval(input())) for _ in range(n))) res = [] for i in range(n - k + 1): res.append(h[i + k - 1] - h[i]) print((min(res)))
import sys sys.setrecursionlimit(10 ** 7) f_inf = float('inf') mod = 10 ** 9 + 7 def resolve(): n, k = list(map(int, input().split())) H = sorted(list(int(eval(input())) for _ in range(n))) res = f_inf for i in range(n - k + 1): diff = H[i + k - 1] - H[i] res = min(res, diff) print(res) if __name__ == '__main__': resolve()
p03208
N,K = list(map(int,input().split())) h = [] for i in range(N): h.append(int(eval(input()))) h.sort() tree = [] for i in range(N-K+1): tree.append(h[i+K-1]-h[i]) print((min(tree)))
N,K = list(map(int,input().split())) h = [int(eval(input())) for _ in range(N)] h.sort() ans = 1e10 for i in range(N-K+1): ans = min(ans,h[i+K-1]-h[i]) print(ans)
p03208
n, k = list(map(int, input().split())) hlist = [] for i in range(n): hlist.append(int(eval(input()))) hlist.sort() h = [] for j in range(n-k+1): h.append(max(hlist[j:j+k])-min(hlist[j:j+k])) print((min(h)))
n, k = list(map(int, input().split())) hlist = [] for i in range(n): hlist.append(int(eval(input()))) hlist.sort() h = [] for j in range(n-k+1): h.append(hlist[j+k-1]-(hlist[j])) print((min(h)))
p03208
N, K = list(map(int, input().split())) hs = sorted([int(eval(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)
N, K = list(map(int, input().split())) hs = sorted([int(eval(input())) for _ in range(N)]) min_diff = 10 ** 10 for i in range(N-K+1): min_diff = min(min_diff, hs[i+K-1] - hs[i]) print(min_diff)
p03208
n,k=list(map(int,input().split())) h=[int(eval(input())) for i in range(n)] h.sort() ans=float("inf") for i in range(len(h)-k+1): ma = max(h[i:i+k]) mi = min(h[i:i+k]) ans = min(ans,ma-mi) print(ans)
n,k=list(map(int,input().split())) h=[int(eval(input())) for i in range(n)] h.sort() ans=float("inf") for i in range(len(h)-k+1): ma = h[i+k-1] mi = h[i] ans = min(ans,ma-mi) print(ans)
p03208
N, M = list(map(int, input().split())) K = [int(eval(input())) for i in range(N)] K = sorted(K) result = 10 ** 10 for i in range(N-M+1): result = min([result, K[i+M-1]-K[i]]) print(result)
N, M = list(map(int, input().split())) K = [int(eval(input())) for i in range(N)] K = sorted(K) print((min([K[i+M-1]-K[i] for i in range(N-M+1)])))
p03208
from itertools import combinations as comb n, k = list(map(int, input().split())) p_list = [] for i in range(n): p = int(eval(input())) p_list.append(p) comb_list = list(comb(p_list, k)) min = 10000000000000000 for c in comb_list: c = sorted(list(c)) if min > c[-1]-c[0]: min = c[-1]-c[0] print(min)
n, k = list(map(int, input().split())) h_list = [] for i in range(n): h_list.append(int(eval(input()))) h_list.sort() min = h_list[-1] for j in range(n-k+1): if min > h_list[j+k-1] - h_list[j]: min = h_list[j+k-1] - h_list[j] print(min)
p03208
N,K=list(map(int,input().split())) L=[] ans=float("inf") for i in range (N): L.append(int(eval(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)
N,K=list(map(int,input().split())) L=[] ans=float("inf") for i in range (N): L.append(int(eval(input()))) S=sorted(L) for i in range(N-K+1): ans=min(ans,S[i+K-1]-S[i]) print(ans)
p03208
N,K=list(map(int,input().split())) L=[] ans=float("inf") for i in range (N): L.append(int(eval(input()))) S=sorted(L) for i in range(N-K+1): ans=min(ans,S[i+K-1]-S[i]) print(ans)
N,K=list(map(int,input().split())) L=[] for i in range(N): x=int(eval(input())) L.append(x) L=sorted(L) ans=float("inf") for i in range(N-K+1): ans=min(ans,(L[i+K-1]-L[i])) print(ans)
p03208
n, k = list(map(int, input().split())) h = [int(eval(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)
n, k = list(map(int, input().split())) height = [int(eval(input())) for _ in range(n)] height.sort() diff = [height[i+k-1] - h for i, h in enumerate(height) if i+k-1 < len(height)] print((min(diff)))
p03208
N,K = list(map(int,input().split(" "))) height = [] for i in range(N): height.append(int(eval(input()))) height.sort() min = 10**9 n = N-K+1 for j in range(n): light = height[j:j+K] hmin = light[0] hmax = light[K-1] hei = hmax-hmin if hei <= min: min = hei print(min)
N,K = list(map(int,input().split(" "))) height = [] for i in range(N): height.append(int(eval(input()))) height.sort() min = 10**9 n = N-K+1 for j in range(n): hmin = height[j] hmax = height[j+K-1] hei = hmax-hmin if hei <= min: min = hei print(min)
p03208
def main(): N, K = list(map(int, input().split())) S = [int(eval(input())) for _ in range(N)] S.sort(reverse=True) diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)] print((min(diff_arr))) if __name__ == "__main__": main()
def main(): N, K = list(map(int, input().split())) S = [int(eval(input())) for _ in range(N)] S.sort(reverse=True) diff_arr = [S[i] - S[i+K-1] for i in range(len(S)-K+1)] # [print(S[i], S[i+K-1], i, i+K-1) for i in range(len(S)-K)] # print(diff_arr) # mx_idx = diff_arr.index(min(diff_arr)) # mn_idx = mx_idx + K - 1 print((min(diff_arr))) if __name__ == "__main__": main()
p03208
N,K = list(map(int,input().split())) h = list(int(eval(input())) for i in range(N)) h.sort(reverse=True) cnt = 0 ans = [] dif = float('inf') for i in range(N): if K-1<=i: if dif>ans[0]-h[i]: dif = ans[0]-h[i] del ans[0] ans.append(h[i]) print(dif)
N,K = list(map(int,input().split())) h = list(int(eval(input())) for i in range(N)) h.sort(reverse=True) dif = float('inf') for i in range(K-1,N): dif = min(dif,h[i-K+1]-h[i]) print(dif)
p03208
n,k = list(map(int,input().split())) l = sorted([int(eval(input())) for i in range(n)]) #print(l) ansl = [] for m in range(n-k+1): ansl.append(max(l[m:m+k])-min(l[m:m+k])) print((min(ansl)))
n,k = list(map(int,input().split())) l = sorted([int(eval(input())) for i in range(n)]) print((min(l[i+k-1] - l[i] for i in range(n-k+1))))
p03208
n,k = list(map(int,input().split())) l = [] for _ in range(n): a = int(eval(input())) l.append(a) ans = 10**9 l.sort() for i in range(n-k+1): tmp = max(l[i:k+i]) - min(l[i:k+i]) if tmp < ans: ans = tmp print(ans)
n,k = list(map(int,input().split())) l = [] for _ in range(n): a = int(eval(input())) l.append(a) ans = 10**9 l.sort() for i in range(n-k+1): tmp = l[k+i-1] - l[i] if tmp < ans: ans = tmp print(ans)
p03208
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] h = sorted(h) print((min(h[i + k - 1] - h[i] for i in range(n - k + 1))))
N, K = list(map(int, input().split())) h = [int(eval(input())) for _ in range(N)] h = list(sorted(h)) ans = min(hr - hl for hr, hl in zip(h[K - 1:], h)) print(ans)
p03208
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] h.sort() print((min(hmax - hmin for hmax, hmin in zip(h[k - 1:], h))))
def main(): import sys input = sys.stdin.readline N, K = list(map(int, input().split())) h = [int(eval(input())) for _ in range(N)] h.sort() ans = min(ma - mi for mi, ma in zip(h, h[K - 1:])) print(ans) if __name__ == '__main__': main() # import sys # input = sys.stdin.readline # # sys.setrecursionlimit(10 ** 7) # # (int(x)-1 for x in input().split()) # rstrip() # # def binary_search(*, ok, ng, func): # while abs(ok - ng) > 1: # mid = (ok + ng) // 2 # if func(mid): # ok = mid # else: # ng = mid # return ok
p03208
N, K = list(map(int, input().split())) h =[int(eval(input())) for _ in range(N)] a = sorted(h) ans = min(a[i + K - 1] - a[i] for i in range(N - K + 1)) print(ans)
N, K = list(map(int, input().split())) h = [int(eval(input())) for _ in range(N)] x = sorted(h) ans = min(b - a for a, b in zip(x, x[(K - 1):])) print(ans)
p03208
n,k=list(map(int,input().split())) h=[] for i in range(n): h.append(int(eval(input()))) h.sort() ans=max(h)-min(h) for i in range(n-k+1): temp=h[i:i+k] ans=min(ans,max(temp)-min(temp)) print(ans)
n,k=list(map(int,input().split())) h=[] for i in range(n): h.append(int(eval(input()))) h.sort() ans=h[-1]-h[0] for i in range(n-k+1): ans=min(ans,h[i+k-1]-h[i]) print(ans)
p03208
# -*- coding: utf-8 -*- n,k = list(map(int, input().split(' '))) prices = [] for i in range(n): prices.append(int(eval(input()))) sortedPrices = sorted(prices) trees = [] for i in range(k): trees.append(sortedPrices.pop(0)) hdiff = 1000000000 while(True): hdiff = min( (trees[-1] - trees[0]), hdiff ) trees.pop(0) if len(sortedPrices) == 0: break trees.append(sortedPrices.pop(0)) print(hdiff)
# -*- coding: utf-8 -*- N, K = list(map(int, input().split(' '))); h = [int(eval(input())) for i in range(N)] h.sort() print(( min( h[i+K-1] - h[i] for i in range(N-K+1) ) ))
p03208
# coding: utf-8 N, K = list(map(int, input().split())) H = [] for i in range(N): h = int(eval(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)))
# coding: utf-8 N, K = list(map(int, input().split())) H = [] for i in range(N): h = int(eval(input())) H.append(h) H.sort() L = [] ans = float("inf") for i in range(N-K+1): ans = min(ans, abs(H[i] - H[i+K-1])) print(ans)
p03208
n, k = list(map(int, input().split())) h = [] for i in range(n): h_ = int(eval(input())) h.append(h_) 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)
n,k=list(map(int,input().split())) h=[int(eval(input())) for i in range(n)] h.sort(reverse=True) ans=float('inf') for i in range(n-k+1): ans=min(h[i]-h[i+k-1],ans) print(ans)
p03208
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] sh = sorted(h) u = [] for i in range(len(sh)-k+1): a = sh[i:i+k] u.append(max(a) - min(a)) print((min(u)))
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] sh = sorted(h) print((min([sh[i+k-1]-sh[i] for i in range(n-k+1)])))
p03208
import itertools def main(): N, K = list(map(int, input().split())) h = [] for i in range(N): h.append(int(eval(input()))) arr = list(itertools.permutations(h, K)) res1 = [] res2 = [] ans = [] for j in range(len(arr)): res1.append(sorted(arr[j])) for k in range(len(res1)): res2.append(sorted(res1[k])) for l in range(len(res2)): ans.append(res2[l][K-1] - res2[l][0]) print((min(ans))) if __name__ == "__main__": main()
N, K = list(map(int, input().split())) h = [int(eval(input())) for i in range(N)] h.sort() print((min(h[i+K-1] - h[i] for i in range(N-K+1))))
p03208
n,k = list(map(int,input().split())) tree = [int(eval(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)
n,k = list(map(int,input().split())) tree = [int(eval(input())) for _ in range(n)] tree.sort() ans = int(1e15) for i in range(n-k+1): ans = min(tree[i+k-1] - tree[i], ans) print(ans)
p03208
N, K = list(map(int, input().split())) H = [] for i in range(N): h = int(eval(input())) H.append(h) H.sort() ans = [] for i in range(N-K+1): dif = max(H[i:i+K]) - min(H[i:i+K]) ans.append(dif) print((min(ans)))
n,k=list(map(int,input().split())) H=[] for i in range(n): H.append(int(eval(input()))) H.sort() #print(H) ans=H[n-1]-H[0] for i in range(n-k+1): #print(H[i+k-1]-H[i]) ans=min(ans,H[i+k-1]-H[i]) print(ans)
p03208
NK = list(map(int, input().split())) a = [0]*NK[0] for i in range(len(a)): a[i] = int(eval(input())) a.sort() N = NK[0] K = NK[1] b = [0]*K offsets = [0]*(N-K+1) for i in range(len(offsets)): for j in range(K): b[j] = a[i+j] offsets[i] = b[K-1] - b[0] print((min(offsets)))
NK = list(map(int, input().split())) a = [0]*NK[0] for i in range(len(a)): a[i] = int(eval(input())) a.sort() N = NK[0] K = NK[1] minimum = a[N-1] for i in range(N-K+1): offset = a[i+K-1] - a[i] if minimum > offset: minimum = offset print(minimum)
p03208
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(eval(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)))
N, K = list(map(int, input().split())) h = [int(eval(input())) for i in range(N)] h.sort() print((min(h[i+K-1] - h[i] for i in range(N-K+1))))
p03208
import sys from operator import itemgetter as ig from collections import defaultdict as dd # お約束 args = None INF = float("inf") MOD = int(1e9 + 7) def int1(n): return int(n) - 1 def input(): return next(args) def parse(*params): if len(params) == 1: return params[0](next(args)) return tuple(p(v) for p, v in zip(params, next(args).split())) def debug(*v): if __debug__: print(*v, file=sys.stderr) # エントリーポイント def main(): N, K = parse(int, int) H = [parse(int) for _ in range(N)] h = sorted(H) l, r = 0, K sum_h = [0] for n in range(1, N): sum_h += [sum_h[-1] + (h[n] - h[n - 1])] debug(sum_h) diff_h = tuple(sum_h[l + K - 1] - sum_h[l] for l in range(N - K + 1)) debug(diff_h) print(min(diff_h)) if __name__ == '__main__': args = iter(sys.stdin.read().split("\n")) main()
import sys from operator import itemgetter as ig from collections import defaultdict as dd # お約束 args = None INF = float("inf") MOD = int(1e9 + 7) def int1(n): return int(n) - 1 def input(): return next(args) def parse(*params): if len(params) == 1: return params[0](next(args)) return tuple(p(v) for p, v in zip(params, next(args).split())) def debug(*v): if __debug__: print(*v, file=sys.stderr) # エントリーポイント def main(): N, K = parse(int, int) H = [parse(int) for _ in range(N)] h = sorted(H) diff_h = tuple(h[l + K - 1] - h[l] for l in range(N - K + 1)) print(min(diff_h)) if __name__ == '__main__': args = iter(sys.stdin.read().split("\n")) main()
p03208
n,k = input().split() n=int(n) k=int(k) h = [int(eval(input())) for i in range(n)] h.sort() #print(h) ans=10000000000 for i in range(n-k+1): list1 = [h[j] for j in range(i,i+k)] # print(list1) if max(list1)-min(list1) < ans: ans = max(list1)-min(list1) print(ans)
n,k = input().split() n=int(n) k=int(k) h = [int(eval(input())) for i in range(n)] h.sort() #print(h) ans=10000000000 for i in range(n-k+1): # list1 = [h[j] for j in range(i,i+k)] # print(list1) if h[i+k-1]-h[i] < ans: ans = h[i+k-1]-h[i] print(ans)
p03208
N,K = list(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)
N, K = list(map(int, input().split())) h = [int(eval(input())) for i in range(N)] h.sort() print((min(h[i+K-1] - h[i] for i in range(N-K+1))))
p03208
from itertools import combinations N,K=list(map(int,input().split())) H=[int(eval(input())) for _ in range(N)] ans=10**9 for i in combinations(H, K): ans=min(ans,max(i)-min(i)) print(ans)
N,K=list(map(int,input().split())) H=[int(eval(input())) for _ in range(N)] H=sorted(H) ans=10**9 for i in range(N-K+1): ans=min(ans,H[i+K-1]-H[i]) print(ans)
p03208
def christmas_eve(n,k,h): _h = sorted(h) opt = 0 ans = 10**9 + 1 for i in range(n-(k-1)): l = _h[i:i+k] opt = max(l) - min(l) if ans > opt: ans = opt return ans if __name__ == '__main__': n,k = list(map(int,input().split())) h = [int(input()) for _ in range(n)] print(christmas_eve(n,k,h))
def christmas_eve(n,k,h): _h = sorted(h) opt = 0 ans = 10**9 + 1 _k = k - 1 loop = n - _k for i in range(loop): opt = _h[i+_k] - _h[i] if ans >= opt: ans = opt return ans if __name__ == '__main__': n,k = list(map(int,input().split())) h = [int(input()) for _ in range(n)] print(christmas_eve(n,k,h))
p03208
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()
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] = H[i] - H[i+K-1] print((min(ret))) if __name__ == '__main__': main()
p03208
N, K= list(map(int, input().split())) h = list(sorted([int(eval(input())) for i in range(N)])) ans = float('inf') for i in range(N-K+1): tmp = h[i:i+K] ans = min(ans, tmp[-1] - tmp[0]) print(ans)
N, K= list(map(int, input().split())) h = list(sorted([int(eval(input())) for i in range(N)])) h_dif = [h[i+K-1] - h[i] for i in range(N-K+1)] print((min(h_dif)))
p03208
n,k=list(map(int,input().split())) h=[int(eval(input())) for _ in range(n)] h.sort() INF=2*10**9 ans=INF for i in range(n-k+1): diff=max(h[i:i+k])-min(h[i:i+k]) if ans>diff: ans=diff print(ans)
n,k=list(map(int,input().split())) h=[int(eval(input())) for _ in range(n)] h.sort() INF=2*10**9 ans=INF for i in range(n-k+1): diff=h[i+k-1]-h[i] if ans>diff: ans=diff if ans==0: print((0)) exit() print(ans)
p03208
import itertools N,K = list(map(int,(input().split()))) h = [int(eval(input()))for _ in range(N)] hmin =[] c = itertools.combinations(h,K) for v in c: hmin.append(max(v)-min(v)) print((min(hmin)))
N,K = list(map(int,(input().split()))) h = [int(eval(input()))for _ in range(N)] h.sort() print((min(h[i+K-1]-h[i] for i in range(N-K+1))))
p03208
N,K = list(map(int,input().split())) height = [] for i in range(N): height.append(int(eval(input()))) height.sort() dif = [0]*(N-1) for i in range(N-1): dif[i] = height[i+1]-height[i] diflist = [dif[0]]*(N-1) for i in range(1,N-1): diflist[i] = diflist[i-1] + dif[i] diflist.insert(0,0) ans = 10**10 for i in range(N-K+1): pro = diflist[i+K-1]-diflist[i] ans = min(ans,pro) print(ans)
import sys input = sys.stdin.readline def main(): N,K = list(map(int,input().split())) h = [int(eval(input())) for _ in range(N)] h.sort() ans = 10**10 for i in range(N-K+1): ans = min(ans,h[K+i-1]-h[i]) print(ans) if __name__ == "__main__": main()
p03208
#!/usr/bin/env python3 # Christmas Eve N = list(map(int, (input().strip().split(' ')))) K = N[1] N = N[0] i = 0 h = [] while i < N: hi = int(input().strip()) h.append(hi) i += 1 h = sorted(h) i = 0 min_i = i min_diff = 10000000000 while i < N - K + 1: sub = h[i:i+K] diff = sub[K-1] - sub[0] if min_diff > diff: min_diff = diff min_i = i i += 1 print(min_diff) """ med = sum(h)/len(h) print(list(map(lambda x:x-med, h))) """
#!/usr/bin/env python3 # Christmas Eve N = list(map(int, (input().strip().split(' ')))) K = N[1] N = N[0] i = 0 h = [] while i < N: hi = int(input().strip()) h.append(hi) i += 1 h = sorted(h) i = 0 min_diff = 10000000000 while i < N - K + 1: diff = h[i+K-1] - h[i] if min_diff > diff: min_diff = diff i += 1 print(min_diff)
p03208
# C N, K = list(map(int, input().split())) h = [0]*N for i in range(N): h[i] = int(eval(input())) h.sort() result = 1e10 for i in range(N-K+1): h_list = h[i:i+K] result = min(result, max(h_list) - min(h_list)) print(result)
# C N, K = list(map(int, input().split())) h = [0]*N for i in range(N): h[i] = int(eval(input())) h.sort() result = 1e10 for i in range(N-K+1): result = min(result, h[i+K-1]-h[i]) print(result)
p03208
n, k = list(map(int, input().split())) h = [] for i in range(n): h.append(int(eval(input()))) h.sort() d = [h[i+1]-h[i] for i in range(0, len(h)-1)] S = 0 for i in range(k-1): S += d[i] ans = S for i in range(0, n-k): SS = S + d[i+k-1] - d[i] if SS <= ans: ans = SS S = SS print(ans)
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] h.sort() print((min([h[i+k-1]-h[i] for i in range(n-k+1)])))
p03208
import itertools import math import fractions import functools n, k = list(map(int, input().split())) h = [] for i in range(n): h.append(int(eval(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)
import itertools import math import fractions import functools n, k = list(map(int, input().split())) h = [] for i in range(n): h.append(int(eval(input()))) h.sort() subs = [] for i in range(1,n): subs.append(h[i]-h[i-1]) amount = [0]*(n) sums = 0 for i in range(0,len(subs)): sums += subs[i] amount[i+1] = sums minimum = 10**10 for i in range(k-1,n): minimum = min(minimum,amount[i]-amount[i-k+1]) print(minimum)
p03208
import itertools import math import fractions import functools import copy n, k = list(map(int, input().split())) h = [] for i in range(n): h.append(int(eval(input()))) h.sort() minimum = 10**10 for i in range(n-k+1): minimum = min(minimum,h[i+k-1]-h[i]) print(minimum)
def main(): # n = int(input()) n, m = list(map(int, input().split())) # v = list(map(int, input().split())) # s = input() h = [int(eval(input())) for _ in range(n)] h.sort() temp = [] for i in range(n-m+1): temp.append(h[i+m-1]-h[i]) temp.sort() print((temp[0])) if __name__ == '__main__': main()
p03208
def resolve(): n, k = list(map(int, input().split())) H = [] for _ in range(n): H.append(int(eval(input()))) H_sort = list(sorted(H, reverse=True)) ans = 10**9 for i in range(n-k+1): ans = min(ans, H_sort[i]-H_sort[i+k-1]) print(ans) resolve()
def resolve(): n, k = list(map(int, input().split())) H_sort = list(sorted([int(eval(input())) for _ in range(n)],reverse=True)) ans = 10**9 for i in range(n-k+1): ans = min(ans, H_sort[i]-H_sort[i+k-1]) print(ans) resolve()
p03208
n,k = list(map(int,input().split())) h = [int(eval(input())) for i in range(n)] h.sort() dh = [h[i+1]-h[i] for i in range(0,n-1)] mindh = dhsum = sum(dh[:k-1]) for i in range(1,n-k+1): dhsum = dhsum-dh[i-1]+dh[i+k-2] mindh = min(dhsum,mindh) ans = mindh print(ans)
n,k = list(map(int,input().split())) h = [int(eval(input())) for i in range(n)] h.sort() mindh = 10**9 for i in range(n-k+1): mindh = min(mindh,h[i+k-1]-h[i]) ans = mindh print(ans)
p03208
import itertools n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] com = list(itertools.combinations(h, k)) ans = max(com[0]) - min(com[0]) for c in com[1:]: ans = min(ans, max(c) - min(c)) print(ans)
n, k = list(map(int, input().split())) h = [int(eval(input())) for _ in range(n)] h = sorted(h) ans = max(h[0:k]) - min(h[0:k]) for i in range(1, n-k+1): ans = min(ans, max(h[i:i+k]) - min(h[i:i+k])) print(ans)
p03208