Buggy python submission (verdict: Time Limit Exceeded):
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:
      print(ans)
      sys.exit()
  
  print(ans)

main()

Fixed submission:
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 = (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()

Buggy python submission (verdict: Time Limit Exceeded):
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
  
  print(ans)

main()

Fixed submission:
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 = (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()

Buggy python submission (verdict: Time Limit Exceeded):
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
    for n in range(i, i+K-1):
      tmp += (h[n+1] - h[n])
    if ans > tmp:
      ans = tmp
  
  print(ans)

main()

Fixed submission:
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 = (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()

Buggy python submission (verdict: Time Limit Exceeded):
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])
    
  for i in range(1,len(h)-K+1):
    tmp = 0
    for n in range(i, i+K-1):
      tmp += (h[n+1] - h[n])
    if ans > tmp:
      ans = tmp
  
  print(ans)

main()

Fixed submission:
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 = (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()

Buggy python submission (verdict: Runtime Error):
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)-K+1):
    if i == 0:
      ans = 0
      for n in range(0,K-1):
        ans += (h[n+1] - h[n])
    else:
      tmp = 0
      for n in range(i, i+K-1):
        tmp += (h[n+1] - h[n])
      if ans > tmp:
        ans = tmp
  
  print(ans)

main()

Fixed submission:
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 = (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()

Buggy python submission (verdict: Time Limit Exceeded):
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)-K+1):
    if i == 0:
      ans = 0
      for n in range(0,K-1):
        ans += (h[n+1] - h[n])
    else:
      tmp = 0
      for n in range(i, i+K-1):
        tmp += (h[n+1] - h[n])
      if ans > tmp:
        ans = tmp
  
  print(ans)

main()

Fixed submission:
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 = (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()

Buggy python submission (verdict: Runtime Error):
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 = 0
      for n in range(0,K-1):
        ans += (h[n+1] - h[n])
    else:
      tmp = 0
      for n in range(i, i+K-1):
        tmp += (h[n+1] - h[n])
      if ans > tmp:
        ans = tmp
  
  print(ans)

main()

Fixed submission:
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 = (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()

Buggy python submission (verdict: Runtime Error):
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    K = int(input())
    h = []
    for _ in range(N):
      h.append(int(input()))
  else:
    # N = 5
    # K g= 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()

Fixed submission:
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 = (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()