/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool
isPossible (int arr[], int n, int m, int curr_min)
{
int studentUsed = 1;
int pages_Reading = 0;
for (int i = 0; i < n; i++)
{
if (pages_Reading + arr[i] > curr_min)
{
studentUsed++;
pages_Reading = arr[i];
if (studentUsed > m)
{
return false;
}
}
else
{
pages_Reading += arr[i];
}
}
return true;
}
int
findpages (int arr[], int n, int m)
{
// int sum = 0;
// if N<M
if (n < m)
{
return -1;
}
// count number of pages
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
int s = arr[n - 1];
int e = sum; // sum_of_all_pages
int ans = INT_MAX;
while (s <= e)
{
int mid = (s + e) / 2;
if (isPossible (arr, n, m, mid))
{
ans = min (ans, mid);
e = mid - 1;
}
else
{
s = mid + 1;
}
}
return ans;
}
int
main ()
{
// int t;
// cin >> t;
// for (int i = 0; i < t; i++)
// {
int n, m;
cin >> n >> m;
int arr[1000];
for (int j = 0; j < n; j++)
{
cin >> arr[j];
}
cout << findpages (arr, n, m) << endl;
// }
return 0;
}