A lot of frogs want to cross a river. A river is ww units width, but frogs can only jump l units long, where l
The stones are located at integer distances from the banks. There are aiai stones at the distance of i units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water.
What is the maximum number of frogs that can cross the river, given that then can only jump on the stones?
Input
The first line contains two integers ww and l (1≤l
The second line contains w−1 integers a1,a2,…,aw−1 (0≤ai≤10^4), where aiai is the number of stones at the distance ii from the bank the frogs are currently at.
Output
Print a single integer — the maximum number of frogs that can cross the river.
const int N = 1e6 + 50;
int a[N], n, l;
bool check(int mid)
{
dequeque;
que.push_back({ 0,mid });
for (int i = 1; i < n; i++)
{
if (a[i] == 0)continue;
if (i - que.front().first > l)return 0;
int x = 0;
while (!que.empty() && x < a[i])
{
auto ans = que.front();
que.pop_front();
if (x + ans.second <= a[i])x += ans.second;
else
{
ans.second -= (a[i] - x);
x = a[i];
que.push_front(ans);
}
}
que.push_back({ i,x });
}
return n - que.front().first <= l;
}
void solve()
{
cin >> n >> l;
for (int i = 1; i < n; i++)cin >> a[i];
int l = 0, r = 1e9;
while (l < r)
{
int mid = (l + r + 1) / 2;
if (check(mid))l = mid;
else r = mid - 1;
}
cout << l;
}