Pagini recente » Cod sursa (job #1334373) | Cod sursa (job #466358) | Cod sursa (job #2309415) | Cod sursa (job #1795385) | Cod sursa (job #2917793)
#include <bits/stdc++.h>
using namespace std ;
#define FAST ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define maxn 5000010
int n,i,k ;
int v[maxn],q[maxn] ;
long long int s ;
int front,back ;
int main()
{
freopen("deque.in","r",stdin) ;
freopen("deque.out","w",stdout) ;
cin>>n>>k ;
for(i=1; i<=n; ++i)
{
cin>>v[i] ;
}
front=1,back=0 ;
for(i=1; i<=n; ++i)
{
while(front<=back && v[i]<=v[q[back]])
back-- ;
q[++back]=i ;
if(q[front]==i-k)
front++ ;
if(i>=k)
s+=v[q[front]];
}
cout<<s ;
return 0 ;
}