#define _CRT_SECURE_NO_WARNINGS
#define _CRT_DISABLE_PERFCRIT_LOCKS
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define v vector
#define ll long long
#define umap unordered_map
#define uset unordered_set
#define f(array) (array).begin(), (array).end()
#define b(array) (array).rbegin(), (array).rend()
bool test(const v <int>& A, int k, ll maxSum) {
int count = 1;
ll curr = 0;
for (int num : A) {
if (curr + num > maxSum) {
count++;
curr = num;
if (count > k) {
return false;
}
} else {
curr += num;
}
}
return true;
}
signed main() {
cin.tie(nullptr), cout.tie(nullptr)
-> ios_base::sync_with_stdio(false);
#define file "main"
#ifndef ONLINE_JUDGE
freopen (file ".inp", "r", stdin);
freopen (file ".out", "w", stdout);
#endif
int n, k;
cin >> n >> k;
v <int> A(n);
ll left = 0, right = 0;
for (int i = 0; i < n; i++) {
cin >> A[i];
left = max(left, (ll)A[i]);
right += A[i];
}
ll result = right;
while (left <= right) {
ll mid = (left + right) / 2;
if (test(A, k, mid)) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
cout << result << endl;
return 0;
}
I2RlZmluZSBfQ1JUX1NFQ1VSRV9OT19XQVJOSU5HUwojZGVmaW5lIF9DUlRfRElTQUJMRV9QRVJGQ1JJVF9MT0NLUwojcHJhZ21hIEdDQyBvcHRpbWl6ZSgiZmFzdC1tYXRoIikKI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIm5vLXN0YWNrLXByb3RlY3RvciIpIAojcHJhZ21hIEdDQyBvcHRpbWl6ZSgiT2Zhc3QsdW5yb2xsLWxvb3BzIikKI3ByYWdtYSBHQ0MgdGFyZ2V0KCJhdngsYXZ4MixmbWEiKQoKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSB2IHZlY3RvciAKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSB1bWFwIHVub3JkZXJlZF9tYXAKI2RlZmluZSB1c2V0IHVub3JkZXJlZF9zZXQKI2RlZmluZSBmKGFycmF5KSAoYXJyYXkpLmJlZ2luKCksIChhcnJheSkuZW5kKCkKI2RlZmluZSBiKGFycmF5KSAoYXJyYXkpLnJiZWdpbigpLCAoYXJyYXkpLnJlbmQoKQoKYm9vbCB0ZXN0KGNvbnN0IHYgPGludD4mIEEsIGludCBrLCBsbCBtYXhTdW0pIHsKICAgIGludCBjb3VudCA9IDE7CiAgICBsbCBjdXJyID0gMDsKCiAgICBmb3IgKGludCBudW0gOiBBKSB7CiAgICAgICAgaWYgKGN1cnIgKyBudW0gPiBtYXhTdW0pIHsKICAgICAgICAgICAgY291bnQrKzsKICAgICAgICAgICAgY3VyciA9IG51bTsKICAgICAgICAgICAgaWYgKGNvdW50ID4gaykgewogICAgICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY3VyciArPSBudW07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHRydWU7Cn0KCnNpZ25lZCBtYWluKCkgeyAKICAgIGNpbi50aWUobnVsbHB0ciksIGNvdXQudGllKG51bGxwdHIpIAogICAgLT4gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CgogICAgI2RlZmluZSBmaWxlICJtYWluIgogICAgI2lmbmRlZiBPTkxJTkVfSlVER0UKICAgICAgICAgICAgZnJlb3BlbiAoZmlsZSAiLmlucCIsICJyIiwgc3RkaW4pOwogICAgICAgICAgICBmcmVvcGVuIChmaWxlICIub3V0IiwgInciLCBzdGRvdXQpOwogICAgI2VuZGlmICAKCiAgICBpbnQgbiwgazsKICAgIGNpbiA+PiBuID4+IGs7CiAgICB2IDxpbnQ+IEEobik7CiAgICBsbCBsZWZ0ID0gMCwgcmlnaHQgPSAwOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgY2luID4+IEFbaV07CiAgICAgICAgbGVmdCA9IG1heChsZWZ0LCAobGwpQVtpXSk7CiAgICAgICAgcmlnaHQgKz0gQVtpXTsKICAgIH0KCiAgICBsbCByZXN1bHQgPSByaWdodDsKICAgIHdoaWxlIChsZWZ0IDw9IHJpZ2h0KSB7CiAgICAgICAgbGwgbWlkID0gKGxlZnQgKyByaWdodCkgLyAyOwogICAgICAgIGlmICh0ZXN0KEEsIGssIG1pZCkpIHsKICAgICAgICAgICAgcmVzdWx0ID0gbWlkOwogICAgICAgICAgICByaWdodCA9IG1pZCAtIDE7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgbGVmdCA9IG1pZCArIDE7CiAgICAgICAgfQogICAgfQoKICAgIGNvdXQgPDwgcmVzdWx0IDw8IGVuZGw7CgogICAgcmV0dXJuIDA7ICAgCn0=