Lots of Work
Submissions: 254   Accuracy:


  Difficulty: Medium   Marks: 4

Ronit had always wished to become a writer. He became a very popular writer. However he knew that with popularity comes lots of work. Time passed and Ronit was deprived of sleep. His parents were now concerned. They advised him to minimize the amount of time he works each day. However Ronit was to complete N books in M days. He has to write the books in the given prescribed order but he can write any number of books each day. He can complete all the books before M days. He knew that he would take an amount of time q[i] to write his ith book. He wanted to find out the minimum amount of time T such that if he worked for a time less than or equal to T each day, he could complete his work in or before M days, i.e. he wanted to find T such that t[i] <= T for i=1 to N, where t[i] is the amount of time spent on the ith day. He is a very good writer but is weak in algorithms. So he wants you to help him out.

Note: Ronit cannot start working on a book on one day and complete it on the next day.

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case consists of two space separated integers, N and M, denoting number of books and number of days respectively. The second line of each test case consists of N space separated integers, the ith integer denoting the time taken to write the ith book. 

Corresponding to each test case, print the desired output in a new line.




5 2
1 3 2 1 2
4 3
1 3 4 2
5 4
4 5 3 1 2




In the first test case, Ronit has 2 days to complete 5 books. He completes the first 2 books on the first day and takes a total time of 4<=5. He completes the remaining books on the second day and takes a total time of 5<=5. Hence T=5.

The remaining test cases follow from the same logic.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Arpan Kundu
Author: akundusaltlake

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.