D - Single-use Stones

A lot of frogs want to cross a river. A river is ww units width, but frogs can only jump ll units long, where l<wl<w. Frogs can also jump on lengths shorter than ll. but can't jump longer. Hopefully, there are some stones in the river to help them.

The stones are located at integer distances from the banks. There are aiai stones at the distance of ii 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 ll (1l<w1051≤l<w≤105) — the width of the river and the maximum length of a frog's jump.

The second line contains w1w−1 integers a1,a2,,aw1a1,a2,…,aw−1 (0ai1040≤ai≤104), 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.

Examples
Input
10 5
0 0 1 0 2 0 0 1 0
Output
3
Input
10 3
1 1 1 1 2 1 1 1 1
Output
3
Note

In the first sample two frogs can use the different stones at the distance 55, and one frog can use the stones at the distances 33 and then 88.

In the second sample although there are two stones at the distance 55, that does not help. The three paths are: 0369100→3→6→9→100258100→2→5→8→100147100→1→4→7→10.



题意:x轴有w个点[1,2,3...w],长度为w-1的序列a,表示第i个点有a[i]个石头.
现在有一群青蛙要过河,已知青蛙的跳跃距离为[1..L],跳到的点其a[i]值必须要>0,并且每只青蛙到第i个点时,会使a[i]--.
问最多有多少只青蛙可以到达对岸

思路

每次青蛙最多跳长度 l ,那么对于任何一个大于l长度的区间来说,必须要有落脚点,那么枚举每个长度为l的区间,

更新最小的落脚点即可


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int dis[100005];

int main()
{
    int w,l;
    cin>>w>>l;

    for(int i=1;i<w;i++)
        cin>>dis[i],dis[i]+=dis[i-1];

    ll ans = 0x3f3f3f3f;

    for(int i=l;i<w;i++)
        ans = min(ans,(ll)dis[i]-dis[i-l]);

    cout<<ans<<endl;

    return 0;
}


猜你喜欢

转载自blog.csdn.net/du_mingm/article/details/80621068