All problems

Problem 706

Allocate Minimum Pages

Medium · 20 pts · Array, Binary Search

Problem

N books lie in a row, and book i has PAGES[i] pages. Give the books to M students so that every student gets at least one book, each student gets a contiguous block of books, and every book goes to exactly one student. Minimise the largest number of pages any single student receives, and print it, or -1 if M > N. Binary search on the answer: for a limit L, greedily fill students from left to right and check whether at most M students are needed.

Input

The first line contains N and M. The second line contains the N page counts.

Output

Print the minimum possible maximum number of pages, or -1.

Constraints

1 <= N, M <= 100000; 1 <= PAGES[i] <= 10^4

Example input

4 2
12 34 67 90

Example output

113

Explanation

With books 12 34 67 90 and 2 students, give 12 34 67 (113 pages) to one and 90 to the other. Any other split gives someone more than 113 pages, so the answer is 113.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Search in Rotated Sorted Array II Next: Minimize Max Distance to Gas Station