Getting WA on test case 4 . The link to the question https://codeforces.me/edu/course/2/lesson/6/3/practice/contest/285083/problem/B Can anyone help? what am i doing wrong?
#include <bits/stdc++.h>
#include <stdio.h>
#define repll(i, a, n) for (lli i = a; i <= n; i++)
#define lli long long int
#define ii int, int
#define pii pair<int, int>
#define pll pair<long, long>
#define plli pair<long long int, long long int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vlli vector<long long int>
#define endl '\n'
#define fastio ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0)
using namespace std;
const int mod = 1000000007;
lli n, k, l, r, mid, ans(-1);
vlli a;
lli segments(lli target)
{
lli cnt(0), sum(0);
repll(i, 0, n - 1)
{
if (a[i] > target)
return k + 1;
sum += a[i];
if (sum > target)
{
cnt++;
sum = a[i];
}
}
cnt++;
return cnt;
}
void solve()
{
cin >> n >> k;
a.resize(n);
repll(i, 0, n - 1) cin >> a[i], r += a[i];
l = 0, r += 10000;
while (l <= r)
{
mid = (l + r) / 2;
lli seg = segments(mid);
if (seg == k)
{
ans = mid;
r = mid - 1;
}
else if (seg > k)
{
l = mid + 1;
}
else
r = mid - 1;
}
cout << ans << endl;
return;
}
int32_t main()
{
fastio;
int t = 1;
while (t--)
{
solve();
}
}