Every universe in the infinite Omniverse is now unstable due to unforeseen circumstances. Doctor Bizarro cannot save all realities, but through a strategic use of his abilities, perhaps he can save some realities to give them enough time to branch off and repopulate the Omniverse. Within reach of Doctor Bizarro's powers are N universes. Unfortunately, all universes outside his reach are doomed and will no longer be of concern in this scenario. Each universe has an instability index X₂. Doctor Bizarro can choose two universes within his powers' reach and collide them with an incursion to merge their realities. This will cause their instability indices to be added up. Doctor Bizarro can perform up to K incursions before his power corrupts his mind with insanity. Doctor Bizarro's goal is to use his K incursions to minimize the maximum instability index among the remaining universes. Can you find the smallest possible maximum instability assuming you perform the incursions optimally? Input Format Each test case begins with a line containing two space-separated integers N and K, indicating the number of universes in Doctor Bizarro's reach and the number of incursions he must perform, respectively. N lines follow, each containing a single integer X₁, the instability index of the ith universe. Constraints 1≤K
Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible (nlogn or n at best, no n^2). Apply greedy
Output Format
The output consists of one line containing a single integer denoting the smallest possible maximum instability index among all the remaining universes.
Sample Input 0
5 2
1
3
1
3
4
Sample Output 0
5
Sample Input 1
6 4
8
7
5
8
7
6
Sample Output 1
25
Sample Input 2
8 7
13
8
13
3
3
10
5
4
Sample Output 2
59
The actual code
""" Parameters: Returns: n,k = list(map(int,input().strip().split(" "))) xs = [int(input()) for i in range(n)] print(f"{solve(n,k,xs)}\n") |
Step by step
Solved in 4 steps with 2 images