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⁹

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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 algorithm in the problem. Make sure both test cases return correct answers.

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

"""
Solve a single test case.

Parameters:
n : int - number of courses to consider
k : int - maximum number of courses that can be taken in one term
vals : array-like - list of difficulty ratings of each of the n courses

Returns:
An integer indicating the smallest total difficulty rating across all terms.
"""
def solve(n,k,vals):
    # TODO

n,k = list(map(int,input().strip().split(" ")))
vals = sorted([int(input()) for i in range(n)])

print(f"{solve(n,k,vals)}\n")

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⁹
Transcribed Image Text: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⁹
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Uncertainty Problems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education