Skip to content

Instantly share code, notes, and snippets.

@soscler
Created May 18, 2019 13:12
Show Gist options
  • Save soscler/99f4f5c247aa2c9afbdfce9ee1a7891e to your computer and use it in GitHub Desktop.
Save soscler/99f4f5c247aa2c9afbdfce9ee1a7891e to your computer and use it in GitHub Desktop.
import sys
def solve(n,p,s):
s = sorted(s, reverse=True)
min_time = sys.maxsize
for i in range(n - p + 1):
sub = s[i:i+p]
sub_time = p * s[i] - sum(sub)
if(sub_time <= min_time):
min_time = sub_time
return min_time
def resolve():
T = int(input())
for t in range(T):
n,p = [int(i) for i in input().split()]
s = [int(i) for i in input().split()]
res = (solve(n, p, s))
print("case #" + str(t + 1) + ": " + str(res))
resolve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment