Pagini recente » Cod sursa (job #2449974) | Cod sursa (job #2463492) | Cod sursa (job #2701486) | Cod sursa (job #2687630) | Cod sursa (job #1146972)
#include<cstdio>
#include<deque>
using namespace std;
int n,k,i;
unsigned long long s;
int v[5000001];
deque<int> dq;
int main()
{
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++) scanf("%d",&v[i]);
for(i=1;i<=k;i++)
{
while(!dq.empty()&&v[i]<=v[dq.back()])
{
dq.pop_back();
}
dq.push_back(i);
}
s=s+v[dq.front()];
for(i=k+1;i<=n;i++)
{
if(dq.front()==i-k) dq.pop_front();
while(!dq.empty()&&v[i]<=v[dq.back()])
{
dq.pop_back();
}
dq.push_back(i);
s=s+v[dq.front()];
}
printf("%llu",s);
return 0;
}