Cherami Bartolini believes that they are only as strong as their weakest link. In college, this means if they are taking six courses, that term is only as difficult as the most difficult course they are currently taking. Based on her understanding of her proficiency in all her candidate courses, she assigns each term's difficulty rating as the highest among them. So, for example, her courses this term have difficulty ratings of 20, 1, 54, 3,, and 7; the term's overall difficulty rating is 54. Cherami is taking a unique degree where prerequisites do not exist. This effectively means she can take the courses in any order she pleases. She also insists on not taking more than K courses per term due to her desire to preserve some semblance of a social life. Given the N courses she has to take and her difficulty rating D, for each course, what is the lowest possible total difficulty rating across all her terms? Input Format Each test case begins with two space-separated integers N and K, indicating the number of courses needed to complete Cherami's degree and the maximum number of courses Cherami is willing to take in one term, respectively. N lines follow, each containing a single integer D; indicating the difficulty rating of the ith course. Constraints 1≤K≤N≤2.105 1 ≤ D₁ ≤ 10⁹
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
Output contains a single integer indicating the smallest possible total difficulty rating across all of Cherami's terms.
Sample Input 0
5 3
6
10
2
3
8
Sample Output 0
13
Sample Input 1
8 4
12
12
3
19
18
14
18
7
Sample Output 1
31
The actual code
""" Parameters: Returns: n,k = list(map(int,input().strip().split(" "))) print(f"{solve(n,k,vals)}\n") |
Step by step
Solved in 4 steps with 3 images