Array PartitioningMedium

Array Partitioning

Medium+30 XP · +10 Aurabinary searchdynamic programminggreedy

You're working on a new feature for UptoSkills Arena that optimizes data storage. You have an array of N positive integers, representing data block sizes. You need to partition this array into K contiguous subarrays. Your goal is to minimize the maximum sum among all these K subarrays. Find this minimum possible value for the maximum sum.

Input

The first line contains two integers, N (the number of elements in the array) and K (the number of subarrays). The second line contains N positive integers representing the array elements.

Output

Print a single integer: the minimum possible value of the maximum sum among the K subarrays.

Constraints

1 <= N <= 200000 1 <= K <= N 1 <= a[i] <= 10^9

Time limit 2 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5 2
7 2 5 10 8
Output
18

The array can be partitioned into [7, 2, 5] and [10, 8]. The sums are 14 and 18. The maximum sum is 18. This is the minimum possible maximum sum. Another partition could be [7, 2] and [5, 10, 8] (sums 9, 23, max 23). Or [7] and [2, 5, 10, 8] (sums 7, 25, max 25).

Example 2

Input
4 3
1 1 1 1
Output
2

The array can be partitioned into [1], [1], [1, 1]. The sums are 1, 1, 2. The maximum sum is 2. Alternatively, [1, 1], [1], [1] also gives max sum 2.

Hints